CleanInsightsSDK Documentation Beta

Class Store

open class Store: NSObject, Codable

The store holds the user's consents to the different Features and Campaigns, and their Visit and Event measurements.

If you want to implement your own persistence of the store (e.g. because you want to write it in a database instead of the file system) and your own implementation of the transmission to the Matomo/CIMP backend (e.g. because you want to tunnel the requests through a proxy or add your own encryption layer), then create a subclass of this class and and implement the #init, #persist and #send methods.

If you only want to change either one or the other, you can use DefaultStore as a base and work from there.

Store Store NSObject NSObject Store->NSObject Codable Codable Store->Codable DefaultStore DefaultStore DefaultStore->Store

Subclasses

DefaultStore

Default implementation of a store. Stores the data in plist format in a given folder. If no folder is given, defaults to the app's Application Support directory on MacOS or its Documents directory elsewhere.

Conforms To

Codable
NSObject

Initializers

init(_:​_:​)

public init(_ args: [String: Any] = [:], _ debug: ((_ message: String) -> Void)? = nil)

Parameters

args [String:​ Any]

Optional arguments your implementation might need for loading the store.

debug ((_ message:​ String) -> Void)?

Optional function to output debug messages.

Properties

consents

var consents

visits

var visits

events

var events

Methods

load(_:​)

public func load(_ args: [String: Any]) -> Store?

This method gets called from the constructor and will receive the same parameters, as the constructor got.

Parameters

args [String:​ Any]

Arguments your implementation might need for loading the store.

Returns

Deserialized store data.

persist(_:​_:​)

public func persist(_ async: Bool, _ done: @escaping (_ error: Error?) -> Void)

This method gets called, when the SDK is of the opinion, that persisting the Store is in order.

This is partly controlled by the Configuration.persistEveryNTimes configuration option, and by calls to CleanInsights#persist.

If possible, try to honor the async flag: If it's true, it is set so, because the SDK wants to reduce impact on user responsitivity as much as possible. If it's false, the SDK wants to make sure, that the call finishes before the app gets killed.

Also make sure to use Cocoa's DispatchQueue features to not run into race conditions. See the DefaultStore implementation on how to do this.

Parameters

async Bool

Indicates, if the persistence should be done asynchronously or synchronously. E.g. a persist call during the exit of an app should be done synchronously, otherwise the operation might never get finished because the OS kills the app too early.

done @escaping (_ error:​ Error?) -> Void

Callback, when the operation is finished, either successfully or not. Be aware that this might get called on different threads/queues.

error

If no error is returned, the operation is considered successful and the internal counter will be set back again.

send(_:​_:​_:​_:​)

public func send(_ data: Data, _ server: URL, _ timeout: TimeInterval, _ done: @escaping (_ error: Error?) -> Void)

This method gets called, when the SDK gathered enough data for a time period and is ready to send the data to a CIMP (CleanInsights Matomo Proxy).

Parameters

data Data

The serialized JSON for a POST request to a CIMP.

server URL

The server URL from Configuration.server.

timeout Time​Interval

The timeout in seconds from Configuration.timeout.

done @escaping (_ error:​ Error?) -> Void

Callback, when the operation is finished, either successfully or not. Be aware that this might get called on different threads/queues.

error

If no error is returned, the data sent will be removed from the store and the store persisted.