2

Setup:

I have a watch app that is not independent, but cooperates with the companion iOS app.
Both use CoreData + CloudKit, and the size of the data stored in iCloud is very little (a few hundred records, where each record has 0.5 kB). The iOS app has no problems syncing CoreDate with iCloud.

Problem:

The watch app, however, is not able to download these few MB from iCloud after the app has been newly installed. Here is the log:

2022-08-18 11:21:47.373416+0200 Watch Extension[1000:1029376] [error] error: Unexpected background task assertion cancellation.
2022-08-18 11:21:47.375646+0200 Watch Extension[1000:1029376] [error] error: Unexpected background task assertion cancellation.
2022-08-18 11:21:47.375822+0200 Watch Extension[1000:1029376] [BackgroundTask] Background task still not ended after expiration handlers were called: <_UIBackgroundTaskInfo: 0x1761c370>: taskID = 45, taskName = CoreData: CloudKit Import, creationTime = 154599 (elapsed = 27). This app will likely be terminated by the system. Call UIApplication.endBackgroundTask(_:) to avoid this.
2022-08-18 11:21:47.381768+0200 Watch Extension[1000:1029376] [BackgroundTask] Background task still not ended after expiration handlers were called: <_UIBackgroundTaskInfo: 0x17525160>: taskID = 77, taskName = CoreData: CloudKit Export, creationTime = 154600 (elapsed = 26). This app will likely be terminated by the system. Call UIApplication.endBackgroundTask(_:) to avoid this.
2022-08-18 11:21:56.234513+0200 Watch Extension[1000:1029376] [BackgroundTask] Background Task 77 ("CoreData: CloudKit Export"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.
2022-08-18 11:21:56.234681+0200 Watch Extension[1000:1029376] [BackgroundTask] Background Task 45 ("CoreData: CloudKit Import"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.  

Obviously, watchOS is not able to download a few MB in half a minute. Worse, the sync is cancelled, and it is unclear when it is continued. The result is that the watch shows incomplete data, a very bad user experience.
I read here that others have a similar problem with slow iCloud sync on watchOS, but there is no solution.

Questions:

What could be the reason for this very slow sync? Can I speed it up? Any other suggestions?

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • `qualityOfService` is set to default when you don't assign a configuration. Please refer https://stackoverflow.com/a/73345485. `let configuration = CKOperation.Configuration() configuration.qualityOfService = .userInitiated queryOperation.configuration = configuration` – user1046037 Aug 19 '22 at 09:05
  • @user1046037 Is it possible to set a configuration for CoreData & CloudKit sync? I thought I can only configure `NSPersistentCloudKitContainerOptions`and I don't see a way to specify a `qualityOfService`. – Reinhard Männer Aug 19 '22 at 12:23
  • I wasn't aware you were using `NSPersistentCloudKitContainer`. Do you have a background mode and are you doing when app goes into background? If so check if the completion handler is called after you finish your background task. – user1046037 Aug 19 '22 at 16:07
  • 2. Under Capabilities have you enabled `Background mode` and checked `remote notifications`?. This is a required step. 3. Is this in debug mode or release mode? – user1046037 Aug 19 '22 at 16:15
  • 1
    Yes, both are enabled, and the app runs in debug mode. The problem is that the background task is not created by me, but by CoreData + CloudKit mirroring, i.e. by iOS. By now I contacted Apple (TSI), and they suggested: _We have reviewed your request and believe this issue would be best handled by the relevant engineering team directly, as it may require changes to the OS, tools, or the frameworks you are using. Please submit a complete bug._ I will post the result here. – Reinhard Männer Aug 20 '22 at 05:48
  • Also if possible test it on release mode just to be sure – user1046037 Aug 20 '22 at 07:09
  • Has there been any progress on this? Have you tried an independent app? – user1046037 Sep 08 '22 at 04:15
  • 1
    I submitted a bug report (ticket # FB11434074 - watchOS), and notified the TSI engineer of it, as requested. Up to now unfortunately no response... – Reinhard Männer Sep 09 '22 at 08:43
  • Fingers crossed, hope it gets fixed. IMHO having an independent app is the recommended approach, not sure if that is feasible / meaningful in your case. (Disclaimer I haven't done any apps for watchOS) – user1046037 Sep 09 '22 at 14:27
  • 1
    You are right. My app is dependent on the iPhone for historical reasons. It needs text input and editing, and that was not possible earlier. After your suggestion I realized that there is now a `WKInterfaceTextField` that allows it. I think I have to make it independent, although it is a lot of work. Anyway, if I get an answer from Apple, I will post it here. – Reinhard Männer Sep 10 '22 at 10:03
  • 1
    By now, my bug report has not been processed, and my TSI is closed. So there is no answer by Apple. I have to say, as expected. I have earlier submitted many bug reports, but nearly none of them have ever been answered, so it's not worth it. – Reinhard Männer Oct 19 '22 at 10:12
  • I can relate to that, my personal opinion is don't wait for it to be fixed, my hunch (I am no expert) it is better to build a standalone app (though more effort) if it makes sense for you as it would be more future proof. Especially because AFAIK companion apps are no longer the recommended way. – user1046037 Oct 19 '22 at 14:07
  • 1
    You are right. I am doing so, and it looks (not yet confirmed) that the stand-alone watch app doesn't have this problem. – Reinhard Männer Oct 19 '22 at 14:09
  • Hopefully SwiftUI can help you build faster and provide better and reusable code with the iPhone app. Good luck! You will find SwiftUI bugs along the way don't be disheartened ... things are getting better but seems to take time .. hopefully there will be new learnings along the way – user1046037 Oct 19 '22 at 14:12

0 Answers0