Search results for

NSPersistentCloudKitContainer

589 results found

Post

Replies

Boosts

Views

Activity

How to read/write to App1’s database from App2?
I have 2 apps in the App Store, and each uses the private database in its own CloudKit container. (ie, App1 uses “iCloud.com.company.App1” and App2 uses “iCloud.com.company.App2”) I want to add a feature to App2 which will require App2 to read/write to the App1 database. To be clear, we’re talking about 2 apps, 2 private databases, but all access occurs under the same user’s AppleID. In App2, I’ve tried to create 2 NSPersistentCloudKitContainers - one for each App’s database as follows: @main struct App2: App { @StateObject var app1DB = PersistenceApp1.shared @StateObject var app2DB = PersistenceApp2.shared @SceneBuilder var body: some Scene { WindowGroup { NavigationView { ContentView() .environmentObject(app1DB) .environmentObject(app2DB) } } } } …where each Persistence object is defined like this… class PersistenceApp1: ObservableObject { static let shared = PersistenceApp1() let container: NSPersistentCloudKitContainer init(inMemory: Bool = false) { container = NSPersistentCloudKitContainer
2
0
1.3k
Sep ’22
Reply to How to read/write to App1’s database from App2?
use NSPersistentCloudKitContainerOptions to configure the container identifier to sync with class PersistenceApp1: ObservableObject { static let shared = PersistenceApp1() let container: NSPersistentCloudKitContainer init(…) { container = NSPersistentCloudKitContainer(name: “App1”) let store = NSPersistentStoreDescription() store.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: iCloud.com.example.App1) container = [store] container.loadPersistentStores… } … } Also, make sure to add both container identifiers to your app's Signing & Capabilities > iCloud > Containers list
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Reply to How to read/write to App1’s database from App2?
OK, I've solved my issue. 1st of all, defining 2 NSPersistence : ObservableObjects most definitely wasn't the way to go: @StateObject var app1DB = PersistenceApp1.shared @StateObject var app2DB = PersistenceApp2.shared I just needed one: @StateObject var persistence = Persistence.shared deeje's answer pointed me in the right direction, but what was still missing for me were the following 3 key ideas: container's name: is just the name of the .xcdatamodeld - and that this has nothing to do with what I expected (ie, that it was somehow related to my CloudKit container ids “iCloud.com.company.App1”) container = NSPersistentCloudKitContainer(name: Model) Within Model.xcdatamodeld I needed to define 2 separate configurations - with each configuration holding the Entities that are held within the the CloudKit containers: App1Config - holds the Entity from iCloud.com.company.App1, and App2Config - holds the Entity from iCloud.com.company.App2 I needed 2 store descriptions for each app, and to specify the sq
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’22
Detect and handle string conflicts in Core Data with iCloud Sync
I'm trying to create a note-taking like app that uses NSPersistentCloudKitContainer and core data. The store uses the NSMergeByPropertyObjectTrumpMergePolicy, which is fine for almost every property. For example, if the name of a file is changed on two different devices, then it's fine to use the latest value. The problem is that the note text cannot be overridden by the latest value if it's changed on two devices at once. It needs to be detected as a conflict so the user can choose which version they want to keep. I can replicate the behavior by turning off wifi on one device and writing content, then writing content on a different device at the same time. When I turn the wifi back on, whichever device saved the changes last completely overrides the other device's text. What I'd like to accomplish is detect when there is a conflict of text, then create a duplicate file called Conflicted Copy. Bonus points if someone can tell me how Apple Notes magically merges text without ever creating a conflict.
0
0
1k
Sep ’22
Can I use NSPersistentCloudKitContainer while at the same time adding extra fields to my CloudKit CKRecord?
For example, because NSPersistentCloudKitContainer doesn't support ordered relationship, I'd like to have an array of references on my CKRecord because CloudKit support NSArray field type; I can then sync these array manually (e.g. persist without relying on Core Data). Is it okay or recommended to do so?
1
0
1.1k
Sep ’22
Reply to Can I use NSPersistentCloudKitContainer while at the same time adding extra fields to my CloudKit CKRecord?
hi, my guess is that you're playing with fire if you go behind NSPersistentCloudKitContainer's back (!) if you want to keep the notion of an ordered set for a relationship among two entities that is A <---->> B (many B entities associated with an A, but only one A entity is associated with a given B), then two options come to mind (and these can be handled directly on the Core Data side). first, add an integer attribute to B to indicate its relative position among all its sibling B objects. if A changes the order of the B objects, just rewrite the position of each (or as may as needed) associated B. second ... perhaps if each B object represents lots of data and you think it an expensive operation to rewrite many B objects when reordered ... then consider adding an intermediate entity C with one integer attribute (perhaps named position) and two relationships: one from C to A (many-to-one) and one from C to B (one-to-one), so that you have A <---->> C <----> B. in essence, eac
Sep ’22
Core Data + CloudKit - data record type does not appear in CloudKit Dashboard
I created a blank SwiftUI project and included Core Data and Cloud Kit. By selecting Core Data and CloudKit while creating the project, Apple already included some Core Data sample code and entities in the project, and also a Persistence.swift file that contained NSPersistentCloudKitContainer code. The sample code included a SwiftUI list in ContentView, with an Add button to add timestamp to the list when pressing. **I suppose after some basic setup to the project and created the CloudKit container, the app data will auto sync and can be viewed in CloudKit dashboard, but it didn't. ** Here is what I have setup: Created the project with Core Data and Cloud Kit Haven't touch any code from the Persistence.swift file since Apple has already set it up. Also haven't touch the app struct, content view and core data entities (left all sample code untouched) Selected Automatically Manage Signing and Development Team, with a paid developer acc. Added iCloud Capability Selected CloudKit in iCloud Capability, Cr
2
0
1.8k
Sep ’22
How to enable/disable cloudkit in runtime.
I am trying to implement toggling iCloud sync in runtime for my App, i.e. when users toggle the switch in my app, a persistent container will be created according to sync on or off. it works when relaunching the app after changing the container, but how to do it in runtime? how to reload data from the new container and refresh into UIs which already contain the old data from the previous container? I searched all over the internet but could not find a solution to toggle iCloud sync in runtime properly. Below is the code from my core data stack: lazy var persistentContainer: NSPersistentContainer = { setupPersistentContainer() }() private func setupPersistentContainer() -> NSPersistentContainer { let isSyncOn = UserDefaults.standard.bool(forKey: isSyncOn) let container = isSyncOn ? NSPersistentCloudKitContainer(name: BaseTypes) : NSPersistentContainer(name: BaseTypes) guard let description = container.persistentStoreDescriptions.first else { fatalError(###(#function): Failed to retrieve a persisten
0
0
1.1k
Oct ’22
0xdead10cc & NSPersistentCloudKitContainer
We're receiving crash reports where our app is killed by the system because of 0xdead10cc. If I understand correctly this is happening because our app has a file lock or SQLite database lock. Our app is using NSPersistentCloudKitContainer and is indexing the data to Core Spotlight. In my naiveté I assumed that NSPersistentCloudKitContainer and Core Spotlight would release any locks when app is suspended. What is the best way to solve this? Asking for more background time doesn't feel like a solution, that will just postpone the crash.
1
0
1.3k
Oct ’22
Reply to 0xdead10cc & NSPersistentCloudKitContainer
HI, Asking for more background time doesn't feel like a solution, that will just postpone the crash. Actually, that is the answer, at least partly. The reason this can't really happen: In my naiveté I assumed that NSPersistentCloudKitContainer and Core Spotlight would release any locks when app is suspended. ...is that the system can't really know in advance what you're going to ask it to do. They actually take out some of their own tasks, but that doesn't really help if the suspension process is starting at roughly the same time you're asking them to do work. That solution here is to create your own background task for the larger job that you're trying to finish (Add some stuff to my CoreSpotlight index) and end it once the work is done. -Kevin Elliott DTS Engineer, CoreOS/Hardware
Oct ’22
CloudKit - How to use Configurations to properly segregate public and private data
I am trying to understand the concepts between two of the CloudKit code samples: CoreDataCloudKitDemo, which shows sync between CoreData and CloudKit CoreDataFetchedProperty which shows how you can keep public and private data in two CoreData configurations and join them together. After some trial and error I created a single NSPersistentCloudKitContainer that I thought used the two separate configurations - each had it's own local persistent store, database scope was set properly for both stores, etc. But when I run the app it complained of the following: Failed to load persistent stores:Error Domain=NSCocoaErrorDomain Code=134060 A Core Data error occurred. UserInfo={NSLocalizedFailureReason=CloudKit integration does not allow relationships to objects that aren't sync'd. The following relationships have destination entities that not in the specified configuration. EntityA: entityB - EntityB So, I went back to the model and although I had created two separate Configurations (with EntityA in one and
2
0
1.3k
Oct ’22
CloudKit UICloudSharingController `recordChangeTag specified, but record not found`
Hello! I'm using NSPersistentCloudKitContainer for storing data and UICloudSharingController for sharing. Here's how UICloudSharingController is created: func share(_ record: Record, avatar: UIImage) { let controller = shareControllerForRecord(record, avatar: avatar) controller.delegate = self controller.popoverPresentationController?.sourceView = self.view controller.popoverPresentationController?.permittedArrowDirections = [] present(controller, animated: true) } func shareControllerForRecord(_ record: Record, avatar: UIImage) -> UICloudSharingController { if let share = share(for: record) { let controller = UICloudSharingController(share: share, container: self.cloudKitContainer) return controller } else { let controller = UICloudSharingController { [weak self] (controller, completion: @escaping (CKShare?, CKContainer?, Error?) -> Void) in guard let self = self else { return } self.persistentContainer.share([record], to: nil) { objectIDs, share, container, error in if let share = share { rec
0
0
810
Oct ’22
NSPersistentCloudKitContainer - creates CD_ CKRecord but does not populate values
I have a working app with core data and cloudkit. I changed the NSPersistentContainer to NSPersistentCloudKitContainer. Two NSManagedObjects 'Contact' and 'Location' already exist in core data with relationships along with some other related NSManagedObjects. Populated core data in 'Contact' and saved. Logged in to CloudKit to see it had created CD_Contact and CD_Location CKRecords along with some other CKRecords. Queried CD_Contact it gave error - index name not available on recordName. So created an Index of type Queryable on recordName and searched again. There is no data in CD_Contact. It is supposed to sync data from core data to cloud Kit, but only CKRecord is created but data saved in core data is not synced and populated in CloudKit. How to fix this? On debug I get this: , CD_contactEmail = crsp@gmail; CD_contactEmail_ckAsset, CD_contact, CD_contactEmail, My AppDelegate Code: let container = NSPersistentCloudKitContainer(name: MyModel) let defaultStoreURL = try! FileManager.default.
0
0
1k
Nov ’22
SwiftUI macOS app not syncing with iCloud
I have an iOS and iPadOS app that use iCloud to sync. They are in the App Store and the database was pushed to production. They work as advertised. I wrote also a macOS app that uses Cloudkit, the container it set as NSPersistentCloudKitContainer and the merge policies are set ok. I also checked that the profile for the app and certificates are good to go, as well as the permissions of the app. Apple signed the app, but, it doesn't sync with iCloud and I can't figure out why. I checked iCloud on my Mac and the app is there, set to be syncing. I have already read a few articles and have tried all the possible solutions, including signing out and back in, rebuilding the app, etc. One thing, thought, when I delete an item, the app takes sometime to react to it, as if it's trying to connect. I tried with TCPDump, and other things to check for connection issues, but I can't find that app trying to connect. I took a few screenshots of the different configs, etc so you can see as well, but has anyone run in
2
0
2.1k
Nov ’22