I have a quesiton on .accountChange
handler code in CKSyncEngine demo project. Below is the code in handleAccountChange():
if shouldDeleteLocalData {
try? self.deleteLocalData() // This error should be handled, but we'll skip that for brevity in this sample app.
}
if shouldReUploadLocalData {
let recordZoneChanges: [CKSyncEngine.PendingRecordZoneChange] = self.appData.contacts.values.map { .saveRecord($0.recordID) }
self.syncEngine.state.add(pendingDatabaseChanges: [ .saveZone(CKRecordZone(zoneName: Contact.zoneName)) ])
self.syncEngine.state.add(pendingRecordZoneChanges: recordZoneChanges)
}
IMHO, when user switches account, the most important thing is to reload data from the new account's document folder. However, I can't see this is done anywhere. In above code, if shouldDeleteLocalData
is false, self.appData
would still hold the previous account's local data. That seems very wrong. Am I missing something?
It would be best if iOS restarts all applications when user switches account. If that's not the case (I guess so, otherwise there is no point to handle .accountChange
in the app), I think application should implement an API to re-initialize itself.
EDIT: after looking at the code again, I realize that the following code makes sure shouldDeleteLocalData
is always true
when user switching accounts. So the code doesn't leak the previous account's data, though I still think it has an issue - it doesn't load the new account's data.
case .switchAccounts:
shouldDeleteLocalData = true
shouldReUploadLocalData = false
deleteLocalData
clears out the in-memory AppData
but also persists the cleared AppData
on disk (by calling try self.persistLocalData()
) — essentially starting fresh, with no local contacts in memory or persisted on disk.
The final line in deleteLocalData
, self.initializeSyncEngine()
, sets up a new CKSyncEngine
, importantly with stateSerialization: self.appData.stateSerialization
which is now fresh. As long as automaticallySync
is true, CKSyncEngine schedules an initial sync on init, and since we're starting fresh with this instance of CKSyncEngine, it will go and fetch everything from the server (i.e. all contacts saved under the new account) and handle them locally.