iCloud & Data

RSS for tag

Learn how to integrate your app with iCloud and data frameworks for effective data storage

CloudKit Documentation

Post

Replies

Boosts

Views

Activity

Document-based SwiftData apps fail to identify a store on iPad?
When trying to run my document-based iPad app using iPadOS 18 beta and Xcode 16 beta, I get an error like the following after opening a document: Thread 1: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData<MyProject.MyModel> from [:] In order to help track down what is going wrong, I downloaded the sample app from WWDC23 session "Build an app with SwiftData" found here: https://developer.apple.com/documentation/swiftui/building-a-document-based-app-using-swiftdata When I try to run the end-state of that sample code, I get a similar error when running the app on my iPad and creating a new deck: Thread 1: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData<SwiftDataFlashCardSample.Card> from [:] Given that the sample project is generating the same error as my own project, is this a problem with SwiftData and document-based apps in general? Or is there a change of approach that I should try?
0
0
44
1d
How to Clear System Data on Mac
How do I clear my system data on my macbook as it is taking up a huge portion of my storage? I have already tried checking if my cache folder in ~/library was too big but it is only 7.42 GB. Also, I checked if time machine was on to find snapshots but it seems I have had it off for a while now and I didn't know. Could this be causing issues and should I have it on from now on? Please help I need more storage.
0
0
42
1d
How do I handle changes from sync with Swift Data?
In Core Data, you can use a pinned query generation to make sure that your app is working from a consistent view of the data store. If you have CloudKit sync turned on, and new changes come in that invalidate relationships, your app won't see them right away as long as it's looking at a pinned query generation. Since Swift Data doesn't yet support query generations, how do I deal with this issue in Swift Data apps? For example, let's say I have an address book app. I open a particular contact, and then tap a control on the screen that opens a list of images for that contact. While looking at the images, CloudKit sync retrieves changes made by other devices, which have completely removed the parent contact. How does my app know this has happened? Suppose the image browser screen needs to refer to the parent contact, or make changes to it, but the contact is no longer there because a background sync removed it.
1
0
64
1d
API for new "Keep Downloaded" feature in iCloud Drive?
It looks like in iOS 18, there is a new option in Files to "Keep Downloaded". I.e., mark a file to be downloaded and never evicted. Is there an API that would allow us to provide this feature in our app? Our app stores files in a ubiquitous container and displays them in a custom UI (not a UIDocumentBrowserViewController). We've had a longstanding request from users that they be able to mark a file as always available locally, so that they can make sure they have access to it even if they go offline. We've done a hacky, best-effort implementation of this, but there is no truly reliable way to achieve this through the existing APIs.
1
0
73
2d
Occasional crash on `context.save()`
Recently, I noticed many crashes reported daily for context.save() in my code. I tried to resolve the issue by refactoring some of the code. My key change is to use context.performAndWait to fetch Core Data objects instead of passing them around. I refactored the code to pass NSManagedObjectID around instead of NSManagedObject itself because NSManagedObjectID is thread-safe. However, even after the refactor, the issue is still. The crash log is attached as below. I'm confused that it doesn't crash on all devices so I have no clue to find the pattern of the crashes. I also enabled -com.apple.CoreData.ConcurrencyDebug 1 on my local dev box, but seems the error cannot be caught. The new code (after I refactored) still causes the crash: private func update(movements: [InferredMovement], from visitOutObjectId: NSManagedObjectID, to visitInObjectId: NSManagedObjectID?) { let context = PersistenceController.shared.container.viewContext context.performAndWait { guard let visitOut = try? context.existingObject(with: visitOutObjectId) as? Visit else { return } var visitIn: Visit? if let visitInObjectId { visitIn = try? context.existingObject(with: visitInObjectId) as? Visit } visitOut.movementsOut.forEach { context.delete($0) } visitIn?.movementsIn.forEach { context.delete($0) } for inferred in movements { let movement = Movement(context: context) movement.type = inferred.type movement.interval = inferred.interval movement.visitFrom_ = visitOut movement.visitTo_ = visitIn } visitOut.objectWillChange.send() context.saveIfNeeded() } } Note, saveIfNeeded() is an extension function implemented as: extension NSManagedObjectContext { /// Only performs a save if there are changes to commit. @discardableResult public func saveIfNeeded() -> Error? { guard hasChanges else { return nil } do { try save() return nil } catch { defaultLogger.error("Core Data Error: Failed to save context") return error } } } Crash context
1
0
42
2d
iCloud Synchronization doesn’t work
I’m trying to sync data from the AppData on one device to another device with the same iCloud. It uploads the data to the CloudKit but it doesn’t write the data it fetches from the cloud into the AppData storage. It should store a timestamp, a few integers, and two lists of integers. The lists are both optional and store numbers. They can have 0 up to 50 numbers in them. This is the part where it should fetch the records and store them in the AppData let container = CKContainer(identifier: "iCloud.com.calchunt") let privateDatabase = container.privateCloudDatabase let dispatchGroup = DispatchGroup() var errors: [Error] = [] // Leere Liste zum Speichern neuer Spielsitzungen aus der Cloud var newGameSessions: [AppModule.GameSession] = [] for gameSession in gameSessions { let recordIDString = gameSession.id.uuidString // UUID zu String umwandeln let recordID = CKRecord.ID(recordName: recordIDString) dispatchGroup.enter() privateDatabase.fetch(withRecordID: recordID) { (existingRecord, error) in defer { dispatchGroup.leave() } if let error = error { // Fehler beim Abrufen des Records print("Fehler beim Abrufen des Records aus CloudKit: \(error.localizedDescription)") errors.append(error) } else if let existingRecord = existingRecord { // Record existiert in der Cloud print("Record mit ID \(existingRecord.recordID.recordName) existiert in CloudKit") // à berprüfen, ob der Record bereits im AppModule vorhanden ist if let _ = gameSessions.firstIndex(where: { $0.id == gameSession.id }) { // Record existiert bereits im AppModule, überspringe das Speichern print("Record mit ID \(existingRecord.recordID.recordName) existiert bereits im AppModule, überspringe das Speichern") } else { // Record existiert nicht im AppModule, füge ihn zur Liste der neuen Spielsitzungen hinzu let newGameSession = AppModule.GameSession( id: gameSession.id, losungszahl: gameSession.losungszahl, elapsedTime: gameSession.elapsedTime, currentDate: gameSession.currentDate, skipped: gameSession.skipped, skipped2: gameSession.skipped2, level: gameSession.level ) newGameSessions.append(newGameSession) print("Record mit ID \(existingRecord.recordID.recordName) wird zum AppModule hinzugefügt") } } else { // Record existiert nicht in der Cloud print("Record mit ID \(recordID.recordName) existiert nicht in CloudKit") } } } dispatchGroup.notify(queue: .main) { if !errors.isEmpty { for error in errors { print("Fehler beim Abrufen der Daten aus CloudKit: \(error.localizedDescription)") } } else { // Füge neue Spielsitzungen zum AppModule hinzu gameSessions.append(contentsOf: newGameSessions) // Speichere die aktualisierten Daten im AppStorage do { let encoder = JSONEncoder() let gameSessionsData = try encoder.encode(gameSessions) GameSessions = gameSessionsData print("Daten erfolgreich aus CloudKit geladen und im AppStorage gespeichert") } catch { print("Fehler beim Codieren und Speichern der Daten im AppStorage: \(error.localizedDescription)") } } } }
1
0
64
3d
CloudKit Request - CKAsset size limit and public database storage per active user
Hello, I had a WWDC Lab with two CloudKit engineers who asked me to file a "Feedback Request" for critical information regarding CloudKit. I've filed the FB and have also decided to post a forum post to increase my chances of having these critical questions answered. If allowed, I will also post responses to my FB here. CKAssets I would like to know how large assets attached to a CKAsset can get before being rejected by the system. If the figure differs for private and public databases, please also let me know. CloudKit pricing information There used to be pricing information available on the website, but there's basically no information now. This makes it hard to calibrate user upload limits for my app in order to avoid overage fees. I'm not looking to game the system, (something this strange opaqueness is likely meant to prevent); I'm just looking to avoid a situation where competitors and vandals abuse my the content upload system so I get smacked by large bills out of nowhere. A rough figure of how many GB of data each active user adds to my app's CloudKit public database would suffice. While we're at it, if I have two apps that share a public database (if that's possible), do the active user counts of both contribute to the public database's free threshold?
0
0
63
3d
SwiftData Custom Datastore Docs and Implementation
I have watched the following WWDC 2024 sessions: What’s new in SwiftData Create a custom data store with SwiftData Platform State of the Union Now, I have an application that exposes a GraphQL API endpoint that's using PostgreSQL Server 16.3 database. Next, this API returns JSON to the client application (i.e. SwiftUI app). Furthermore, I have checked the current documentation and the above videos appear to be the best reference at this time. My proposed architecture looks like the following: SwiftUI <--> SwiftData <--> PostgreStoreConfiguration && PostgreStore TBD <--> GraphQL API <--> PostgreSQL Thus, I have the following questions: Are there plans to add common out-of-the-box data store implementations for PostgreSQL, Cassandra, Redis, and so on? Will it be possible to implement data stores built to use gRPC, GraphQL, REST, and others to name a few? Will there be more documentation on the actual creation of a custom data store because the current documentation provides a slim API reference? I look forward to your feedback regarding the SwiftData custom data stores.
1
0
118
3d
CloudKit in TestFlight: No sync between devices 😭
I have read and tried all the possible solutions available online, but still didn't get a result. My multi-platform iOS/macOS app uses private databases in iCloud with Core Data. All works as expected when I build the app from Xcode to my multiple devices: data is being synced. But when I upload the app to TestFlight, data is not being synced. This is what I have already tried: In CloudKit Dashboard, I reset the schema and deployed schema changes from the development to production. In Xcode project settings, in Targets, under Frameworks, Libraries... I added the CloudKit.framework, set as "do not embed". In Xcode project settings, under Signing & Capabilities, all the CloudKit, Background fetch and Remote notifications checkboxes are enabled for both Debug and Release. They all point to the same correct iCloud container. In Xcode project settings, under Build Settings, Code Signing Entitlements for both Debug and Release point to the same entitlements file. In .entitlements file, CloudKit container identifier points to the correct container. iCloud Services set to CloudKit. In .entitlements file, APS Environment for both iOS and macOS is set as "production". In Core Data .xcdatamodeld file, under Configurations, I have a Default option, and it is being set to "Used with CloudKit." Each time I upload new version to the TestFlight, I delete all the previous versions from all my devices, so development and production containers are not mixed up in any way. I understand that I may be missing something. But after researching all the resources available online, I didn't find anything else to configure or to add in this setup. I want to point out again that data is not being synced only in TestFlight, and thus, possibly, after release. Whenever I build app directly to the device from Xcode, all works as expected. I hope someone can help me.
2
0
57
3d
Setting .fetchBatchSize causes entire collection to be immediately traversed
Using an @FetchRequest in my SwiftUI-redesigned app, I was looking to improve performance of my search and filtering on a List that has potentially a large amount of data. One of the things I was asking about was how to load some data initially and then fetch additional objects as the user scrolls. In a WWDC lab today, while viewing my code the Apple engineer noted I had set a .fetchLimit on my FetchRequest, and suggested that I should be setting a .fetchBatchSize. It sounded like that was designed to do exactly what I wanted. However, having attempted this, I can see from the console output that when I set a .fetchBatchSize of 50 and go to the view, it immediately loops through the entire collection of thousands of objects, attempting to load them into memory, and as some contain images this eventually crashes with an out of memory error. This data is presented in a List. Originally, inside that list there was a ForEach inside that list but the same problem was seen when I removed the ForEach and used: List(entries) { entry in if let entryData = viewModel.entryData(for: entry) { TimelineEntryView(entryData: entryData) } } Can you advise on what might be going on here? I recall that in the previous iteration of my app, in UIKit and using an NSFetchedResultsController I had the exact same symptom: setting a fetch batch size immediately traversed the whole collection in the datastore, leading to terrible performance rather than the hoped-for good performance. So I did not set a batch size then either. Am I holding this wrong? Is there something that might be triggering this?
3
0
121
3d
Using Core Data with the Swift 6 language mode
I'm starting to work on updating my code for Swift 6. I have a number of pieces of code that look like this: private func updateModel() async throws { try await context.perform { [weak self] in // do some work } } After turning on strict concurrency checking, I get warnings on blocks like that saying "Sending 'self.context' risks causing data races; this is an error in the Swift 6 language mode." What's the best way for me to update this Core Data code to work with Swift 6?
2
0
187
3d
SwiftData SchemaMigrationPlan and VersionedSchema not Sendable?
I've just tried to update a project that uses SwiftData to Swift 6 using Xcode 16 beta 1, and it's not working due to missing Sendable conformance on a couple of types (MigrationStage and Schema.Version): struct LocationsMigrationPlan: SchemaMigrationPlan { static let schemas: [VersionedSchema.Type] = [LocationsVersionedSchema.self] static let stages: [MigrationStage] = [] } struct LocationsVersionedSchema: VersionedSchema { static let models: [any PersistentModel.Type] = [ Location.self ] static let versionIdentifier = Schema.Version(1, 0, 0) } This code results in the following errors: error: static property 'stages' is not concurrency-safe because non-'Sendable' type '[MigrationStage]' may have shared mutable state static let stages: [MigrationStage] = [] ^ error: static property 'versionIdentifier' is not concurrency-safe because non-'Sendable' type 'Schema.Version' may have shared mutable state static let versionIdentifier = Schema.Version(1, 0, 0) ^ Am I missing something, or is this a bug in the current seed? I've filed this as FB13862584.
1
1
103
4d
Access SwiftData in Widgets
How do I access the SwiftData ModelContainer of my app inside the widget extension? Following the guide to add a Widget Extension I’ve added the necessary target to my project and enabled App Groups. I’ve seen that the Backyard Birds example code offers an example how to build this, but it encapsulates the SwiftData handling in a separate app package. Therefore the example simply points to the app package. Though, in my case I don’t host my SwiftData in an external app package, but simply inside the regular app target. So, my question is how and at which point in code do I need to access the ModelContainer?
1
1
113
4d
SwiftData Remote Database Updates
I just saw the new SwiftData updates, including the DataStore API. I’m wondering if, in the case of a shared remote datastore, it is possible to enable updates to the model context from the datastore without the model context explicitly requesting them (e.g., through database listeners). If I’m not mistaken, when you use CloudKit with SwiftData, this happens, right?
1
0
91
4d
SwiftData ModelContext Fetch Crashing
I'm currently using Xcode 16 Beta (16A5171c) and I'm getting a crash whenever I attempt to fetch using my ModelContext in my SwiftUI video using the environment I'm getting a crash specifically on iOS 18 simulators. I've opened up a feedback FB13831520 but it's worth noting that I can run the code I'll explain in detail below on iOS 17+ simulator and devices just fine. I'm getting the following crash: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The specified URI is not a valid Core Data URI: x-coredata:///MyApp/XXXXX-XXXXX-XXXX-XXXX-XXXXXXXXXXXX' It's almost as if on iOS18 SwiftData is unable to find the file on the simulator to perform CRUD operations. All I'm doing in my project is simply fetching data using the modelContext. func contains(_ model: MyModel, in context: ModelContext) -> Bool { let objId = palette.persistentModelID let fetchDesc = FetchDescriptor<MyModel>(predicate: #Predicate { $0.persistentModelID == objId }) let itemCount = try? context.fetchCount(fetchDesc) return itemCount != 0 }
3
2
144
4d
Using Cloud Storage With SwiftData Custom Data Store
The following videos from WWDC24 say that we can use cloud storage for our SwiftData stores. Video references below: What's new in SwiftData Create a custom data store with SwiftData When watching the videos it shows examples of how we can read and write to a JSON file as a store, rather than using the built in store that comes with SwiftData. But there are mentions that we can use cloud storage like a backend i.e. a database hosted on a server. The only thing that I'm struggling to figure out and it would be great if we had code samples for this is how to achieve using cloud storage as a store since looking at the current implementations of: DataStoreConfiguration DataStore The required functions we need to implement look synchronous rather than asynchronous so how would or could we handle fetching asynchronous data from cloud storage. So how would we handle this? Also it would be great if someone could clarify how or if there is a way to send notifications for changes to your store between different devices similar to CloudKit? Are there there any plans to provide more documentation & sample code for the questions I've asked above? Feedback FB13857743
1
3
123
4d
SwiftData UndoManager leaks?
In any SwiftData project with undo enabled, I noticed that repeatedly undoing and redoing a change will cause memory to continuously climb. The larger the change, the more memory is consumed. This is surprising to me. I confirmed this on iOS and macOS using Xcode 15.4 and 16 beta. To reproduce, simply add undo/redo functionality to any project and repeatedly undo/redo a change. Memory consumption will climb continuously and will not get released. In Paul Hudson's SwiftData tutorials, he has an entire section about the numerous tricks required to get SwiftData relationships to work correctly. Does anyone know if SwiftData also requires tricks to get the UndoManager to work correctly?
1
0
69
4d
CKSyncEngine questions
I've been building an app with CKSyncEngine based off the documentation and sample code on GitHub. So far it's been working great, but I still have a number of questions I couldn't find the answer to I'd like to know before going into production. Here's a list in no particular order: When sending changes, are you expected to always send the entire record or just the fields that changed? It's not clear there would even be a way to know the fields that changed since when we have to populate the CKRecord from our local record, we only know the id. Likewise, when we get a record that changed on the server, do we always get a complete record even if only a single field changed? Related to that, if a record has asset(s), is the complete asset also returned on every server change even if we already have a copy locally and it hasn't been modified? If a record does have an asset, is the asset guaranteed to be downloaded and available at the asset.fileURL location by the time CKSyncEngine calls the delegate? If not, is there a way to know it's still downloading and when it will be available? Is there a way to lazy load assets to avoid unnecessary data fetching? If there is a failure during sync, for example if I fail to save just one record out of many, how do I recover from that? Is there a way to retry? Related, is there a way to verify we're completely in sync with the server? Is there any way to resync besides deleting the state serialization data and doing a complete sync again? Can I use CKSyncEngine from the main app and the app extensions if they share a database and state serialization. For example, when adding an image from the share extension. Any caveats to that? Sorry for all the questions, but I want to make sure this is as efficient and reliable as possible. I'm going to request a Lab as well, but it's the lab request form isn't working at the moment so I figured I'd post here in case it's easier to answer async. Thanks! – Zach
1
0
90
4d