apple / sample-cloudkit-sync-engine

MIT License
170 stars 12 forks source link

CloudKit sync refresh problem when changes going from iOS to macOS #15

Open jessegrosjean opened 2 months ago

jessegrosjean commented 2 months ago

I am then testing sync by running this app in my MacBook and my iPhone. Generally sync is working, but the behavior is very different when syncing changes from macOS -> iOS compared to from iOS -> macOS:

  1. I open app on both my MacBook and iPhone
  2. I add item on my MacBook, it shows up a second later on iPhone
  3. On the other hand, if I add item on my iPhone it never shows up on my MacBook until I send app to background and then bring back to foreground.

Is this known/expected behavior? Any way to fix?

I have also tried calling try await self.database.syncEngine.fetchChanges(.init(scope: .all)) on the mac, but that doesn't seem to help. I must command-tab to another app, and then command tab back to the SyncEngine app to see changes updated in the Mac UI

jessegrosjean commented 2 months ago

Update It just started to work (so I see updates on my MacBook without having to put app into background and bring it forward). I didn't change anything that I can think of. It would be nice to know why this behavior changed.

jessegrosjean commented 2 months ago

And like that... back to old behavior, but worse. Now the Mac app UI doesn't update even when I put it into the background and bring it forward. I need to quit and reopen the Mac app to see changes. All the while the changes from Mac to iPhone are showing up instantly.

So summary in my testing (with no code or config changes) is:

  1. Occasional sync is instant between Mac and iPhone versions of this app.
  2. Often the macOS version updates only when deactivated and then reactivated.
  3. Sometimes the macOS version only updates after restart

This feels like a macOS bug somewhere... not a throttling of CloudKit. I say this because even when macOS version hasn't reflected changes from the iOS version... it can still instantly send its own changes.

For example right now the Mac version is being problematic... and not showing items that I've just created in my iOS version (unless I restart app). But even in this state if I create a new item (or delete existing) then that change is instantly shown on iOS version of app. It's just changes from iOS that are not showing on Mac.

Help me understand and fix! :)

marcomasser commented 2 months ago

Just a wild guess: Does it behave differently when you launch your app from Xcode vs. from Finder? Or do you see any differences between the CloudKit development environment vs. the production environment (see the docs here)?

jessegrosjean commented 2 months ago

@marcomasser Thanks for the idea, but I still see the same behavior in production environment.

Here’s a video of exactly what I’m seeing: https://vimeo.com/957328976?share=copy

Here’s code I’m using: https://github.com/jessegrosjean/sample-cloudkit-sync-engine

sjrmanning commented 1 month ago

Hi Jesse, when switching to production are you also changing your aps-environment and com.apple.developer.aps-environment entitlements to production? These entitlements are important for the push notifications CloudKit receives when changes occur on one device.

I would check that the macOS device is correctly able to receive push notifications. On the macOS device you can listen to the NSApplicationDelegate.application(_:didRegisterForRemoteNotificationsWithDeviceToken:) callback to confirm you're getting correctly registered and receiving a device token.