RequestBatch
open class RequestBatch: ObjectBase
Encapsulates an OData batch request.
Example using dynamic API
open func processBatchExample() throws -> Void {
defer {
DebugConsole.traceOut("example.NorthwindClient.processBatchExample")
}
do {
DebugConsole.traceIn("example.NorthwindClient.processBatchExample")
do {
let service = self.service
let suppliersEntitySet = service.entitySet(withName: "Suppliers")
let productsEntitySet = service.entitySet(withName: "Products")
let supplierEntityType = suppliersEntitySet.entityType
let companyNameProperty = supplierEntityType.property(withName: "CompanyName")
let productEntityType = productsEntitySet.entityType
let productNameProperty = productEntityType.property(withName: "ProductName")
let supplierProperty = productEntityType.property(withName: "Supplier")
let supplier1 = try service.executeQuery(DataQuery()
.from(suppliersEntitySet).top(1))
.requiredEntity()
let supplier2 = supplier1.copyEntity()
let supplier3 = supplier1.copyEntity()
let supplier4 = supplier1.copyEntity()
companyNameProperty.setStringValue(in: supplier2, to: "Alpha Inc.")
companyNameProperty.setStringValue(in: supplier3, to: "Beta Inc.")
try service.createEntity(supplier2)
try service.createEntity(supplier3)
companyNameProperty.setStringValue(in: supplier3, to: "Gamma Inc.")
let product1 = try service.executeQuery(DataQuery()
.from(productsEntitySet).top(1))
.requiredEntity()
let product2 = product1.copyEntity()
productNameProperty.setStringValue(in: product2, to: "Delta Cake")
let batch = RequestBatch()
let changes = ChangeSet()
changes.createEntity(supplier4)
changes.updateEntity(supplier3)
changes.deleteEntity(supplier2)
changes.createEntity(product2)
changes.createLink(from: product2, property: supplierProperty,
to: supplier4)
let query = DataQuery().from(suppliersEntitySet)
batch.addChanges(changes)
batch.addQuery(query)
try service.processBatch(batch)
let suppliers = try batch.queryResult(for: query).entityList()
try Example.show("There are now ",
Example.formatInt(suppliers.length), " suppliers.")
}
}
}
Example using proxy classes
open func processBatchExample() throws -> Void {
defer {
DebugConsole.traceOut("example.NorthwindProxyClient.processBatchExample")
}
do {
DebugConsole.traceIn("example.NorthwindProxyClient.processBatchExample")
do {
let service = self.service
let supplier1 = try service.fetchSupplier(matching: DataQuery()
.top(1))
let supplier2 = supplier1.copy()
let supplier3 = supplier1.copy()
let supplier4 = supplier1.copy()
supplier2.companyName = "Alpha Inc."
supplier3.companyName = "Beta Inc."
try service.createEntity(supplier2)
try service.createEntity(supplier3)
supplier3.companyName = "Gamma Inc."
let product1 = try service.fetchProduct(matching: DataQuery().top(1))
let product2 = product1.copy()
product2.productName = "Delta Cake"
let batch = RequestBatch()
let changes = ChangeSet()
changes.createEntity(supplier4)
changes.updateEntity(supplier3)
changes.deleteEntity(supplier2)
changes.createEntity(product2)
changes.createLink(from: product2, property: Product.supplier,
to: supplier4)
let query = DataQuery()
.from(NorthwindServiceMetadata.EntitySets.suppliers)
batch.addChanges(changes)
batch.addQuery(query)
try service.processBatch(batch)
let suppliers = try Supplier.array(from: batch.queryResult(for: query)
.entityList())
try Example.show("There are now ", Example.formatInt(suppliers.count),
" suppliers.")
}
}
}
-
Response status (e.g. HTTP status code 200 = OK).
Declaration
Swift
final public var status: Int = (0 as Int)
-
Error if
status
does not represent a successful response.Declaration
Swift
final public var error: DataServiceError?
-
Default initializer.
Declaration
Swift
override public init()
-
Add a change set to this request batch.
Declaration
Swift
open func addChanges(_ changes: ChangeSet, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void
Parameters
changes
Change set.
headers
Change set headers.
options
Change set options.
-
Add a data query to this request batch.
Declaration
Swift
open func addQuery(_ query: DataQuery, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void
Parameters
query
Data query.
headers
Data query headers.
options
Data query options.
-
Add a query result to this request batch.
Declaration
Swift
open func addQueryResult(query: DataQuery, result: QueryResult) -> Void
Parameters
query
Data query, which must have been previously added to this batch using
addQuery
.result
Query result.
-
Declaration
Swift
open func changeSet(at index: Int) -> ChangeSet
Parameters
index
From zero to
size - 1
.Return Value
The change set, if
isChangeSet(index)
istrue
; otherwise throwsundefined
. -
Declaration
Swift
open func dataQuery(at index: Int) -> DataQuery
Parameters
index
From zero to
size - 1
.Return Value
The data query, if
isDataQuery(index)
istrue
; otherwise throwsundefined
. -
Declaration
Swift
open func function(at index: Int) -> DataQuery
Parameters
index
From zero to
size - 1
Return Value
The DataQuery for the Function, if
isFunction(index)
istrue
; otherwise throwsundefined
-
Declaration
Swift
open func headers(at index: Int) -> HTTPHeaders
Parameters
index
From zero to
size - 1
.Return Value
The HTTP headers for the request at
index
. -
Add a function to the batch. If the method is not a function, throws an
undefined
.See also
DataQuery.bind
, for setting the binding parameter for a bound function. Yo can set it on the returned DataQuery by calling load method.Declaration
Swift
open func invokeFunction(method: DataMethod, parameters: ParameterList = ParameterList.empty, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> DataQuery
Parameters
method
Function to be called.
parameters
Method parameters.
headers
Request-specific headers.
options
Request-specific options.
Return Value
The DataQuery with the function to be invoked in the batch request.
-
Declaration
Swift
open func isChangeSet(at index: Int) -> Bool
Parameters
index
From zero to
size - 1
.Return Value
true
ifindex
is a valid request index, and the request at that index is aChangeSet
; otherwisefalse
. -
Declaration
Swift
open func isDataQuery(at index: Int) -> Bool
Parameters
index
From zero to
size - 1
.Return Value
true
ifindex
is a valid request index, and the request at that index is aDataQuery
; otherwisefalse
. -
Declaration
Swift
open func isFunction(at index: Int) -> Bool
Parameters
index
From zero to
size - 1
Return Value
true
ifindex
is a valid change index, and the request at that index is for a function -
Declaration
Swift
open func options(at index: Int) -> RequestOptions
Parameters
index
From zero to
size - 1
.Return Value
The request options for the request at
index
. -
See also
addQuery
.Declaration
Swift
open func queryResult(for query: DataQuery) -> QueryResult
Parameters
query
Data query.
Return Value
The result of a data query within this batch.
-
The number of requests in this request batch.
Declaration
Swift
open var size: Int