Comments (5)
Hi @aehlke,
Good question, and I think there may be a race condition in the design of the module in "normal" use.
Basically, CloudKitSyncMonitor is usually accessed as a singleton (SyncMonitor.shared
), but the SyncMonitor instance won't be initialized until that singleton is accessed. So, if CloudKitSyncMonitor is used in a view (as will usually be the case), it's possible that the view won't be initialized before NSPersistentCloudKitContainer starts sending messages, so it could miss a message.
In my code, I use a view model, which is initialized very early in the startup process (I forget where and I've paused my Apple development for the time being so I can't look it up), so I didn't run into problems.
If there is an issue caused by missing early messages, then you can try putting let _ = SyncMonitor.shared
before NSPersistentCloudKitContainer is set up (e.g. in the app delegate) and see if that fixes it. That should cause SyncMonitor to subscribe to the notifications before NSPersistentCloudKitContainer can start doing anything.
If you try this, please post your findings here. :)
from cloudkitsyncmonitor.
Hi @thatvirtualboy ,
setupState
is set to.succeeded(started: startDate, ended: endDate)
whenNSPersistentCloudKitContainer
sends aSyncEvent
whose type is.setup
and whosestartDate
andendDate
properties are notnil
(which in theory means the setup has ended, therefore setup has succeeded). Seeinternal func setProperties(from event: SyncEvent)
. IfsetupState
is showing.notStarted
, it means thatCloudKitSyncMonitor
didn't receive one or more events fromNSPersistentCloudKitContainer
. You might have a race condition in whichNSPersistentCloudKitContainer
is firing events before your app is detecting them withCloudKitSyncMonitor
. (NSPersistentCloudKitContainer
publishes a stream of events (of type SyncEvent) telling your app what it's doing.CloudKitSyncMonitor
listens to those events and turns them into state variables you can check and display; if it misses events, it won't be able to update the variables). You may just need to make sure to set upCloudKitSyncMonitor
beforeNSPersistentCloudKitContainer
, although it's been so long since I've done that that I can't remember how.CKERRORDOMAIN ERROR 2
is a network error - the device can't connect to the network (or, really, it is running into some network-like error when CloudKit is trying to export data). Export errors like this are why I wroteCloudKitSyncMonitor
, as iCloud is the "source of truth" for synced data, and the usual fix for errors like this, if it's not something simple like they're not on the Internet or a firewall is blocking outgoing traffic, is to delete and reinstall the app. Yes, that means the 6 items that are not exporting from the iPhone (assuming the iPhone is what's showing the export error) will be lost. I'll pause a moment while you gasp and go through the stages of grief. Your user might be able to go into Settings and turn iCloud Sync off and back on for your app, but I'm pretty sure the local data will be deleted anyway, not merged. WithNSPersistentCloudKitContainer
, it's critical that data make it up to iCloud, or it doesn't really exist. However, I also haven't run into random export errors like that since iOS 14 (15?) or so, so make sure your users are on new OSes.NSPersistentCloudKitContainer
was very buggy at first, having frequent problems with items not syncing; detecting that export error quickly was critical to avoiding data loss. I assume your users are on something newer than iOS 14, but you never know, and it makes a big difference in stability.
from cloudkitsyncmonitor.
@ggruen Thanks Grant so much for the detailed explanation on both items! This is very helpful.
Unfortunately the export errors are still present on current iOS builds, though seemingly less frequent.
At any rate, I really appreciate the reply. Thank you!
from cloudkitsyncmonitor.
You may just need to make sure to set up CloudKitSyncMonitor before NSPersistentCloudKitContainer,
Does this mean that instead of just having an ObservedObject in a view, the sync monitor should also be initialized somewhere in the app delegate before it's actually later used by a view that the user navigates to? The docs don't suggest triggering some behavior before its use in a view. Thanks
from cloudkitsyncmonitor.
That's what I ended up doing, thanks!
from cloudkitsyncmonitor.
Related Issues (12)
- CKContainer.default().accountStatus does not get callback HOT 4
- Can I used the class from Objective-C code ? HOT 5
- Xcode 13 compatibility - Needs import CloudKit HOT 3
- Unrecognized AccountStatus error? HOT 12
- Use without SwiftUI context
- Missing LICENSE HOT 1
- "syncStateSummary" not `@Published` HOT 2
- Status stuck on โSyncingโ in iOS 17 HOT 5
- 'LocalizationValue' is only available in Mac Catalyst 15 or newer HOT 1
- Working with SwiftData? HOT 3
- Use of `@MainActor` throwing warnings when initializing outside of Swift concurrency
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cloudkitsyncmonitor.