ChangeSet

open class ChangeSet: ObjectBase

Encapsulates an OData change set. A change set is used to group a set of entity or link changes into a single unit of work, like an atomic database transaction.

Example using proxy classes

open func applyChangesExample() throws -> Void {
    let service = self.service
    let suppliers = try service.fetchSuppliers(matching: DataQuery().top(2))
    let products = try service.fetchProducts(matching: DataQuery().top(3))
    let product1 = products[0].copy()
    let product2 = products[1].copy()
    let product3 = products[2].copy()
    product1.productName = "Blueberry Muffins"
    product2.productName = "Strawberry Yoghurt"
    product3.productName = "Raspberry Pie"
    let entityCreates = ChangeSet()
    entityCreates.createEntity(product1)
    entityCreates.createEntity(product2)
    entityCreates.createEntity(product3)
    try service.applyChanges(entityCreates)
    let entityChanges = ChangeSet()
    product2.productName = "Blackberry Yoghurt"
    entityChanges.updateEntity(product2)
    entityChanges.deleteEntity(product3)
    try service.applyChanges(entityChanges)
    let linkChanges = ChangeSet()
    let supplier1 = suppliers[0]
    let supplier2 = suppliers[1]
    linkChanges.createLink(from: product1, property: Product.supplier,
        to: supplier1)
    linkChanges.updateLink(from: product1, property: Product.supplier,
        to: supplier2)
    linkChanges.deleteLink(from: product1, property: Product.supplier)
    try service.applyChanges(linkChanges)
}

Example using dynamic API

