Does CKSyncEngine have to be re-initialized after an account change event?

According to this comment in the sample project on GitHub and this answer by Apple Staff, CKSyncEngine should be re-initialized after signing out or switching accounts so that "CKSyncEngine schedules a new fetch on init."

But according to my tests, CKSyncEngine will schedule a fetch after having a signed out and signed in again, without me ever having to reset the serialized sync state.

The documentation doesn't mentioned anywhere that CKSyncEngine should be re-initialized after an account change. In fact, it states that CKSyncEngine will reset its state internally on account changes.

So if that's the case, then I'm very confused as to why the "official" recommendation is to re-initialize CKSyncEngine after receiving .signOut or .accountSwitch.

Can someone please clarify the correct approach here?

Answered by DTS Engineer in 884061022

When an account change happens, the behavior of CKSyncEngine is documented here, which is quite clear that you don't have to create / initialize a new CKSyncEngine instance when handling the account change event. In case you appropriately clean up your local cache, reusing the existing CKSyncEngine instance is fine, although creating a new instance is nothing wrong.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

When an account change happens, the behavior of CKSyncEngine is documented here, which is quite clear that you don't have to create / initialize a new CKSyncEngine instance when handling the account change event. In case you appropriately clean up your local cache, reusing the existing CKSyncEngine instance is fine, although creating a new instance is nothing wrong.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Does CKSyncEngine have to be re-initialized after an account change event?
 
 
Q