Custom Options

Overview

Whenever you need to set a different value other then the default value you can use the Options struct to specify a new value for each request.

The Options struct contains a few values such as client, readPolicy, writePolicy, deltaSet and so on. To see the complete list of values, please check the OptionsReference Docs.

All the parameters in the Options struct are optional parameters, so only the properties that have a value must be set, otherwise nil value is set, which is the default value.

Some Examples

Here's some examples how to use Options in for some use cases.

Read Policy

Sets a custom read policy different the the policy defined by the DataStore type

let dataStore = DataStore<Book>.collection()
let options = Options(readPolicy: .forceNetwork)
dataStore.find(query, options: options) { (result: Result<[Book], Swift.Error>) in
    switch result {
    case .success(let books):
        print("change this line to handle succeed")
    case .failure(let error):
        print("change this line to handle failure")
    }
}

Write Policy

Sets a custom write policy different the the policy defined by the DataStore type

let dataStore = DataStore<Book>.collection()
let options = Options(writePolicy: .forceNetwork)
dataStore.save(book, options: options) { (result: Result<Book, Swift.Error>) in
    switch result {
    case .success(let book):
        print("change this line to handle succeed")
    case .failure(let error):
        print("change this line to handle failure")
    }
}

Client App Version and Custom Request Properties

Sets the value for the custom header X-Kinvey-Client-App-Version and X-Kinvey-Custom-Request-Properties respectively.

let dataStore = DataStore<Book>.collection()
let options = Options(
    clientAppVersion: "1.0.0",
    customRequestProperties: [
        "myCustomString" : "test",
        "myCustomNumber" : 1
    ]
)
dataStore.save(book, options: options) { (result: Result<Book, Swift.Error>) in
    switch result {
    case .success(let book):
        print("change this line to handle succeed")
    case .failure(let error):
        print("change this line to handle failure")
    }
}
Got a question?