How to enforce immediate Coredata & CloudKit sync?
Asked Answered
C

0

7

The setup:
My app uses either private data, i.e. data of a user identified by the iCloud login, or shared data, i.e. data of another user, shared via iCloud.
It uses thus 2 persistent stores, private and shared. Both are mirrored by Coredata & CloudKit synchronization.
When the user switches from private data to shared data and back, fetch requests and save requests are either directed to the private or the shared persistent store.

The use case:
Assume the user is using the private store. In this case, the shared iCloud database and thus the mirrored shared persistent store do not contain any data, since sharing is not activated.
He/she then receives an invitation of another user to share his/her data. When the current user accepts the invitation, the shared data of the other user are mapped to the shared iCloud database of the current user, and eventually mirrored to the shared persistent store.

The problem:
Unfortunately, mirroring of the shared data does not start immediately. Sometimes, it starts after a few seconds, sometimes only after the app is restarted.

The question:
How can I enforce immediate Coredata & CloudKit mirroring?
Apparently a NSPersistentCloudKitContainer does not have a function to trigger a re-sync manually. One suggestion I found in this post is to re-initialize the NSPersistentCloudKitContainer, which triggers a re-sync.
Is this the right way to proceed?

Crackleware answered 17/2, 2022 at 17:54 Comment(3)
Please see: https://mcmap.net/q/1629234/-coredata-not-updated-until-application-inactivatesJd
Thanks for pointing this out. Unfortunately, this solution is only available for macOS. I am developing for iOS.Folliculin
Have you tried to send corresponding UIApplicationWillEnterForegroundNotification or UIApplicationDidBecomeActiveNotification?Jd

© 2022 - 2024 — McMap. All rights reserved.