Search results for

NSPersistentCloudKitContainer

589 results found

Post

Replies

Boosts

Views

Activity

Reply to How to safely create root and branch objects in a custom zone?
First, I’d like to be clear about the behavior NSPersistentCloudKitContainer implements when you use it to share a managed object (NSManagedObject) so we are on the same page: When you save object A to the Core Data store associated with the private database, NSPersistentCloudKitContainer transforms the object to a CloudKit record A, and synchronizes it to the record zone 1 in the CloudKit private database, with zone ID being com.apple.coredata.cloudkit.zone. When you then use NSPersistentCloudKitContainer.share(_:to:completion:) to create a new share (CKShare) and share object A to another user, NSPersistentCloudKitContainer moves record A to a record zone 2, with zone ID being com.apple.coredata.cloudkit.share. . Note that zone 2 is still in the private database. What you call NSPersistentCloudKitContainer.share(_:to:completion:) with an unsaved managed object A. Core Data saves the object for you, and the associated record A will be saved to zone 2. Regarding Core Data relationsh
Jul ’24
Is History Tracking in Cloudkit shared database needed?
I’ve setup the Cloudkit persistent container with private and shared database (see code below). I’ve enabled NSPersistentHistoryTrackingKey to true also for .shared database. I’ve noticed in the example from Apple that the History Tracking is only enabled in .private but not for .shared. Questions: For a CloudKit setup to sync (a) between owners’ own devices (only private database), and (b) between multiple iCloud Users through .private and .shared databases, Do I need to enable history tracking for .shared database if I want to check the remote changes in the .shared database (or is the history tracking of the .private database of the owner also accessible in the .shared database)? ======================== let APP_BUNDLE_IDENTIFIER = Bundle.main.bundleIdentifier! let APP_GROUP_IDENTIFIER = group. + APP_BUNDLE_IDENTIFIER private func setupPersistentContainer(_ container: NSPersistentCloudKitContainer? = nil, isStartup: Bool = true) -> NSPersistentCloudKitContainer { let container = contai
1
0
729
Jul ’24
How to save to specific stores/configurations in Core Data
I'm currently syncing core data with the CloudKit private and public databases, as you can see in the code below, I'm saving the private database in the default configuration in Core Data and the public in a configuration called Public everything works fine when NSPersistentCloudKitContainer syncs, what I'm having an issue with is trying to save to the public data store PublicStore, for instance when I try to save with func createIconImage(imageName: String) it saves the image to the default store, not the PublicStore(Public configuration). What could I do to make the createIconImage() function save to the PublicStore sqlite database? class CoreDataManager: ObservableObject{ static let instance = CoreDataManager() private let queue = DispatchQueue(label: CoreDataManagerQueue) @AppStorage(UserDefaults.Keys.iCloudSyncKey) private var iCloudSync = false lazy var context: NSManagedObjectContext = { return container.viewContext }() lazy var container: NSPersistentContainer = { return setupContainer() }()
1
0
720
Jul ’24
Reply to Tokenised text search in SwiftData help
I’ve considered turning textString into a to-many relationship by creating a Token entity but I’m unsure whether this will significantly increase overhead? I expect my database to grow very large unless I apply unique constraints on tokens and handle the CloudKit syncing myself (not using NSPersistentCloudKitContainer). Also in my app if many results are fetched (between 1000 to 100,000 matches) from the predicate I will need to perform an additional filter that can only be done in-memory, in this scenario would I need to perform both the predicate and filter in batches?
Topic: Community SubTopic: Apple Developers Tags:
Jul ’24
Reply to Trigger data transfer from watchOS when connectivity is restored
There isn't, and I see that an as-designed behavior. When a file is queued for a Watch Connectivity transfer, or a change is queued for a Core Data + CloudKit export, it is up to the system to decide when the operation should happen. That is for optimizing the overall system performance, and there is no way for developers to change the behavior. This is documented in the following API reference and technote respectively: transferFile(_:metadata:): The system attempts to send files as quickly as possible but may throttle delivery speeds to accommodate performance and power concerns. TN3164: Debugging the synchronization of NSPersistentCloudKitContainer The goal of implementing such a mechanism is to balance the use of system resources and achieve the best overall user experience on the devices. There is no API for apps to configure the timing for the synchronization. So even you have your watchOS app run in the background (for a period of time) using something like background workout processing or Hea
Topic: App & System Services SubTopic: Core OS Tags:
Jul ’24
Trigger data transfer from watchOS when connectivity is restored
Hello, I have an iOS app and a companion watchOS app. Users record a workout on Apple Watch, the data for which is then transferred using both Watch Connectivity and Core Data + CloudKit (NSPersistentCloudKitContainer) to their iPhone, where it is processed and displayed. As users are recording the workout on their Apple Watch, when they finish and the transfer begins, their iPhone is often not reachable to immediately send the data using Watch Connectivity and they have no network connection (cellular or Wi-Fi). With Watch Connectivity I use transferFile from WCSession, which queues the file for transfer. With Core Data + Cloudkit I save the data and the export is queued. An undetermined amount of time may pass until the user returns to their iPhone or connects to Wi-Fi and most of the time neither of the transfer methods actually transfers the data until the user opens the watchOS app into the foreground, at which point the transfer happens immediately for both methods. I've tried a number of thing
1
0
986
Jun ’24
Reply to Can the NSPersistentCloudKitContainer mirror the data from the cloudKit public database to the local Core Data if the user is not logged in?
Here is the code that works only if the user is logged in. Again, I'm connecting to the Public database. class CoreDataManager: ObservableObject{ // Singleton static let instance = CoreDataManager() private let queue = DispatchQueue(label: CoreDataManagerQueue) private var iCloudSync = true lazy var context: NSManagedObjectContext = { return container.viewContext }() lazy var container: NSPersistentContainer = { return setupContainer() }() func updateCloudKitContainer() { queue.sync { container = setupContainer() } } func setupContainer()->NSPersistentContainer{ let container = NSPersistentCloudKitContainer(name: CoreDataContainer) guard let description = container.persistentStoreDescriptions.first else{ fatalError(###(#function): Failed to retrieve a persistent store description.) } description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) let cloudKitContainerIdentifier = iCloud.com.example.PublicDatabaseTest let options = NSPersistentCloudKitContainerOptions(containerIdent
Jun ’24
Can the NSPersistentCloudKitContainer mirror the data from the cloudKit public database to the local Core Data if the user is not logged in?
I'm currently syncing Core Data with the CloudKit public database using NSPersistentCloudKitContainer. The app starts with an empty Core Data store locally and at the app launch it downloads the data from CloudKit public database to the Core Data store, but this can only be accomplished if the user is logged in, if the user is not logged, no data gets downloaded and I get the error below. Can the NSPersistentCloudKitContainer mirror the data from the CloudKit public database to the local Core Data even if the user is not logged in? Can someone please confirm this is possible? The reason for my question is because I was under the impression that the users didn't need to be logged to read data from the public database in CloudKit but I'm not sure this applies to NSPersistentCloudKitContainer when mirroring data. I know I can fetch data directly with CloudKit APIs without the user beign logged but I need to understand if NSPersistentCloudKitContainer should in theory work with
4
0
990
Jun ’24
Reply to Restart sync when using NSPersistentCloudKitContainer
With the following code I'm successfully turning On or Off sync between CoreData and CloudKit. I have iCloudSync saved in UserDefault / @AppStorage and controlled with a toggle switch in my app settings. This line is what turn it off description.cloudKitContainerOptions = nil, I hope it helps. class CoreDataManager: ObservableObject{ // Singleton static let instance = CoreDataManager() private let queue = DispatchQueue(label: CoreDataManagerQueue) @AppStorage(UserDefaults.Keys.iCloudSyncKey) private var iCloudSync = false lazy var context: NSManagedObjectContext = { return container.viewContext }() lazy var container: NSPersistentContainer = { return setupContainer() }() func updateCloudKitContainer() { queue.sync { container = setupContainer() } } func setupContainer()->NSPersistentContainer{ let container = NSPersistentCloudKitContainer(name: YourCoreDataContainerName) guard let description = container.persistentStoreDescriptions.first else{ fatalError(###(#function): Failed to retrieve a persis
Jun ’24
Understanding Syncing between Core Data and CloudKit Public Database using NSPersistantCloudKitContainer
Can someone please give me an overview of how sync works between Core Data and the public CloudKit database when using the NSPersistentCloudKitContainer and please point out my misunderstandings based on what I describe below? In the following code, I'm successfully connecting to the public database in CloudKit using the NSPersistentCloudKitContainer. Below is how I have Core Data and CloudKit set up for your reference. In CloudKit I have a set of PublicIconImage that I created manually via the CloudKit Console. I intend to be able to download all images from the public database at the app launch to the local device and manage them via Core Data to minimize server requests, which works but only if the user is logged in. This is the behavior I see: When the app launches, all the CloudKit images get mirrored to Core Data and displayed on the screen but only if the user is logged in with the Apple ID, otherwise nothing gets mirrored. What I was expecting: I was under the impression that when co
0
0
574
Jun ’24
Reply to CloudKit + SwiftData
In the CloudKit environment, duplicates can indeed happen if multiple peers can create the same data. We discuss this topic in Remove duplicate data. (As of today, SwiftData + CloudKit integration uses NSPersistentCloudKitContainer, and so the content applies.) Regarding CloudKit public database, yeah, it fits the use case where any user can generate data and all users can read the data. However, SwiftData + CloudKit public database integration is not supported today. You probably need to stick with NSPersistentCloudKitContainer if to use CloudKit public database. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Jun ’24
Reply to SwiftData and CloudKit learning resource
In the SwiftData layer, the only thing you can use to configure CloudKit is ModelConfiguration.CloudKitDatabase. Underneath, SwiftData uses NSPersistentCloudKitContainer for CloudKit integration, so all the documentations and knowledges related to NSPersistentCloudKitContainer help understand how SwiftData + CloudKit works, and you can start with CloudKit mirroring. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’24
Reply to CloudKit in TestFlight: No sync between devices 😭
In this situation, you might consider capturing and analyzing a sysdiagnose. The following technote discusses how to do so: TN3163: Understanding the synchronization of NSPersistentCloudKitContainer The following related technote may also help: TN3164: Debugging the synchronization of NSPersistentCloudKitContainer Best, —— Ziqiao Chen  Worldwide Developer Relations.
Jun ’24