How to let iCloud sync across the devices without launching the app at midnight?

Hi, I have designed an app which needs to reschedule notifications according to the user's calendar at midnight. The function has been implemented successfully via backgroundtask. But since the app has enabled iCloud sync, some users will edit their calendar on their iPad and expect that the notifications will be sent promptly to them on iPhone without launching the app on their iPhone. But the problem is that if they haven't launched the app on their iPhone, iCloud sync won't happen. The notifications on their iPhone haven't been updated and will be sent wrongly. How can I design some codes to let iCloud sync across the devices without launching the app at midnight and then reschedule notifications?

Answered by DTS Engineer in 820578022

Thanks for sharing. SwiftData + CloudKit is based on NSPersistentCloudKitContainer as of today, which doesn't do any synchronization if the app is not launched.

Even if the app is running, it will be up to the system to decide when to synchronize data. There is no way for an app to force NSPersistentCloudKitContainer to synchronize with CloudKit.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Before I comment your question, would you mind to share the following information?

  • What iCloud API are you using?
  • What kind of data do you sync via the API?
  • Why do you need to send the notification from the iPhone, and not the iPad that changes the data?

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hi, Ziqiao, Thank you for your reply. 1.We just enabled iCloud sync in our app by following the guidance(https://www.hackingwithswift.com/quick-start/swiftdata/how-to-sync-swiftdata-with-icloud). 2.The app is similar with apple calendar. The app sync events data across the devices. 3.We have talked with some potential users. Their habits are to edit their agenda on iPad or Macbook because the screen is big enough. And then they will carry their iPhone to go outside and depend on the notifications from their iPhone to remind them. We can remind them to launch the app on their iPhone to sync the data from iCloud but sometimes they will forget it. That's why we want to design some codes to sync the events data from iCloud in the midnight without launching the app on iPhone.We have designed codes to reschedule the notifications in the midnight via backgroundtask. It works well. But we can't find the method to manually sync iCloud data via backgroundtask. Look forward to good solutions from Apple for it. Best Wishes,

Accepted Answer

Thanks for sharing. SwiftData + CloudKit is based on NSPersistentCloudKitContainer as of today, which doesn't do any synchronization if the app is not launched.

Even if the app is running, it will be up to the system to decide when to synchronize data. There is no way for an app to force NSPersistentCloudKitContainer to synchronize with CloudKit.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hi, Zhiqiao, Thank you for your reply. It seems that the only way is to remind the users to sync iCloud across the devices by launching the app. Hope there will be smarter solutions from Apple in future.

Best Wishes,

How to let iCloud sync across the devices without launching the app at midnight?
 
 
Q