open func applyChangesExample() throws -> Void {
    let service = self.service
    let suppliersEntitySet = service.entitySet(withName: "Suppliers")
    let productsEntitySet = service.entitySet(withName: "Products")
    let productEntityType = productsEntitySet.entityType
    let productNameProperty = productEntityType.property(withName: "ProductName")
    let supplierProperty = productEntityType.property(withName: "Supplier")
    let suppliers = try service.executeQuery(DataQuery().from(suppliersEntitySet)
        .top(2))
        .entityList()
    let products = try service.executeQuery(DataQuery().from(productsEntitySet)
        .top(3))
        .entityList()
    let product1 = products.item(at: 0).copyEntity()
    let product2 = products.item(at: 1).copyEntity()
    let product3 = products.item(at: 1).copyEntity()
    productNameProperty.setStringValue(in: product1, to: "Blueberry Yoghurt")
    productNameProperty.setStringValue(in: product2, to: "Strawberry Yoghurt")
    productNameProperty.setStringValue(in: product3, to: "Raspberry Pie")
    let entityCreates = ChangeSet()
    entityCreates.createEntity(product1)
    entityCreates.createEntity(product2)
    entityCreates.createEntity(product3)
    try service.applyChanges(entityCreates)
    let entityChanges = ChangeSet()
    productNameProperty.setStringValue(in: product2, to: "Blackberry Yoghurt")
    entityChanges.updateEntity(product2)
    entityChanges.deleteEntity(product3)
    try service.applyChanges(entityChanges)
    let linkChanges = ChangeSet()
    let supplier1 = suppliers.item(at: 0)
    let supplier2 = suppliers.item(at: 1)
    linkChanges.createLink(from: product1, property: supplierProperty,
        to: supplier1)
    linkChanges.updateLink(from: product1, property: supplierProperty,
        to: supplier2)
    linkChanges.deleteLink(from: product1, property: supplierProperty)
    try service.applyChanges(linkChanges)
}
  • 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()
  • Declaration

    Swift

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

    Parameters

    index

    From zero to size - 1

    Return Value

    The DataQuery for the action, if isAction(index) is true, otherwise throws undefined

  • Declaration

    Swift

    open func actionResult(for actionQuery: DataQuery) -> QueryResult

    Parameters

    actionQuery

    The DataQuery containing the action call.

    Return Value

    The QueryResult for the action. Throws UsageException if the result cannot be found for the query.

  • Add a query result for action to this changeset.

    Declaration

    Swift

    open func addActionResult(query: DataQuery, result: QueryResult) -> Void

    Parameters

    query

    Data query for action, which must have been previously added to this changeset using invokeAction.

    result

    Query result.

  • Add a pending created entity to the change set. The entity will be created when this change set is submitted.

    Declaration

    Swift

    open func createEntity(_ entity: EntityValue, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void

    Parameters

    entity

    Entity to be created.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • Add a pending created link to the change set. The link will be created when this change set is submitted.

    Declaration

    Swift

    open func createLink(from: EntityValue, property: Property, to: EntityValue, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void

    Parameters

    from

    Source entity for the link to be created.

    property

    Source navigation property for the link to be created.

    to

    Target entity for the link to be created.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • Add a pending created entity to the change set, related to a parent entity via a parent navigation property. The entity will be created when this change set is submitted.

    Example using proxy classes

    open func createRelatedEntityInChangeSetExample() throws -> Void {
        defer {
            DebugConsole.traceOut("example.NorthwindProxyClient.createRelatedEntityInChangeSetExample")
        }
        do {
            DebugConsole.traceIn("example.NorthwindProxyClient.createRelatedEntityInChangeSetExample")
            do {
                let service = self.service
                let customers = try service.fetchCustomers(matching: DataQuery()
                    .filter(Customer.customerID.equal("ALFKI")))
                let orders = try service.fetchOrders(matching: DataQuery().top(2))
                let changes = ChangeSet()
                let newCustomer = customers.first!.copy()
                changes.createEntity(newCustomer)
                let firstOrder = orders.first!.copy()
                let secondOrder = orders.last!.copy()
                changes.createRelatedEntity(firstOrder, in: newCustomer,
                    property: Customer.orders)
                changes.createRelatedEntity(secondOrder, in: newCustomer,
                    property: Customer.orders)
                try service.applyChanges(changes)
            }
        }
    }
    

    Example using dynamic API

    open func createRelatedEntityInChangeSetExample() throws -> Void {
        defer {
            DebugConsole.traceOut("example.NorthwindClient.createRelatedEntityInChangeSetExample")
        }
        do {
            DebugConsole.traceIn("example.NorthwindClient.createRelatedEntityInChangeSetExample")
            do {
                let service = self.service
                let customersEntitySet = service.entitySet(withName: "Customers")
                let ordersEntitySet = service.entitySet(withName: "Orders")
                let customerEntityType = customersEntitySet.entityType
                let customerIDProperty = customerEntityType.property(withName: "CustomerID")
                let ordersProperty = customerEntityType.property(withName: "Orders")
                let customers = try service.executeQuery(DataQuery()
                    .from(customersEntitySet).filter(customerIDProperty.equal("ALFKI")))
                    .entityList()
                let orders = try service.executeQuery(DataQuery()
                    .from(ordersEntitySet).top(2))
                    .entityList()
                let changes = ChangeSet()
                let newCustomer = customers.first().copyEntity()
                changes.createEntity(newCustomer)
                let firstOrder = orders.first().copyEntity()
                let secondOrder = orders.last().copyEntity()
                changes.createRelatedEntity(firstOrder, in: newCustomer,
                    property: ordersProperty)
                changes.createRelatedEntity(secondOrder, in: newCustomer,
                    property: ordersProperty)
                try service.applyChanges(changes)
            }
        }
    }
    

    Declaration

    Swift

    open func createRelatedEntity(_ entity: EntityValue, in parent: EntityValue, property: Property, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void

    Parameters

    entity

    Entity to be created.

    parent

    Previously created parent entity.

    property

    Parent’s navigation property.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • Add a pending deleted entity to the change set. The entity will be deleted when this change set is submitted.

    Declaration

    Swift

    open func deleteEntity(_ entity: EntityValue, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void

    Parameters

    entity

    Entity to be deleted.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • Add a pending deleted link to the change set. The link will be deleted when this change set is submitted.

    Declaration

    Swift

    open func deleteLink(from: EntityValue, property: Property, to: EntityValue = EntityValue.ofType(EntityType.undefined), headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void

    Parameters

    from

    Source entity for the link to be deleted.

    property

    Source navigation property for the link to be deleted.

    to

    Target entity for the link to be deleted.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • Example

    open func showChangedEntities(changes: ChangeSet) throws -> Void {
        let n = changes.size
        do {
            var i = 0
            var first_1081 : Bool = true
            while (true) {
                if first_1081 {
                    first_1081 = false
                } else {
                    i = i + 1
                }
                if !(i < n) { break }
                if changes.isEntity(at: i) {
                    let change = changes.entity(at: i)
                    if change.isCreated {
                        try Example.show("created entity")
                    }
                    else if change.isUpdated {
                        try Example.show("updated entity")
                    }
                    else if change.isDeleted {
                        try Example.show("deleted entity")
                    }
                }
            }
        }
    }
    

    Declaration

    Swift

    open func entity(at index: Int) -> EntityValue

    Parameters

    index

    From zero to size - 1.

    Return Value

    The changed entity, if isEntity(index) is true, otherwise throws undefined. The EntityValue.isCreated, EntityValue.isUpdated and EntityValue.isDeleted properties can be accessed on the resulting entity value to determine the type of change.

  • Declaration

    Swift

    open func headers(at index: Int) -> HTTPHeaders

    Parameters

    index

    From zero to size - 1.

    Return Value

    The HTTP headers for the change at index.

  • Add an action to the change set. If the method is not an action throws an undefined.

    See also

    DataQuery.bind, for setting the binding parameter for a bound action.

    Declaration

    Swift

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

    Parameters

    method

    Action to be called.

    parameters

    Method parameters.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • 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 change is for an action

  • Declaration

    Swift

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

    Parameters

    index

    From zero to size - 1.

    Return Value

    true if index is a valid change index, and the change is for a created, updated or deleted entity; otherwise false.

  • Declaration

    Swift

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

    Parameters

    index

    From zero to size - 1.

    Return Value

    true if index is a valid change index, and the change is for a created, updated or deleted link; otherwise false.

  • Example

    open func showChangedLinks(changes: ChangeSet) throws -> Void {
        let n = changes.size
        do {
            var i = 0
            var first_1105 : Bool = true
            while (true) {
                if first_1105 {
                    first_1105 = false
                } else {
                    i = i + 1
                }
                if !(i < n) { break }
                if changes.isLink(at: i) {
                    let change = changes.link(at: i)
                    if change.isCreated {
                        try Example.show("created link")
                    }
                    else if change.isUpdated {
                        try Example.show("updated link")
                    }
                    else if change.isDeleted {
                        try Example.show("deleted link")
                    }
                }
            }
        }
    }
    

    Declaration

    Swift

    open func link(at index: Int) -> ChangedLink

    Parameters

    index

    From zero to size - 1.

    Return Value

    The changed link, if isLink(index) is true, otherwise throws undefined. The ChangedLink.isCreated, ChangedLink.isUpdated and ChangedLink.isDeleted properties can be accessed on the resulting changed link to determine the type of change.

  • Declaration

    Swift

    open func options(at index: Int) -> RequestOptions

    Parameters

    index

    From zero to size - 1.

    Return Value

    The request options for the change at index.

  • Call createEntity, if entity.isNew == true, otherwise call updateEntity.

    Declaration

    Swift

    open func saveEntity(_ entity: EntityValue, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void

    Parameters

    entity

    Entity to be created or updated.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • The number of changes in this change set.

    Declaration

    Swift

    open var size: Int
  • Add an updated entity to the change set. The entity will be updated when this change set is submitted.

    Declaration

    Swift

    open func updateEntity(_ entity: EntityValue, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void

    Parameters

    entity

    Entity to be updated.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • Add a pending updated link to the change set. The link will be updated when this change set is submitted.

    Declaration

    Swift

    open func updateLink(from: EntityValue, property: Property, to: EntityValue, headers: HTTPHeaders = HTTPHeaders.empty, options: RequestOptions = RequestOptions.none) -> Void

    Parameters

    from

    Source entity for the link to be updated.

    property

    Source navigation property for the link to be updated. This must be a one-to-one navigation property.

    to

    Target entity for the link to be updated.

    headers

    Request-specific headers.

    options

    Request-specific options.