Fetch data from CloudKit to app when app is in background

Hello

I have an app that uses Core Data with CloudKit to store data. When I am in the app (using the app) and create some new data on an other device, the data is fetched in a few seconds and I see it immediately, however if I am not using the app and create some new data on an other device, I have to enter the app and then the data starts fetching, is there a way to fetch data even if I am not using the app.

Here is my core data stack:

import CoreData
import Combine

class PersistenceController {
    static let shared = PersistenceController()
    
    let container: NSPersistentCloudKitContainer
    
    init() {
        container = NSPersistentCloudKitContainer(name: "CoreData")
        
        guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_NAME")?.appendingPathComponent("CoreData.sqlite") else {
            fatalError("Shared file container could not be created.")
        }
        
        let storeDescription = NSPersistentStoreDescription(url: fileContainer)
        storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
        storeDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "Container_Identifier")

        container.persistentStoreDescriptions = [storeDescription]
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        
        container.viewContext.automaticallyMergesChangesFromParent = true
        container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
    }
}

Are you expecting immediate propagation when records are modified or eventual propagation? What is your 'expectation' for acceptable delay in the background? I'd have to imagine that data synchronization in the background is supported, but I don't remember any videos stating this explicitly. Does your app make use of any other background execution modes?

The way I achieve this is through CKSubscriptions and notifications which is pretty much immediate as long as the push volume isn't too high for a given device, however I'm not using the CloudContainer and rolling my own Core Data / CKRecord translation.

  1. Subscribe to record changes (insert, delete, update)
  2. Request remote notifications on UIApplication instance
  3. Implement the didReceiveRemoteNotification
  4. Depending if data was purged (too much data in push payload), run a CKFetchRecordsOperation or CKQueryOperation
Fetch data from CloudKit to app when app is in background
 
 
Q