CloudKit

RSS for tag

Store structured app and user data in iCloud containers that can be shared by all users of your app using CloudKit.

CloudKit Documentation

Posts under CloudKit tag

251 Posts
Sort by:
Post not yet marked as solved
0 Replies
246 Views
I've defined 'public' and 'private' configurations in CoreData and configured the 'public' configuration to be handled in the CloudKit publicDB. The two configurations contain disjoint sets of NSManagedObject entity types. Some of the entities in the private configuration reference an entity in the public configuration using a UUID; thereby avoiding cross-configuration CoreData relationships. Avoiding this is a requirement of CoreData + CloudKit. Now, since the UUID is meant to point to an object, in the awakeFromFetch() method I decided to fill in a transient property. No go. Even for a transient property there cannot be a cross-configuration relationship. Why? Is there a way to avoid this constraint? The cost of not having a transient property is to do a fetch of the object based on the UUID every time it is needed. Is this something that one should never worry about?
Posted
by GoZoner.
Last updated
.
Post not yet marked as solved
1 Replies
336 Views
I’m having an issue when two of my SwiftData models have a one-to-many relationship and I have them synced via CloudKit. To be clear, I’ve met all of the requirements to make it iCloud friendly and sync to work. I followed this https://www.hackingwithswift.com/quick-start/swiftdata/how-to-sync-swiftdata-with-icloud, and can confirm I’ve done it correctly because initially I was seeing this crash on startup when I had not: Thread 1: Fatal error: Could not create ModelContainer: SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer) This is to say, the problem may be iCloud related but it’s not due to a wrong model setup. Speaking of which, these are models: @Model class Film { var name: String = "" var releaseYear: Int = 0 var director: Director? = nil init(name: String, releaseYear: Int, director: Director) { self.name = name self.releaseYear = releaseYear self.director = director } } @Model class Director { var name: String = "" @Relationship(deleteRule: .cascade, inverse: \Film.director) var films: [Film]? = [] init(name: String, films: [Film]) { self.name = name self.films = films } } I’ve set the delete rule for the relationship between Film and Director to be cascading because you can’t have a film without a director (to be clear, even when set as nullify, it doesn’t make a difference) And this is the @main App definition: @main struct mvpApp: App { var sharedModelContainer: ModelContainer = { let schema = Schema([ Film.self, Director.self ]) let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false) do { return try ModelContainer(for: schema, configurations: [modelConfiguration]) } catch { fatalError("Could not create ModelContainer: \(error)") } }() var body: some Scene { WindowGroup { ContentView() } } And this is the dummy ContentView: struct ContentView: View { var body: some View { EmptyView() .onAppear { let newDirector = Director(name: "Martin Scorcese", films: []) let film = Film(name: "The Wolf of Wall Street", releaseYear: 2019, director: newDirector) newDirector.films!.append(film) } } } I create a Director with no films assigned. I then create a Film, and the append it to the Director’s [Film] collection. The last step however causes a crash consistently: There is a workaround that involves removing this line from the Film init(): self.director = director // comment this out so it’s not set in a Film’s init() When I do this, I can append the (Director-less) Film to the Director’s [Film] collection. Am I misunderstanding how these relationships should work in SwiftData/CloudKit? It doesn’t make any sense to me that when two models are paired together that only one of them has a reference to the relationship, and the other has no knowledge of the link. The above is a minimum reproducible example (and not my actual application). In my application, I basically compromised with the workaround that initially appears to be without consequence, but I have begun to notice crashes happening semi-regularly when deleting models that I suspect must be linked to setting the foundations incorrectly.
Posted
by Bardi.
Last updated
.
Post marked as solved
6 Replies
1.1k Views
I have an app that is already running with CoreData, and I want to allow the users to upload their data to iCloud so, in the case they need to delete their apps or change devices, they don't lose it. Every time the app is opened, there is a synchronization that happens between CoreData and a JSON file fixture, in order to fill the app with its base values (creating NSManagedObject instances from the aforementioned fixture). Before the iCloud sync, the CoreData model had some constraints for different entities, to enforce uniqueness, but these had to be stripped since CloudKit doesn't support them. Most of these constraints were basically ids that came from the JSON and represent an item in our Firebase database. Given that, I want to make the underlying CKRecord.id the same as these ids, so I can avoid the situation where, if a person open the app in a second device, the fixture data is repeated, since the fixture runs before the sync with the iCloud happens. Is that possible? Any help would be appreciated.
Posted Last updated
.
Post not yet marked as solved
4 Replies
576 Views
When I logged into my cloudkit console to inspect the database for some debugging work I couldn't access the private database. It keeps saying "failed to access iCloud data, please signi n again". No matter how many times I sign in again, whether with password or passwordless key it keeps saying the same thing. It says that message when I click on Public database, and private and shared databases are below it. I only noticed this a couple of days ago. It's done this in the past, but I eventually got back into the database but I don't know what changed to make it work.
Posted Last updated
.
Post not yet marked as solved
1 Replies
345 Views
What interfaces do I use to propagate a CloudKit change in a shared zone to a notification/badge to all participants in the shared zone? Assume I have a 'League' that is the root object in a shared zone and that N Players are members of the league. One of the players, the 'organizer', schedules a 'Game' that is open to any of the players. When the organizer creates the game (in the league's shared zone) and it is mirrored in CloudKit, how can the other players see it (as a timely notification)? I already observe .NSPersistentStoreRemoteChange on NSPersistentStoreCoordinator and NSPersistentCloudKitContainer.eventChangedNotification on NSPersistentCloudKitContainer. Are these delivered in the background? Can/Should they generate a 'local/remote' notification for handling at the AppDelegate level? How? Do I need to use a CKDatabaseSubscription looking for CD_Game records directly? (I'd rather not; because then I'd have a potential race between the remote iCloud database(s) and the local CoreData)
Posted
by GoZoner.
Last updated
.
Post not yet marked as solved
0 Replies
223 Views
I am using the public cloud database to store my application data, this data is accessed by all users of the application, but at some point it is necessary for a user who did not create a respective data in the database to delete it, but from what I read in the documentation this is not possible, only with a permission. How do I allow a user to change or delete any data created by another user in the public cloud database?
Posted
by useravsr.
Last updated
.
Post not yet marked as solved
2 Replies
337 Views
Young developer (old man though) need some advice/guidance on how to fault find iCloud issues. A new App, pretty much started with iCloud setup, and I believe I have fulfilled all the necessary prerequisites. I have built other Apps that have worked find on iCloud, but this one simply refuses to work. Error messages from the Console log file as follows: =====>> error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1223): <NSCloudKitMirroringDelegate: 0x2812901e0>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x15bd05ee0> (URL: file:///var/mobile/Containers/Data/Application/EB138C06-5CE0-4FD2-BDCB-3087AF075ED6/Library/Application%20Support/MyShoppingLists.store) <CKError 0x2823edf80: "Partial Failure" (2/1011); "Failed to modify some record zones"; uuid = 838326A8-25E4-4239-8FBF-F2604F1DEC3D; partial errors: { com.apple.coredata.cloudkit.zone:defaultOwner = <CKError 0x2823ed230: "Server Rejected Request" (15/2001); "Request failed with http status code 500"; uuid = 838326A8-25E4-4239-8FBF-F2604F1DEC3D> }> <<=====>> error: CoreData+CloudKit: NSCloudKitMirroringDelegate recoverFromError:](2303): <NSCloudKitMirroringDelegate: 0x2812901e0> - Attempting recovery from error: <CKError 0x2823edf80: "Partial Failure" (2/1011); "Failed to modify some record zones"; uuid = 838326A8-25E4-4239-8FBF-F2604F1DEC3D; partial errors: { com.apple.coredata.cloudkit.zone:defaultOwner = <CKError 0x2823ed230: "Server Rejected Request" (15/2001); "Request failed with http status code 500"; uuid = 838326A8-25E4-4239-8FBF-F2604F1DEC3D> }> <<=====>> error: CoreData+CloudKit: NSCloudKitMirroringDelegate _recoverFromError:withZoneIDs:forStore:inMonitor:](2604): <NSCloudKitMirroringDelegate: 0x2812901e0> - Failed to recover from error: CKErrorDomain:15 Recovery encountered the following error: (null):0 <<===== Thanks in advance for any suggestions...
Posted Last updated
.
Post not yet marked as solved
2 Replies
310 Views
Hey everyone, I'm trying to get the user's name to display in a welcome screen, but unfortunatelly no success so far. For that, I'm using CKContainer.default().shareParticipant(forUserRecordID: recordID).userIdentity.nameComponents, but the returned nameComponents are empty, despite receiving no error and accountStatus of .available. Here's my code: struct Helper { static func getUserInformation() async throws -> Models.UserInfo { let container = CKContainer.default() let accountStatus = try! await container.accountStatus() var accountStatusDescription = "" switch accountStatus { case .couldNotDetermine: accountStatusDescription = "couldNotDetermine" case .available: accountStatusDescription = "available" case .restricted: accountStatusDescription = "restricted" case .noAccount: accountStatusDescription = "noAccount" case .temporarilyUnavailable: accountStatusDescription = "temporarilyUnavailable" @unknown default: accountStatusDescription = "default" } print("[Helper] CKContainer accountStatus: \(accountStatusDescription) ") // Prints "[Helper] CKContainer accountStatus: available" do { let recordID = try await container.userRecordID() let id = recordID.recordName let participant = try await container.shareParticipant(forUserRecordID: recordID) guard let nameComponents = participant.userIdentity.nameComponents else { throw Models.ServiceError.userIdentityUnknownName } print("[Helper] CKShare.Participant nameComponents \(nameComponents)") // Prints "[Helper] CKShare.Participant nameComponents - " print("[Helper] CKShare nameComponents.givenName \(String(describing: nameComponents.givenName))") print("[Helper] CKShare nameComponents.nickname \(String(describing: nameComponents.nickname))") print("[Helper] CKShare nameComponents.familyName \(String(describing: nameComponents.familyName))") print("[Helper] CKShare nameComponents.namePrefix \(String(describing: nameComponents.namePrefix))") print("[Helper] CKShare nameComponents.nameSuffix \(String(describing: nameComponents.nameSuffix))") print("[Helper] CKShare nameComponents.middleName \(String(describing: nameComponents.middleName))") let name = PersonNameComponentsFormatter().string(from: nameComponents) return Models.UserInfo( id: id, name: name ) } catch { throw error } } } Other than that, this project is using CloudKit for persistence through SwiftData and everything seems to be duly setup and working fine. Any idea of what I might be missing? Any user permissions required? As far as I understood, from iOS 17 on and using this code, no permissions are required anymore but I may be wrong. Any hint / help would be much appreciated! Cheers, Jorge
Posted
by j_oneill.
Last updated
.
Post not yet marked as solved
3 Replies
527 Views
I enabled Advanced Data Protection for my developer account, and this (understandably) broke access to my private records in CloudKit Console. I disabled Advanced Data Protection but CloudKit Console still cannot connect. In the database popup the "Click here to retry..." option always fails silently. Does anyone know a workaround?
Posted
by jhokit.
Last updated
.
Post not yet marked as solved
0 Replies
196 Views
I implemented to my app iCloud backup and restore services. Basically App uploads it's document directory files to iCloud using NSURL containerURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; and [fileManager copyItemAtURL:fileURL toURL:destinationURL error:&error]; and everything has been OK to upload files. By checking my iCloud account, it has taken the space needed to store those files. However, when I changed my decice, I'm unable to get those files back. At first, files had .icloud extra extension added, but now the function just reports only 5 files from few hunder. The function used to get backup files is: `- (NSArray) getBackupFiles { NSError *error; NSURL *containerURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; if (!containerURL) return nil; // NSArray *keysArray = [[NSArray alloc] initWithObjects: nil]; NSArray *resultsArray = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:containerURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:&error]; if (error) { [self myAlertView:error.localizedDescription]; return nil; } else return resultsArray; } Any ideas, what went wrong?`
Posted Last updated
.
Post not yet marked as solved
2 Replies
232 Views
I'm having some trouble with the following function from the CKSyncEngineDelegate protocol. func nextRecordZoneChangeBatch(_ context: CKSyncEngine.SendChangesContext, syncEngine: CKSyncEngine) async -> CKSyncEngine.RecordZoneChangeBatch? { The sample code from the documentation is func nextRecordZoneChangeBatch( _ context: CKSyncEngine.SendChangesContext, syncEngine: CKSyncEngine ) async -> CKSyncEngine.RecordZoneChangeBatch? { // Get the pending record changes and filter by the context's scope. let pendingChanges = syncEngine.state.pendingRecordZoneChanges .filter { context.options.zoneIDs.contains($0) } // Return a change batch that contains the corresponding materialized records. return await CKSyncEngine.RecordZoneChangeBatch( pendingChanges: pendingChanges) { self.recordFor(id: $0) } } init?(pendingChanges: [CKSyncEngine.PendingRecordZoneChange], recordProvider: (CKRecord.ID) -> (CKRecord?)) works fine for the sample app which only has one record type, but it seems incredible inefficient for my app which has a dozen different record types. The recordProvider gives you a CKRecord.ID, but not the CKRecord.RecordType. Searching each record type for a matching ID seems very inefficient. Doesn't the CKSyncEngine.PendingRecordZoneChange contain an array of CKRecords, not just CKRecord.IDs? According to the documentation CKSyncEngine.RecordZoneChangeBatch has a recordsToSave property, but Xcode reports 'CKSyncEngine.PendingRecordZoneChange' has no member 'recordsToSave' I'm looking for someway to get the CKRecords from syncEngine.state.pendingRecordZoneChanges.
Posted
by adamek.
Last updated
.
Post not yet marked as solved
1 Replies
392 Views
I get this error even though everything is turned on, how can I solve it? It works on IOS but I get this error on VisionOS CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _recoverFromPartialError:forStore:inMonitor:]_block_invoke(2726): <NSCloudKitMirroringDelegate: 0x600003b0c700>: Found unknown error as part of a partial failure: <CKError 0x600000cce460: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; op = 7FE8CD52A7B2E8FC; uuid = D8D1F2C9-2C01-45B2-BECC-270CA5520D55; container ID = "iCloud.Multitools"> let previewContainer:ModelContainer = { do { let config = ModelConfiguration(cloudKitDatabase: .private("iCloud.Multitools")) let container = try ModelContainer(for: NoteModel.self, configurations: config) return container } catch { fatalError("Error to create container") } }()
Posted
by OVRIDOO.
Last updated
.
Post not yet marked as solved
0 Replies
334 Views
I want to make icloud backup using SwiftData in VisionOS and I need to use SwiftData first but I get the following error even though I do the following steps I followed the steps below I created a Model import Foundation import SwiftData @Model class NoteModel { @Attribute(.unique) var id: UUID var date:Date var title:String var text:String init(id: UUID = UUID(), date: Date, title: String, text: String) { self.id = id self.date = date self.title = title self.text = text } } I added modelContainer WindowGroup(content: { NoteView() }) .modelContainer(for: [NoteModel.self]) And I'm making inserts to test import SwiftUI import SwiftData struct NoteView: View { @Environment(\.modelContext) private var context var body: some View { Button(action: { // new Note let note = NoteModel(date: Date(), title: "New Note", text: "") context.insert(note) }, label: { Image(systemName: "note.text.badge.plus") .font(.system(size: 24)) .frame(width: 30, height: 30) .padding(12) .background( RoundedRectangle(cornerRadius: 50) .foregroundStyle(.black.opacity(0.2)) ) }) .buttonStyle(.plain) .hoverEffectDisabled(true) } } #Preview { NoteView().modelContainer(for: [NoteModel.self]) }
Posted
by OVRIDOO.
Last updated
.
Post marked as solved
1 Replies
207 Views
I remember long time ago I could click on a button in Xcode to launch a Web page to manage iCloud data. But now I cannot find the button.
Posted
by imneo.
Last updated
.
Post not yet marked as solved
0 Replies
251 Views
I am checking the Push Notification Console of Cloudkit to understand my apps push delivery metrics. There are nice metrics in there to see push sends and discards. But I couldn't see any metric to understand "How many of the users opened the notiifications I send". Is there any way me to understand this statistic? Also can we export this metrics without logging in to CloudKit like an API request?
Posted
by emindeniz.
Last updated
.
Post not yet marked as solved
1 Replies
316 Views
I have created a simple app where a user is tracking their mood. A simple click of the button inserts the necessary data to a CloudKit database. So I consider each record as a 'transactional' record. I am able to successfully query the data and present in a list view. I am able to sort and incorporate simple predicates. I am now at a point in my development that I would like to add a pie chart based on the users data and I am not sure how to roll-up the data / group by the data / aggregate the data [I am not sure what the correct terminology is within Swift] The pie chart would show the various moods that the exists in the CloudKit database and the slices would be sized based on the count of each mood. Any guidance that you can provide would be greatly helpful!
Posted
by mbain108.
Last updated
.
Post not yet marked as solved
0 Replies
276 Views
My apps are set up to store data in a SQLite database on the device. The user is also able to add images and those are also stored on the device. The database and images are stored in the apps documents folder. The database is set up with four tables, one of them containing a list of selectable items so the information in that table is constant. The other three are read/write to the user. The database also contains a field, which contains true/false as to whether the app has been purchased or not. My thought behind was that this would make the users data private and secure. My apps are set up using UIKit so SwiftData is not an option unless I rewrite the entire app in SwiftUI. Or is there a good way to use SwiftData in UIKit? Is there a way to store/move this information into the cloud so that the data can be synced across multiple devices? Or maybe set up an import/export scenario using a CSV file for the database using Dropbox? Any help or advice would be appreciated. Thanks in advance.
Posted Last updated
.