ADCrossReference

public struct ADCrossReference<T: ADDataTable>: ADDataCrossReference

Creates and maintains a one-to-many or many-to-many cross reference relationship between two ADDataTable instances that can be stored in or read from a ADDataProvider.

Example:

The following creates a relationship between the Group and Person tables (both conforming to ADDataTable) on the Group.people property:

import Foundation
import ActionData

class Group: ADDataTable {

    static var tableName = "Groups"
    static var primaryKey = "id"
    static var primaryKeyType: ADDataTableKeyType = .computedInt

    var id = ADSQLiteProvider.shared.makeID(Group.self) as! Int
    var name = ""
    var people = ADCrossReference<Person>(name: "PeopleInGroup", leftKeyName: "groupID", rightKeyName: "personID")

    required init() {

    }
}

Instance of the Person table stored in people will be cross referenced in the PeopleInGroup table where groupID is the Group instance’s id and personID is the Person instances id.

Warning

ADCrossReference was meant to work with a small number of relationships only, because all cross referenced items are always read into memory when the parent object is loaded. Care should be taken to keep from overflowing memory.
  • The name of the cross reference table.

    Declaration

    Swift

    public var crossReferenceName: String = ""
  • The name of the left-side key used to store the primary key of the ADDataTable on the left side of the relationship.

    Declaration

    Swift

    public var leftKeyName: String = "leftID"
  • The name of the right-side key used to store the primary key of the ADDataTable on the right side of the relationship.

    Declaration

    Swift

    public var rightKeyName: String = "rightID"
  • Stores all child ADDataTable instances that are a part of the cross reference relationship to the left-side primary key value.

    Declaration

    Swift

    public var storage: [T] = []
  • Provides quick access to the items in the storage property.

    Declaration

    Swift

    public subscript(_ index: Int) -> T
  • Returns the number of ADDataTable instances in the cross reference.

    Declaration

    Swift

    public var count: Int
  • Creates a new instance of the ADCrossReference with the given table name and key names.

    Example

    var people = ADCrossReference<Person>(name: "PeopleInGroup", leftKeyName: "groupID", rightKeyName: "personID")
    

    Declaration

    Swift

    public init(name: String, leftKeyName: String = "leftID", rightKeyName: String = "rightID")

    Parameters

    name

    The name of the cross reference table.

    leftKeyName

    The name of the left-side key used to store the primary key of the ADDataTable on the left side of the relationship. The default value is leftID.

    rightKeyName

    The name of the right-side key used to store the primary key of the ADDataTable on the right side of the relationship. The default value is rightID.

  • Adds the given ADDataTable instance to the cross reference’s storage.

    Declaration

    Swift

    public mutating func append(_ item: T)

    Parameters

    item

    The ADDataTable instance to add.

  • Ensures the given cross reference table exists in the given data provider and left-side key value and creates it if required.

    Declaration

    Swift

    @discardableResult public func ensureCrossReferenceExists(in provider: ADDataProvider, forKeyValue leftKey: Any) throws -> Bool

    Parameters

    provider

    The ADDataProvider to see if the cross reference exists in.

    leftKey

    The left-side key value to build the cross reference on.

    Return Value

    true if the table exists or was successfully created, else returns false.

  • Removes all cross references for the given key value from the given data provider.

    Declaration

    Swift

    public func delete(from provider: ADDataProvider, forKeyValue key:Any) throws

    Parameters

    provider

    The ADDataProvider to remove the cross references from.

    key

    The left-side key value to delete the values for.

  • Saves the given cross references to the data provider for the give key value. The cross reference table will automatically be created if it doesn’t already exist.

    Declaration

    Swift

    public func save(to provider: ADDataProvider, forKeyValue leftKey: Any) throws

    Parameters

    provider

    The provider to save values to the data source.

    leftKey

    The left-side key value to create the cross reference on.

  • Loads all cross referenced ADDataTable instances for the given cross reference based on the given key value.

    Declaration

    Swift

    public mutating func load(from provider: ADDataProvider, forKeyValue key:Any) throws

    Parameters

    provider

    The ADDataProvider to load the ADDataTable instances from.

    key

    The left-side key value to load the cross reference on.