DataStore

open class DataStore<T> where T : NSObject, T : Persistable
extension DataStore: Hashable

Class to interact with a specific collection in the backend.

  • Undocumented

    Declaration

    Swift

    public typealias ArrayCompletionHandler = ([T]?, Swift.Error?) -> Void
  • Undocumented

    Declaration

    Swift

    public typealias ObjectCompletionHandler = (T?, Swift.Error?) -> Void
  • Undocumented

    Declaration

    Swift

    public typealias IntCompletionHandler = (Int?, Swift.Error?) -> Void
  • Undocumented

    Declaration

    Swift

    public typealias UIntErrorTypeArrayCompletionHandler = (UInt?, [Swift.Error]?) -> Void
  • Undocumented

    Declaration

    Swift

    public typealias UIntArrayCompletionHandler = (UInt?, [T]?, [Swift.Error]?) -> Void
  • Collection name that matches with the name in the backend.

    Declaration

    Swift

    public let collectionName: String
  • Client instance attached to the DataStore.

    Declaration

    Swift

    public let client: Client
  • DataStoreType defines how the DataStore will behave.

    Declaration

    Swift

    public let type: StoreType
  • Undocumented

    Declaration

    Swift

    public let validationStrategy: ValidationStrategy?
  • Undocumented

    Declaration

    Swift

    open var deltaSet: Bool { get }
  • Undocumented

    Declaration

    Swift

    open var options: Options?
  • ttl

    TTL (Time to Live) defines a filter of how old the data returned from the DataStore can be.

    Declaration

    Swift

    open var ttl: TTL? { get set }
  • Factory method that returns a DataStore.

    Declaration

    Swift

    @available(*, deprecated, message: "Deprecated in version 3.21.0. Please use `collection(type:autoPagination:tag:validationStrategy:options:﹚` instead")
    open class func collection(
        _ type: StoreType = .cache,
        autoPagination: Bool = false,
        tag: String = defaultTag,
        validationStrategy: ValidationStrategy? = nil,
        options: Options? = nil
    ) throws -> DataStore

    Parameters

    type

    defines the data store type which will define the behavior of the DataStore. Default value: .cache

    deltaSet

    Enables delta set cache which will increase performance and reduce data consumption. Default value: false

    client

    define the Client to be used for all the requests for the DataStore that will be returned. Default value: Kinvey.sharedClient

    tag

    A tag/nickname for your DataStore which will cache instances with the same tag name. Default value: Kinvey.defaultTag

    validationStrategy

    (Optional) Defines a strategy to validate results upfront. Default value: nil

    Return Value

    An instance of DataStore which can be a new instance or a cached instance if you are passing a tag parameter.

  • Factory method that returns a DataStore.

    Declaration

    Swift

    open class func collection(
        type: StoreType,
        autoPagination: Bool = false,
        tag: String = defaultTag,
        validationStrategy: ValidationStrategy? = nil,
        options: Options? = nil
    ) throws -> DataStore

    Parameters

    type

    defines the data store type which will define the behavior of the DataStore. Default value: .auto

    deltaSet

    Enables delta set cache which will increase performance and reduce data consumption. Default value: false

    client

    define the Client to be used for all the requests for the DataStore that will be returned. Default value: Kinvey.sharedClient

    tag

    A tag/nickname for your DataStore which will cache instances with the same tag name. Default value: Kinvey.defaultTag

    validationStrategy

    (Optional) Defines a strategy to validate results upfront. Default value: nil

    Return Value

    An instance of DataStore which can be a new instance or a cached instance if you are passing a tag parameter.

  • Factory method that returns a new instance of a DataStore copying all the current configuration but for a new type.

    Declaration

    Swift

    open func collection<NewType: Persistable>(
        newType: NewType.Type
    ) throws -> DataStore<NewType> where NewType: NSObject

    Parameters

    newType

    Type for the new DataStore instance

    Return Value

    A new DataStore instance for the type specified

  • Gets a single record using the _id of the record.

    Declaration

    Swift

    @discardableResult
    open func find(
        _ id: String,
        options: Options? = nil,
        completionHandler: ((Swift.Result<T, Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<T, Swift.Error>>

    Parameters

    id

    The _id value of the entity to be find

    completionHandler

    Completion handler to be called once the respose returns

    Return Value

    A AnyRequest instance which will allow cancel the request later

  • Gets a list of records that matches with the query passed by parameter.

    Declaration

    Swift

    @discardableResult
    open func find(
        _ query: Query = Query(),
        options: Options? = nil,
        completionHandler: ((Swift.Result<AnyRandomAccessCollection<T>, Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<AnyRandomAccessCollection<T>, Swift.Error>>

    Parameters

    query

    The query used to filter the results

    deltaSet

    Enforces delta set cache otherwise use the client’s deltaSet value. Default value: false

    readPolicy

    Enforces a different ReadPolicy otherwise use the client’s ReadPolicy. Default value: nil

    completionHandler

    Completion handler to be called once the respose returns

    Return Value

    A Request instance which will allow cancel the request later

  • Gets a count of how many records that matches with the (optional) query passed by parameter.

    Declaration

    Swift

    @discardableResult
    open func count(
        _ query: Query? = nil,
        options: Options? = nil,
        completionHandler: ((Swift.Result<Int, Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<Int, Swift.Error>>

    Parameters

    query

    The query used to filter the results

    readPolicy

    Enforces a different ReadPolicy otherwise use the client’s ReadPolicy. Default value: nil

    completionHandler

    Completion handler to be called once the respose returns

    Return Value

    A Request instance which will allow cancel the request later

  • Undocumented

    Declaration

    Swift

    @discardableResult
    open func group(
        keys: [String]? = nil,
        initialObject: JsonDictionary,
        reduceJSFunction: String,
        condition: NSPredicate? = nil,
        options: Options? = nil,
        completionHandler: ((Swift.Result<[AggregationCustomResult<T>], Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<[AggregationCustomResult<T>], Swift.Error>>
  • Undocumented

    Declaration

    Swift

    @discardableResult
    open func group<Count: CountType>(
        count keys: [String],
        countType: Count.Type? = nil,
        condition: NSPredicate? = nil,
        options: Options? = nil,
        completionHandler: @escaping (Swift.Result<[AggregationCountResult<T, Count>], Swift.Error>) -> Void
    ) -> AnyRequest<Swift.Result<[AggregationCountResult<T, Count>], Swift.Error>>
  • Undocumented

    Declaration

    Swift

    @discardableResult
    open func group<Sum: AddableType>(
        keys: [String],
        sum: String,
        sumType: Sum.Type? = nil,
        condition: NSPredicate? = nil,
        options: Options? = nil,
        completionHandler: @escaping (Swift.Result<[AggregationSumResult<T, Sum>], Swift.Error>) -> Void
    ) -> AnyRequest<Swift.Result<[AggregationSumResult<T, Sum>], Swift.Error>>
  • Undocumented

    Declaration

    Swift

    @discardableResult
    open func group<Avg: AddableType>(
        keys: [String],
        avg: String,
        avgType: Avg.Type? = nil,
        condition: NSPredicate? = nil,
        options: Options? = nil,
        completionHandler: @escaping (Swift.Result<[AggregationAvgResult<T, Avg>], Swift.Error>) -> Void
    ) -> AnyRequest<Swift.Result<[AggregationAvgResult<T, Avg>], Swift.Error>>
  • Undocumented

    Declaration

    Swift

    @discardableResult
    open func group<Min: MinMaxType>(
        keys: [String],
        min: String,
        minType: Min.Type? = nil,
        condition: NSPredicate? = nil,
        options: Options? = nil,
        completionHandler: @escaping (Swift.Result<[AggregationMinResult<T, Min>], Swift.Error>) -> Void
    ) -> AnyRequest<Swift.Result<[AggregationMinResult<T, Min>], Swift.Error>>
  • Undocumented

    Declaration

    Swift

    @discardableResult
    open func group<Max: MinMaxType>(
        keys: [String],
        max: String,
        maxType: Max.Type? = nil,
        condition: NSPredicate? = nil,
        options: Options? = nil,
        completionHandler: @escaping (Swift.Result<[AggregationMaxResult<T, Max>], Swift.Error>) -> Void
    ) -> AnyRequest<Swift.Result<[AggregationMaxResult<T, Max>], Swift.Error>>
  • Creates or updates a record.

    Declaration

    Swift

    @discardableResult
    open func save(
        _ persistable: T,
        options: Options? = nil,
        completionHandler: ((Swift.Result<T, Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<T, Swift.Error>>
  • Creates or updates a record.

    Declaration

    Swift

    @discardableResult
    open func save<C: RandomAccessCollection>(
        _ persistable: C,
        options: Options? = nil,
        completionHandler: ((Swift.Result<MultiSaveResultTuple<T>, Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<MultiSaveResultTuple<T>, Swift.Error>> where C.Element == T
  • Deletes a record.

    Declaration

    Swift

    @discardableResult
    open func remove(
        _ persistable: T,
        options: Options? = nil,
        completionHandler: ((Swift.Result<Int, Swift.Error>) -> Void)? = nil
    ) throws -> AnyRequest<Swift.Result<Int, Swift.Error>>
  • Deletes a list of records.

    Declaration

    Swift

    @discardableResult
    open func remove<S: Sequence>(
        _ array: S,
        options: Options? = nil,
        completionHandler: ((Swift.Result<Int, Swift.Error>) -> Void)?
    ) -> AnyRequest<Swift.Result<Int, Swift.Error>> where S.Element == T
  • Deletes a record using the _id of the record.

    Declaration

    Swift

    @discardableResult
    open func remove(
        byId id: String,
        options: Options? = nil,
        completionHandler: ((Swift.Result<Int, Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<Int, Swift.Error>>
  • Deletes a list of records using the _id of the records.

    Declaration

    Swift

    @discardableResult
    open func remove(
        byIds ids: [String],
        options: Options? = nil,
        completionHandler: ((Swift.Result<Int, Swift.Error>) -> Void)?
    ) -> AnyRequest<Swift.Result<Int, Swift.Error>>
  • Deletes a list of records that matches with the query passed by parameter.

    Declaration

    Swift

    @discardableResult
    open func remove(
        _ query: Query = Query(),
        options: Options? = nil,
        completionHandler: ((Swift.Result<Int, Swift.Error>) -> Void)?
    ) -> AnyRequest<Swift.Result<Int, Swift.Error>>
  • Deletes all the records.

    Declaration

    Swift

    @discardableResult
    open func removeAll(
        options: Options? = nil,
        completionHandler: ((Swift.Result<Int, Swift.Error>) -> Void)?
    ) -> AnyRequest<Swift.Result<Int, Swift.Error>>
  • Sends to the backend all the pending records in the local cache.

    Declaration

    Swift

    @available(*, deprecated, message: "Deprecated in version 3.17.0. Please use DataStore.push(options:completionHandler:﹚ instead")
    @discardableResult
    open func push(
        timeout: TimeInterval? = nil,
        completionHandler: UIntErrorTypeArrayCompletionHandler? = nil
    ) -> AnyRequest<Swift.Result<UInt, MultipleErrors>>
  • Sends to the backend all the pending records in the local cache.

    Declaration

    Swift

    @available(*, deprecated, message: "Deprecated in version 3.17.0. Please use DataStore.push(options:completionHandler:﹚ instead")
    @discardableResult
    open func push(
        timeout: TimeInterval? = nil,
        completionHandler: ((Swift.Result<UInt, MultipleErrors>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<UInt, MultipleErrors>>
  • Sends to the backend all the pending records in the local cache.

    Declaration

    Swift

    @discardableResult
    open func push(
        options: Options? = nil,
        completionHandler: ((Swift.Result<UInt, MultipleErrors>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<UInt, MultipleErrors>>
  • Gets the records from the backend that matches with the query passed by parameter and saves locally in the local cache.

    Declaration

    Swift

    @available(*, deprecated, message: "Deprecated in version 3.17.0. Please use DataStore.pull(_:deltaSetCompletionHandler:options:completionHandler:﹚ instead")
    @discardableResult
    open func pull(
        _ query: Query = Query(),
        deltaSetCompletionHandler: ((AnyRandomAccessCollection<T>, AnyRandomAccessCollection<T>) -> Void)? = nil,
        deltaSet: Bool? = nil,
        completionHandler: DataStore<T>.ArrayCompletionHandler? = nil
    ) -> AnyRequest<Swift.Result<AnyRandomAccessCollection<T>, Swift.Error>>
  • Gets the records from the backend that matches with the query passed by parameter and saves locally in the local cache.

    Declaration

    Swift

    @discardableResult
    open func pull(
        _ query: Query = Query(),
        deltaSetCompletionHandler: ((AnyRandomAccessCollection<T>, AnyRandomAccessCollection<T>) -> Void)? = nil,
        options: Options? = nil,
        completionHandler: ((Swift.Result<AnyRandomAccessCollection<T>, Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<AnyRandomAccessCollection<T>, Swift.Error>>
  • Returns the number of changes not synced yet.

    Declaration

    Swift

    @available(*, deprecated, message: "Deprecated in version 3.24.0. please use DataStore.pendingSyncCount(﹚ instead")
    open func syncCount() -> UInt
  • Returns the number of changes not synced yet.

    Declaration

    Swift

    open func pendingSyncCount() -> Int
  • Returns the changes not synced yet.

    Declaration

    Swift

    open func pendingSyncEntities() -> AnyRandomAccessCollection<PendingOperation>
  • Returns the changes not synced yet, but with the POST requests aggregated to use multi-insert.

    Declaration

    Swift

    open func pendingSyncOperations() -> AnyRandomAccessCollection<PendingOperation>
  • Undocumented

    Declaration

    Swift

    open func clearSync() -> Int
  • Calls push and then pull methods, so it sends all the pending records in the local cache and then gets the records from the backend and saves locally in the local cache.

    Declaration

    Swift

    @available(*, deprecated, message: "Deprecated in version 3.17.0. Please use DataStore.sync(_:deltaSetCompletionHandler:options:completionHandler:﹚ instead")
    @discardableResult
    open func sync(
        _ query: Query = Query(),
        deltaSetCompletionHandler: ((AnyRandomAccessCollection<T>, AnyRandomAccessCollection<T>) -> Void)? = nil,
        deltaSet: Bool? = nil,
        completionHandler: UIntArrayCompletionHandler? = nil
    ) -> AnyRequest<Swift.Result<(UInt, [T]), MultipleErrors>>
  • Calls push and then pull methods, so it sends all the pending records in the local cache and then gets the records from the backend and saves locally in the local cache.

    Declaration

    Swift

    @available(*, deprecated, message: "Deprecated in version 3.17.0. Please use DataStore.sync(_:deltaSetCompletionHandler:options:completionHandler:﹚ instead")
    @discardableResult
    open func sync(
        _ query: Query = Query(),
        deltaSetCompletionHandler: ((AnyRandomAccessCollection<T>, AnyRandomAccessCollection<T>) -> Void)? = nil,
        deltaSet: Bool? = nil,
        completionHandler: ((Swift.Result<(UInt, [T]), MultipleErrors>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<(UInt, [T]), MultipleErrors>>
  • Calls push and then pull methods, so it sends all the pending records in the local cache and then gets the records from the backend and saves locally in the local cache.

    Declaration

    Swift

    @discardableResult
    open func sync(
        _ query: Query = Query(),
        deltaSetCompletionHandler: ((AnyRandomAccessCollection<T>, AnyRandomAccessCollection<T>) -> Void)? = nil,
        options: Options? = nil,
        completionHandler: ((Swift.Result<(UInt, AnyRandomAccessCollection<T>), MultipleErrors>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<(UInt, AnyRandomAccessCollection<T>), MultipleErrors>>
  • Deletes all the pending changes in the local cache.

    Declaration

    Swift

    @available(*, deprecated, message: "Deprecated in version 3.17.0. Please use DataStore.purge(_:options:completionHandler:﹚ instead")
    @discardableResult
    open func purge(
        _ query: Query = Query(),
        completionHandler: DataStore<T>.IntCompletionHandler? = nil
    ) -> AnyRequest<Swift.Result<Int, Swift.Error>>
  • Deletes all the pending changes in the local cache.

    Declaration

    Swift

    @discardableResult
    open func purge(
        _ query: Query = Query(),
        options: Options? = nil,
        completionHandler: ((Swift.Result<Int, Swift.Error>) -> Void)? = nil
    ) -> AnyRequest<Swift.Result<Int, Swift.Error>>
  • Clear all data for all collections.

    Declaration

    Swift

    open class func clearCache(_ tag: String? = nil, client: Client = sharedClient)
  • Clear all data for the collection attached to the DataStore.

    Declaration

    Swift

    open func clearCache(query: Query? = nil, cascadeDelete: Bool = false)
  • Subscribe and start listening to changes in the collection

    Declaration

    Swift

    @discardableResult
    open func subscribe(
        options: Options? = nil,
        subscription: @escaping () -> Void,
        onNext: @escaping (T) -> Void,
        onStatus: @escaping (RealtimeStatus) -> Void,
        onError: @escaping (Swift.Error) -> Void
    ) -> AnyRequest<Swift.Result<Void, Swift.Error>>
  • Unsubscribe and stop listening changes in the collection

    Declaration

    Swift

    @discardableResult
    open func unsubscribe(
        options: Options? = nil,
        completionHandler: @escaping (Swift.Result<Void, Swift.Error>) -> Void
    ) -> AnyRequest<Swift.Result<Void, Swift.Error>>
  • Undocumented

    Declaration

    Swift

    public func observe(_ query: Query? = nil, completionHandler: @escaping (CollectionChange<AnyRandomAccessCollection<T>>) -> Void) -> AnyNotificationToken?
  • Declaration

    Swift

    public func hash(into hasher: inout Hasher)
  • Declaration

    Swift

    public static func == (lhs: DataStore<T>, rhs: DataStore<T>) -> Bool