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.

Posts under CloudKit tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

SwiftData with shared and private containers
I was hoping for an update of SwiftData which adopted the use of shared and public CloudKit containers, in the same way it does for the private CloudKit container. So firstly, a big request to any Apple devs reading, for this to be a thing! Secondly, what would be a sensible way of adding a shared container in CloudKit to an existing app that is already using SwiftData? Would it be possible to use the new DataStore method to manage CloudKit syncing with a public or shared container?
2
2
293
1w
Bricked com.apple.homekit.config CloudKit container
How did the issue (probably) occur? The issue appeared in January 2024 after setting up a new Apple TV 4K (tvOS 16.X and updated to latest tvOS after setup) or a new Apple Watch Ultra 2 (watchOS 10.X and updated after setup). Both devices were set up using my personal iCloud and both were set up to use HomeKit. I suspect the issue is related to the creation of a new Home that was subsequently shared to a family member (see below). How did I notice the issue? From the week that followed, I noticed that my iPhone 15 Pro Max tends to get really hot in standby and that the battery drops significantly in the space of a couple standby hours. (Worst case I’ve experienced is about ~80% drop in 4 hours standby) Apple Watch also has huge drops in battery life. Again, to give some perspective: I wear my AW for sleep tracking, sometimes AW will drop ~5% throughout the night, sometimes ~60% (and turn off if it was charged below 60%, making me lose my sleep tracking). My iPad Pro occasionally loses ~30-50% charge over night in standby. I went to the Battery section of the Settings app on my iPhone and iPad and could notice that about 50-75% of the time (over 24h that is), Home Accessories is running in the background. I could not confirm this on Apple Watch as the battery section of Settings does not show per app usage, however clear drops in battery percentage are noticeable from the graph. What's the issue? Seeing the issue arise on three of my four portable Apple devices, I decided to go check on my MacBook Pro M1 Max in Activity Monitor if the same behaviour was to be seen. I quickly realised that the Home Daemon (homed) is permanently running in background at (unusual) high usage. It is the top entry in CPU usage and Uptime. Going into the Console app I can see that the homed daemon on macOS is throwing 3x-5x batches of the same 5 error messages each second. The errors are the following: In the same Console app, connecting to my iPhone and my iPad, I can see the exact same errors occurring at approximately the same rate. What is the issue in technical terms? My CloudKit HomeKit config (com.apple.homekit.config) container is bricked and returns an internal server error The homed/cloudd daemons do not have a backoff policy and retry fetching the CloudKit database instantly on failure. This leads to an infinite loop of network requests going out and draining my battery on all devices massively. How did I try to solve the issue? All these measures were unsuccessful: Restarting all devices All devices are on the latest version Deleting the Home app where possible Turning off Home, Keychain and iCloud Drive in iCloud settings Signing out of iCloud and signing in again Using the HomeKit Reset mobileconfig profile where possible (http://appldnld.apple.com/iOSProfiles/HomeKitReset.mobileconfig) Use the previously mentioned reset profile in combination with a HomeKit Architecture downgrade (http://appldnld.apple.com/iOSProfiles/KeepLegacyHome.mobileconfig) On macOS, delete ~/Library/HomeKit Sending sysdiags using the Feedback Assistant (FB13529370, still open) Manually revoking all (pending and accepted) home invitations and deleting all homes from the Home app Removing all Apple TVs and HomePods from my iCloud account Reseting and repairing Apple Watch Disabled Advanced Data Protection on iCloud Logging out of all my devices’ iCloud, use the reset profile, wait 20min, login to iCloud, the issue reappears in the console (while no other devices are logged in) Changing Apple ID password and choose to log out all devices, log in again into a single device, use the reset profile, wait 20min, and the issue reappears in the console To be absolutely sure it is an iCloud issue and not a local misconfiguration (that could be solved by resetting all my devices), I took an older iPhone 12 Pro and set it up as a new device with no apps/account/data, just a blank installation of iOS. As one would expect, the issue is not present and the Console app does not show any errors. As soon as I log into my iCloud account, the Home daemon homed throws the same 5 errors over and over and the battery draining issue magically appears. This confirms my theory that it is indeed an iCloud issue on the server-side and not a client-side issue. Reseting my devices would not help fix the issue as demonstrated by a blank iOS installation on a separate iPhone 12 Pro getting the same error just by logging into my iCloud account. I’ve had an Apple Support case for over five months, it has been (unsuccessfully) escalated twice to engineering. While many basic troubleshooting steps have been taken, nothing helped. Also, while the Apple Support agent is really trying their best to help me, they understand the symptoms of the problem but not the root cause which I tried explaining multiple times. Essentially we’re sticking to the Apple Support instructions they have, and doing basic diagnostics and battery troubleshooting even though I technically understand and can explain the issue to a CloudKit engineer. We even proceeded to do a fresh install of macOS on a separate volume and took diagnostics before and after login in with my Apple ID. They were able to confirm that there is indeed a massive battery drain issue related to my account. The case is still open but is currently leading nowhere, I'm just told to keep my devices updated... How Apple can fix the issue: Investigating the internal server error and fixing the record(s) or the CoreData entity that is throwing the problem Implementing a client-side backoff policy for internal server errors coming from iCloud. Quite trivially by reseting my com.apple.homekit.config container Also worth mentioning: I don’t use a VPN or any Proxy I’m fine with losing all my HomeKit-related data on my iCloud account. At this point I just want the issue to disappear and to have useable battery life on all my devices. I’m giving my consent to the immediate and irrevocable deletion of all my past and present HomeKit-related data.
2
0
248
2w
Local SwiftData to CloudKit migration
Hi, I've been working on an app that - so far - has only had to deal with offline data store. The usage is fairly simple: the user picks their favourite tv shows and networks, and they get persisted with SwiftData with all the correct relationships. Now, I would like to migrate the local storage of the users to a private CloudKit db, but every time I upgrade the app, the data seems to disappear completely. This is the snippet evolved through the attempt of migrating the data: Current Production code public static func makeModelContainer() -> ModelContainer { do { let processRequiresInMemory = ProcessInfo.processInfo.arguments.contains("inMemoryDatabasePreferred") let modelConfiguration = ModelConfiguration( isStoredInMemoryOnly: processRequiresInMemory, groupContainer: .automatic, cloudKitDatabase: .none ) let modelContainer = try ModelContainer( for: Country.self, Episode.self, Movie.self, Season.self, Show.self, Network.self, NetworkSubscription.self, migrationPlan: AppModelMigrationPlan.self, configurations: modelConfiguration ) return modelContainer } catch { fatalError("Could not initialize model container") } } Testing CloudKit enabled public static func makeModelContainer() -> ModelContainer { do { let processRequiresInMemory = ProcessInfo.processInfo.arguments.contains("inMemoryDatabasePreferred") let modelConfiguration = ModelConfiguration( "synced", isStoredInMemoryOnly: processRequiresInMemory, groupContainer: .automatic, cloudKitDatabase: .automatic ) let modelContainer = try ModelContainer( for: Country.self, Episode.self, Movie.self, Season.self, Show.self, Network.self, NetworkSubscription.self, migrationPlan: AppModelMigrationPlan.self, configurations: modelConfiguration ) return modelContainer } catch { fatalError("Could not initialize model container") } } } The differences, which I don't understand fully because I could not find documentation, are: ModelContainer(...) -> ModelContainer("synced", ...) cloudKitDatabase, from none to automatic. I have the feeling that changing the name of the configuration also changes some reference to the db itself, but if I were not to change the name, the app would have crashed because unable to migrate. What's the best approach to take here?
4
0
308
2w
Migrate Core Data to SwiftData in an App Group (& CloudKit)
Hello, I’m upgrading my app from Core Data to SwiftData. Due to my old setup the Core Data store has an explicitly name like „Something.sqlite“, because it was defined via NSPersistentContainer(name: "Something") before switching to SwiftData. Now my goal is to migrate the Core Data stack to SwiftData, while moving it to an App Group (for Widget support) as well as enable iCloud sync via CloudKit. Working Migration without App Group & CloudKit I’ve managed to get my migration running without migrating it to an App Group and CloudKit support like so: @main struct MyAppName: App { let container: ModelContainer init() { // Legacy placement of the Core Data file. let dataUrl = URL.applicationSupportDirectory.appending(path: "Something.sqlite") do { // Create SwiftData container with migration and custom URL pointing to legacy Core Data file container = try ModelContainer( for: Foo.self, Bar.self, migrationPlan: MigrationPlan.self, configurations: ModelConfiguration(url: dataUrl)) } catch { fatalError("Failed to initialize model container.") } } var body: some Scene { WindowGroup { ContentView() } .modelContainer(container) } } How To Migrate to App Group & CloudKit? I’ve already tried to use the ModelConfiguration with a name, but it seems to only look for a .store file and thus doesn’t copy over the Core Data contents. let fullSchema = Schema([Foo.self, Bar.self]) let configuration = ModelConfiguration("Something", schema: fullSchema) Can someone help me how to do this migration or point me into the right direction? I can’t find anything relating this kind of migration …
1
0
249
2w
How to store Public Data in ICloud
I have been working on an ios application which I decided to use the new SwiftData architecture, and I now have realized that SwiftData does not support public or shared databases using SwiftData. I am a new and upcoming Swift developer, who has been self learning the Apple Swift technology. I just learned in this forum that Swift Data does not support Public and Shared Data and I also understand that no plans that have been announced to addressed this feature in the IOS 18 release time frame. The use case for my application is to implement a social type application, something like applications including X, facebook, etc. These type of applications appear to use Public, Shared and Private data in various existing Apple application. I would like to complete an application using Swift using ICloud, but I must assume that these current social applications are using other technologies. I am assuming you can do this in Core Data, but my understanding is Apple is asking that we use Swift Data to replace core data. I also am assuming that Swift data is built on core data technology layers. So ... to me it seems hopeful, somehow, to accomplish this using IOS 17 and follow the recommend Swift Data Path. What are my options for completing these data objectives in IOS? I hope I am addressing these questions in the proper and best forum? Much thanks for any suggestions.
3
1
383
2w
Failed to create public DB CloudKit subscription in production
I'm attempting to save a CKQuerySubscription in production, in the public database, but I'm seeing the following error: Failed to create CloudKit subscription for Encounter: <CKError 0x3038f66a0: "Invalid Arguments" (12/2006); server message = "attempting to create a subscription in a production container"; op = 093C69242BCC1B0A; uuid = D6E49665-6B8A-4F16-BBD8-B188A4E71F70; container ID = "iCloud.com..."> This is the query’s predicate: NSPredicate(format: "users CONTAINS %@", user.recordID), and the same subscription works perfectly fine in the development CloudKit environment. I have a queryable index on the users field in both environments. Also, I have saved a TRUEPREDICATE CKQuerySubscription in production, and that works perfectly. Does anyone know what I could be missing here?
1
0
217
2w
swiftUI apps with SwiftData and CloudKit crashes on iOS but works on MacOS
sample code let modelConfiguration = ModelConfiguration() // 1. create mom guard let mom = NSManagedObjectModel.makeManagedObjectModel(for: [Attachment.self]) else { fatalError("====> makeManagedObjectModel error") } // 2. create description with config url and setting let description = NSPersistentStoreDescription(url: modelConfiguration.url); description.shouldAddStoreAsynchronously = false; container = NSPersistentCloudKitContainer(name: "swiftDataCloudTest", managedObjectModel: mom); container.persistentStoreDescriptions = [description] // 3. get modelContainer let modelContainer = try! ModelContainer(for: Attachment.self, configurations: self.modelConfiguration) this code works fine on MacOS(14.4.1) and previous iOS 17.x.(iOS 17.2 is OK) but on iOS 17.5, the app crashes with message A Core Data error occurred." UserInfo={NSLocalizedFailureReason=Unable to find a configuration named 'default' in the specified managed object model how can I fix these?
4
0
234
2w
ITMS-90863: Macs with Apple silicon support issue (iCloud library)
Recently I started getting emails from AppStoreConnect when I submit new builds The email states: ITMS-90863: Macs with Apple silicon support issue - The app links with libraries that aren’t present in macOS: /usr/lib/swift/libswiftCloudKit.dylib I can run this app on apple silicon from TestFlight or directly from Xcode and it runs just fine including all iCloud functions. This app has been using iCloud for several years now. So my question is: Should I just ignore the email or do I need to change something to bring this app into compliance?
2
0
288
2w
Do remote notifications work on macOS when app is closed?
I have a native SwiftUI Mac app which is based off my iOS app and includes Core Data with iCloud sync. As per my understanding, when a user makes a change on one of the devices a remote notification is sent to the others. I have a widget which displays information from Core Data and the remote notification makes the widget update its information on the next timeline refresh without the user having to open the app manually. My question is, if an app is closed on macOS so it's not even running in the Dock, do remote notifications work? This page says "the delegate receives this message when the application is running and a remote notification arrives for it". Does that mean the app won't receive remote CloudKit notifications when closed on macOS?
1
0
203
3w
SwiftData doesn't parse my object correctly
Hello, im trying to parse a JSON Request into a Swift Data Model. The specific bug happens with the nutriscore. It works perfectly fine if I do my request and Decode or Encode the Model directly. But when im trying to add it to the Database the field is empty. Somehow when im iterating through each product (my model) and console log it, it works. But only if there is one product in my database. sometimes when im adding another product, it somehow deletes(?) all nutriscores and the console log prints "nil" for all products. even for the product it worked before. This right here is the way I insert into my database which should be perfectly fine. And the printing works also perfectly fine and it always displays the correct nutriscore func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem]) { for item in addedItems { if case let .barcode(barcode) = item { performAPIRequest(with: barcode.payloadStringValue!) { result in switch result { case .success(let product): ... var descriptor: FetchDescriptor<Product> { var descriptor = FetchDescriptor<Product>( predicate: #Predicate { $0.code == product.code } ) descriptor.fetchLimit = 1 return descriptor } var products: [Product] = [] products = try! self.parent.context.fetch(descriptor) print(product.nutriscore ?? "no nutri"); //WORKS PERFECTLY FINE!! if let existingProduct = products.first { existingProduct.amount! += 1 existingProduct.lastScannedAt = Date() } else { self.parent.context.insert(product) } self.processMeals() case .failure(let error): print("Error: \(error)") } } } } } This has to be a SwiftData Bug, or why doesn't it work
1
0
208
4w
Can SwiftData @Model with .externalStorage be used with CloudKit?
Hi, I'm about to adopt .externalStorage but I will also use CloudKit in the near future. I did not find any reference that list of requirements for SwiftData models to be used with CloudKit, only for Core Data models. It seems those Core Data requirements (like no-unique) apply to SwiftData as well. However I did not find any info on this: Can @Attribute(.externalStorage) be used when I want to sync my model with CloudKit?
2
0
218
May ’24
Is there a way to search in SwitData optional to many relationships?
Done this model: @Model public final class Category: Codable { var nom: String = "" @Relationship(deleteRule: .cascade, inverse: \Property.category) var properties: [Property]? } How to create #predicate to search text in like… let predicate = #Predicate<Category> { category in searchText.isEmpty || category.nom.localizedStandardContains(searchText) || category.properties.contains { $0.nom.localizedStandardContains(searchText) } } without this error: SQLCore dispatchRequest: exception handling request: <NSSQLCountRequestContext: 0x6000038dc620>, to-many key not allowed here with userInfo of (null)
0
0
198
May ’24
CloudKit NSPersistentCloudKitContainer Bug
Hi, does someone knows about the bug, that the NSPersistentCloudKitContainer doesn't return CKShares at the first time? Is this an intended behavior? My current fix: let container: NSPersistentCloudKitContainer = .... // Fix begin let managedObject = .... // Object which is in a share let record = container.record(for: managedObject.objectID) // Fix end let shares = (try? container.fetchShares(in: nil)) ?? [] If I execute exactly the same code without the fix that I fetch the record first, then it returns 0 shares. With the fix it is currently 9 shares (the actual count of the shares). Another fix: let container: NSPersistentCloudKitContainer = .... // Fix begin let _ = try? container.fetchShares(in: nil) Thread.sleep(forTimeInterval: 0.1) // Fix end let shares = (try? container.fetchShares(in: nil)) ?? [] For that fix it also returns at the second time the expected count of shares. But without the delay it returns also at the second time zero shares. Anyone had the same problem and if so, how do you solve it correctly? Thanks!
0
0
230
May ’24
CloudKit data is lost when reinstalling the app
We worked with SwiftData, and once CloudKit was integrated, the synchronization worked well. Even if I rerun the app, it works just as well. However, when I delete the app and reinstall it, I get a Token Expired error and CloudKit doesn't work properly. My code is organized like this public lazy var modelContext: ModelContext = { ModelContext(modelContainer) }() private lazy var modelContainer: ModelContainer = { let schema = Schema([ Entity1.self, Entity2.self, Entity3.self, ]) let modelConfiguration = ModelConfiguration( schema: schema, groupContainer: .identifier("myGroupContainer"), cloudKitDatabase: .automatic ) do { return try ModelContainer(for: schema, configurations: [modelConfiguration]) } catch { fatalError("Could not create ModelContainer: \(error)") } }() The error content is as follows error: CoreData+CloudKit: -[PFCloudKitImportRecordsWorkItem fetchOperationFinishedWithError:completion:]_block_invoke(707): <PFCloudKitImporterZoneChangedWorkItem: 0x3022c0000 - <NSCloudKitMirroringImportRequest: 0x3036e7ac0> 1A7E53D4-E95B-423F-8887-66360F6D8865> { ( "<CKRecordZoneID: 0x301bb1bf0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>" ) } - Fetch finished with error: <CKError 0x301bb5650: "Partial Failure" (2/1011); "Couldn't fetch some items when fetching changes"; uuid = 3F346302-C3EE-4F72-820C-988287C92C0A; container ID = "MyContainerID"; partial errors: { com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x301bb1830: "Change Token Expired" (21/2026); server message = "client knowledge differs from server knowledge"; op = 515034AC3ADC4348; uuid = 3F346302-C3EE-4F72-820C-988287C92C0A> }> error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _importFinishedWithResult:importer:](1390): <PFCloudKitImporter: 0x3000a1240>: Import failed with error: <CKError 0x301bb5650: "Partial Failure" (2/1011); "Couldn't fetch some items when fetching changes"; uuid = 3F346302-C3EE-4F72-820C-988287C92C0A; container ID = "MyContainerID"; partial errors: { com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x301bb1830: "Change Token Expired" (21/2026); server message = "client knowledge differs from server knowledge"; op = 515034AC3ADC4348; uuid = 3F346302-C3EE-4F72-820C-988287C92C0A> }> Forcing the ModelContainer to be reinitialized fixes the problem, it's a problem to get this error in the first place, the error doesn't even go to fatal for me, so I don't even know how to verify that it's happening. Is there something I'm doing wrong, or do you have any good ideas for solving the same problem?
2
0
317
May ’24
CloudKit database - 500 Internal error
As of yesterday all queries my app makes to the CloudKit database, and requests via the CloudKit console result in 500 internal errors. We have made no changes to the database or app that could have caused this. The status page for CloudKit database is green. Here is a response from the CloudKit console: {code: 500, message: "internal-error", reason: "Internal error", detailedMessage: undefined, requestUuid: "808955a2-e564-459e-ba9b-1101917ce1a4"}
1
0
228
May ’24
HealthKit SwiftData sync
Hello, I want to build an app that will allow the user to entry some health related records and be synced with the HealthKit. Any record that is in the HealthKit is stored locally on the device. I find it conceptually unsure if I should be storing the HealthKit records in the SwiftData to make the user records available across the iCloud synced devices. Should I read the HealthKit record, make a copy of it (including it's ID) on my app's data in SwiftData? How the syncing should be done the right way? thanks.
1
0
457
May ’24
Huge increase in sqlite file size after adopting CloudKit
I recently converted a Core Data app to use CloudKit. Before the conversion, the sqlite file was around 25MB. After the conversion, the file grew to over one gigabyte. I ran sqlite3_analyzer on the new file and found that a single table, ANSCKRECORDMETADATA, used 95% of the storage, 998MB. Is this to be expected? I confess that the conversion was a bit bumpy. Maybe I did something to create a monstrosity. If not expected, should I revert to the old Core Data file, create a new CloudKit container, and repeat the conversion.
0
0
195
May ’24