RequestBatch

open class RequestBatch : ObjectBase

Encapsulates an OData batch request.

See also

ChangeSet, DataQuery, DataService.processBatch.

Example using dynamic API

open func processBatchExample() throws -> Void {
    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 {
    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)
    changes.updateLink(from: product2, property: Product.supplier, to: supplier3)
    changes.deleteLink(from: product2, property: Product.supplier, to: supplier3)
    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.")
}
  • Should the server continue executing all requests in a batch if one or more of the requests returns an error status. Defaults to true.

    Declaration

    Swift

    final public var continueOnError: Bool
  • Default initializer.

    Declaration

    Swift

    override public init()
  • Declaration

    Swift

    open func action(at index: Int) -> DataQuery

    Parameters

    index

    From zero to size - 1

    Return Value

    The DataQuery for an action call, if isAction(index) is true; otherwise throws undefined.

  • Declaration

    Swift

    open func actionResult(for call: DataQuery, at index: Int = Int(Int32.min)) -> QueryResult

    Parameters

    call

    DataQuery containing the action call.

    index

    From zero to size - 1. Index for the query. Omit if unknown, in which case the query will be located by a linear search.

    Return Value

    The QueryResult for an action call.

  • Add an action call to this request batch.

    Example using proxy classes

    open func invokeActionsInBatch() throws -> Void {
        let service = self.service
        let batch = RequestBatch()
        let action1 = DataQuery()
        let action2 = DataQuery()
        try service.deleteVideosInCategory("Romance", query: action1.deferExecution())
        try service.deleteVideosInCategory("Suspense", query: action2.deferExecution())
        batch.addAction(call: action1)
        batch.addAction(call: action2)
        try service.processBatch(batch)
        batch.checkActionResult(for: action1)
        batch.checkActionResult(for: action2)
    }
    

    Declaration

    Swift

    open func addAction(call: DataQuery, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)

    Parameters

    call

    Query with non-null DataQuery.methodCall for an action method.

    headers

    Action call headers.

    options

    Action call options.

  • Add a change set to this request batch.

    Declaration

    Swift

    open func addChanges(_ changes: ChangeSet, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)

    Parameters

    changes

    Change set.

    headers

    Change set headers.

    options

    Change set options.

  • Add a function call to this request batch.

    Example using proxy classes

    open func invokeFunctionsInBatch() throws -> Void {
        let service = self.service
        let batch = RequestBatch()
        let query1 = DataQuery()
        let query2 = DataQuery()
        _ = try service.videosInCategory("Romance", query: query1.deferExecution())
        _ = try service.videosInCategory("Suspense", query: query2.deferExecution())
        batch.addFunction(call: query1)
        batch.addFunction(call: query2)
        try service.processBatch(batch)
        let videos1 = try Video.array(from: batch.functionResult(for: query1)
            .entityList())
        let videos2 = try Video.array(from: batch.functionResult(for: query2)
            .entityList())
        self.showVideos(videos1)
        self.showVideos(videos2)
    }
    

    Declaration

    Swift

    open func addFunction(call: DataQuery, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)

    Parameters

    call

    Query with non-null DataQuery.methodCall for a function method.

    headers

    Function call headers.

    options

    Function call options.

  • Add a data query to this request batch.

    Declaration

    Swift

    open func addQuery(_ query: DataQuery, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)

    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, index: Int = Int(Int32.min))

    Parameters

    query

    Data query, which must have been previously added to this batch using addQuery.

    index

    From zero to size - 1. Index for the query. Omit if unknown, in which case the query will be located by a linear search.

    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) is true; otherwise throws undefined.

  • Call QueryResult.check on the QueryResult for an action call. Throws DataServiceException if the action failed.

    Declaration

    Swift

    open func checkActionResult(for call: DataQuery, at index: Int = Int(Int32.min))

    Parameters

    call

    DataQuery containing the action call.

    index

    From zero to size - 1. Index for the query. Omit if unknown, in which case the query will be located by a linear search.

  • Declaration

    Swift

    open func dataQuery(at index: Int) -> DataQuery

    Parameters

    index

    From zero to size - 1.

    Return Value

    The data query, if isDataQuery(index) is true; otherwise throws undefined.

  • Add a “download media” query to the batch, to obtain a stream for downloading the content of a media entity from the target system. Caution: streams are often used for large content that may not fit (all at once) in available application memory. Having too many threads simultaneously downloading streams, or using ByteStream.readAndClose, may result in out-of-memory conditions on memory-constrained devices.

    See also

    QueryResult.getByteStream, QueryResult.getCharStream.

    Declaration

    Swift

    open func downloadMedia(entity: EntityValue, headers: HTTPHeaders? = nil, options: RequestOptions? = nil) -> DataQuery

    Parameters

    entity

    Entity whose content is to be downloaded.

    headers

    Request-specific headers.

    options

    Request-specific options.

    Return Value

    A data query representing the media download request.

  • Add a “download stream” query to the batch, to obtain a stream for downloading the content of a stream property from the target system. Caution: streams are often used for large content that may not fit (all at once) in available application memory. Having too many threads simultaneously downloading streams, or using ByteStream.readAndClose, may result in out-of-memory conditions on memory-constrained devices.

    See also

    QueryResult.getByteStream, QueryResult.getCharStream.

    Declaration

    Swift

    open func downloadStream(entity: EntityValue, link: StreamLink, headers: HTTPHeaders? = nil, options: RequestOptions? = nil) -> DataQuery

    Parameters

    entity

    Entity containing the stream property whose content is to be downloaded.

    link

    Stream link for the stream to be downloaded.

    headers

    Request-specific headers.

    options

    Request-specific options.

    Return Value

    A data query representing the stream download request.

  • Error if status does not represent a successful response.

    Declaration

    Swift

    public final var error: DataServiceError? { get set }
  • Declaration

    Swift

    open func function(at index: Int) -> DataQuery

    Parameters

    index

    From zero to size - 1

    Return Value

    The DataQuery for a function call, if isFunction(index) is true; otherwise throws undefined.

  • Declaration

    Swift

    open func functionResult(for call: DataQuery, at index: Int = Int(Int32.min)) -> QueryResult

    Parameters

    call

    DataQuery containing the function call.

    index

    From zero to size - 1. Index for the query. Omit if unknown, in which case the query will be located by a linear search.

    Return Value

    The QueryResult for a function call.

  • 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 call to the batch.

    See also

    DataQuery.bind and DataQuery.from, for setting the binding parameter for a bound function (either can be applied to the returned query).

    Declaration

    Swift

    open func invokeFunction(method: DataMethod, parameters: ParameterList = ParameterList.empty, headers: HTTPHeaders? = nil, options: RequestOptions? = nil) -> DataQuery

    Parameters

    method

    Function to be called.

    parameters

    Function call parameters.

    headers

    Function call headers.

    options

    Function call options.

    Return Value

    A data query representing the function call.

  • Declaration

    Swift

    open func isAction(at index: Int) -> Bool

    Parameters

    index

    From zero to size - 1

    Return Value

    true if index is a valid change index, and the request at that index is for an action call.

  • Declaration

    Swift

    open func isChangeSet(at index: Int) -> Bool

    Parameters

    index

    From zero to size - 1.

    Return Value

    true if index is a valid request index, and the request at that index is a ChangeSet; otherwise false.

  • Declaration

    Swift

    open func isDataQuery(at index: Int) -> Bool

    Parameters

    index

    From zero to size - 1.

    Return Value

    true if index is a valid request index, and the request at that index is a DataQuery; otherwise false.

  • Declaration

    Swift

    open func isFunction(at index: Int) -> Bool

    Parameters

    index

    From zero to size - 1

    Return Value

    true if index is a valid change index, and the request at that index is for a function call.

  • 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, index: Int = Int(Int32.min)) -> QueryResult

    Parameters

    query

    Data query.

    index

    From zero to size - 1. Index for the query. Omit if unknown, in which case the query will be located by a linear search.

    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 { get }
  • Response status (e.g. HTTP status code 200 = OK).

    Declaration

    Swift

    public final var status: Int { get set }