NSPersistentCloudKitContainer doesn't sync records on iCloud until switching the app to background/foreground

Originator:mshibanami
Number:rdar://FB9094758 Date Originated:May 2, 2021 at 9:04 PM
Status:Open Resolved:No
Product:macOS Product Version:macOS Big Sur 11.3.1 (20E241)
Classification:Core Data API Reproducible:Always
 
Basic Information

Please provide a descriptive title for your feedback:
NSPersistentCloudKitContainer doesn't sync records on iCloud until switching the app to background/foreground

Which area are you seeing an issue with?
Core Data API

What type of issue are you reporting?
Incorrect/Unexpected Behavior

Description
Please describe the issue and what steps we can take to reproduce it:

## Environment

- macOS Big Sur 11.3 (20E232)
- Xcode 12.5 (12E262)

## Description of the problem

`NSPersistentCloudKitContainer` doesn't send newly created/modified/removed records to iCloud immediately. It only syncs:

- When the persistent container was loaded
- When the app was switched to background
- When the app was switched to foreground

To demonstrate this issue, I attached 2 videos:

- One is with CloudKit Dashboard
- Another one is with multiple Macs

Also, I attached my source code (RedirectWebForSafari.zip) that was used for the above videos.
The code creating NSPersistentCloudKitContainer is here:
`RedirectWebForSafari/RedirectWebForSafari/Modules/Database/AnyCoreDataRepository.swift`

Hope these will be helpful.

## step-by-step set of instructions to reproduce the problem

1. Create `NSPersitentCloudKitContainer` object that can sync records via iCloud
2. Add a record with the container's `viewContext` and do `try viewContext.save()`

## Expected Result

The added record is uploaded to the iCloud and I can find it on CloudKit Dashboard.

## Actual Result

The added record is not uploaded to the iCloud and I can't find it on CloudKit Dashboard.
To upload it, I need to change the app to background/foreground, or quit & reopen the app. 




By the way, I found another person who got this issue, on Twitter: https://twitter.com/simonemontalto/status/1286659669046505472?s=21

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!