I am struggling to understand what is the idea of keyValueStore
in the ParseSwift initializer. These are my first steps with Parse and this SDK also as I decided to not use Firebase at the end.
Starting with simple init in AppDelegate:
ParseSwift.initialize(applicationId: "...",
clientKey: "...",
serverURL: URL(string: "https://...")!)
For the learning purpose I created user struct:
struct ParseUsr: ParseUser {
//: These are required for `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: These are required for `ParseUser`.
var username: String?
var email: String?
var password: String?
var authData: [String: [String: String]?]?
}
then when my app launch I am checking if current user is nil, for simplicity, just print it:
print(ParseUsr.current)
but upon retrieving there is a nil on line 46, ParseStorage.swift -> "Unexpectedly found nil while implicitly unwrapping an Optional value" for backingStore
later I tried also to add keyValueStore in the initialiser:
ParseSwift.initialize(applicationId: "...",
clientKey: "...",
serverURL: URL(string: "https://...")!, keyValueStore: KeyChain())
class for KeyChain is empty implementation so far, but that should not cause the above nil exception on backingStore
:
class KeyChain: ParseKeyValueStore {
func delete(valueFor key: String) throws {
}
func deleteAll() throws {
}
func get<T>(valueFor key: String) throws -> T? where T : Decodable {
return nil
}
func set<T>(_ object: T, for key: String) throws where T : Encodable {
}
}
Could you please give me a hint how to use the local persistence of current user?
Thank you!
The reason was something that I believe is called "code race":
I did call the initialiser in didFinishLaunchingWithOptions
and as it does asynchronous init, another code in the first viewController's viewDidLoad
was executing faster and therefore calling print(user.current)
before the Parse was initialised.
I moved Parse init as first in didFinishLaunchingWithOptions
and it is working fine now.