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

What is the proper way to handle videos in SwiftData ?
I'm creating an application with swiftui which gets images and videos from the Photos picker then store them with swiftData for later use. I save both images and videos as data with  @Attribute(.externalStorage). But it just seems wrong to me to store the videos that way, they can be several gigabytes in size . What is the correct way to handle something like this ? Is it to store the url and then each time the user wants to see a video save a temporary video ?. If that's the case can anyone show me how this should be done? Any comments appreciated Guillermo
0
0
30
6h
@Attribute(.unique) working as intended, kind of
I have a model that has a unique property, e.g.: @Model final class UserWord @Attribute(.unique) let word: String let partOfSpeech: PartOfSpeech let metaData: ... At the end of the init I have this: init(...) { if partOfSpeech == .verb { metaData = fetchMeta() } } This works fine when a word is newly created and saved. But let's say there's a unique conflict and a user tries to save a new entry with the same word. Apparently this init still fires and fetchMeta edits the existing entry which gives me the error: CoreData: error: Mutating a managed object 0xb890d8167c911ade <x-coredata://4C75194F-D923-477F-BB22-ACBDECCD7530/UserWord/p2> (0x600002170af0) after it has been removed from its context. I think the solution here is to do some manual checking of the modelContext before saving. Would love to hear other's thoughts.
0
0
50
16h
Library/Caches for app groups: automatically deleted when needed ?
In an app we can use FileManager.SearchPathDirectory.cachesDirectory (objc:NSCachesDirectory) to store files that could be recreated if necessary (and will be automatically deleted by iOS in cases of low disk memory). For app groups, there is a shared location that is automatically created as soon as we use containerURL(forSecurityApplicationGroupIdentifier:) (objc:containerURLForSecurityApplicationGroupIdentifier) : Library/Caches Is this cache directory (created by iOS) also gets automatically deleted by iOS in cases of low disk memory ? I also have more related questions : does this cache directory size count in the used disk space by the app displayed in the settings app ? is this cache directory (and same question for the top containerURL directory) saved in the cloud backups ? Does anyone have any information about this?
0
0
42
21h
Swift Data Array with Elements to conform protocol
Hi, I'm try to convert from CoreData to SwiftData. Problem is that I'm using inheritance in CoreData. So, I have a base class "animal" and "cat" and "bird" inherit from it. I can have a array of type "animal" which contains both cats and dogs. Now as I try to move to SwiftData I tried the following approach protocol Animal { var name: String { get set } } @Model class Cat: Animal { var name: String var legs: Int } @Model class Bird: Animal { var name: String var legs: Int var wings: Int } @Model class Enviroment { //leads to error: //Type 'any Animal' cannot conform to 'PersistentModel' var animals: [any Animal] } So how to I get around of this? I would like to store multiple types of animals (classes that conform to protocol animal) … but how can I achieve that? Thanks
0
0
42
1d
iOS 18 SwiftData ModelContext reset
Since the iOS 18 and Xcode 16, I've been getting some really strange SwiftData errors when passing @Model classes around. The error I'm seeing is the following: SwiftData/BackingData.swift:409: Fatal error: This model instance was destroyed by calling ModelContext.reset and is no longer usable. PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://34EE9059-A7B5-4484-96A0-D10786AC9FB0/TestApp/p2), implementation: SwiftData.PersistentIdentifierImplementation) The same issue also happens when I try to retrieve a model from the ModelContext using its PersistentIdentifier and try to do anything with it. I have no idea what could be causing this. I'm guessing this is just a bug in the iOS 18 Beta, since I couldn't find a single discussion about this on Google, I figured I'd mention it. if someone has a workaround or something, that would be much appreciated.
0
1
61
2d
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 connecting to the public database in CloudKit you didn't need to be logged in to read data. Now, if the user is logged in on the first launch, all data is successfully mirrored to Core Data, but then if the user logs off, all data previously mirrored gets removed from Core Data, and I was under the impression that since Core Data had the data already locally, it would keep the data already downloaded regardless if it can connect to CloudKit or not. What am I doing wrong? Core Data Model: Entity: PublicIconImage Attributes: id (UUID), imageName (String), image (Binary Data). CloudKit Schema in Public Database: Record: CD_PublicIconImage Fields: CD_id (String), CD_imageName (String), CD_image (Bytes). Core Data Manager 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(containerIdentifier: cloudKitContainerIdentifier) description.cloudKitContainerOptions = options description.cloudKitContainerOptions?.databaseScope = .public // Specify Public Database container.loadPersistentStores { (description, error) in if let error = error{ print("Error loading Core Data. \(error)") } } container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy return container } func save(){ do{ try context.save() }catch let error{ print("Error saving Core Data. \(error.localizedDescription)") } } } View Model Class class PublicIconImageViewModel: ObservableObject { let manager: CoreDataManager @Published var publicIcons: [PublicIconImage] = [] init(coreDataManager: CoreDataManager = .instance) { self.manager = coreDataManager loadPublicIcons() } func loadPublicIcons() { let request = NSFetchRequest<PublicIconImage>(entityName: "PublicIconImage") let sort = NSSortDescriptor(keyPath: \PublicIconImage.imageName, ascending: true) request.sortDescriptors = [sort] do { publicIcons = try manager.context.fetch(request) } catch let error { print("Error fetching PublicIconImages. \(error.localizedDescription)") } } } SwiftUI View struct ContentView: View { @EnvironmentObject private var publicIconViewModel: PublicIconImageViewModel var body: some View { VStack { List { ForEach(publicIconViewModel.publicIcons) { icon in HStack{ Text(icon.imageName ?? "unknown name") Spacer() if let iconImageData = icon.image, let uiImage = UIImage(data: iconImageData) { Image(uiImage: uiImage) .resizable() .aspectRatio(contentMode: .fit) .frame(width: 35, height: 35) } } } } .onAppear { // give some time to get the images downlaoded DispatchQueue.main.asyncAfter(deadline: .now() + 5){ publicIconViewModel.loadPublicIcons() } } } .padding() } }
0
0
52
3d
Deleting CloudKit data
I have been testing an app which uses cloudKit with SWIFTDATA and after testing for several months my 200GB iCloud store is showing 168GB for iCloud Drive. Now my iCloud drive is only 22.6GB so the rest of the 168GB must be data from my app. Also, I have function in my app to delete all iCloud Data which I thought that should clean up iCloud storage but it does not. I tried resetting the Develop Environment but no change to iCloud data. Also I have several other containers in iCloud created while getting iCloud working which I would like to delete but I understand you can’t. https://forums.developer.apple.com/forums/thread/45251?answerId=788694022#788694022 Bottom line cloudkit console has been pretty much useless for me and I need a way to manage (delete containers and data). Am I missing something?
1
0
50
4d
Account/data management
I am working on a social media app called "Aura Tracker" where people can add/subtract aura from people based off of their actions. I am new to app development so I don't know how to handle user accounts and aura value changing. Content view code: import SwiftUI struct ContentView: View { var body: some View { ScrollView{ VStack { Text("Aura Tracker") Image(systemName: "sparkles") .imageScale(.large) .foregroundStyle(.yellow) Text("You have "+"#"+" aura") } } } } #Preview { ContentView() } Aura_TrackerApp code: import SwiftUI @main struct Aura_TrackerApp: App { var body: some Scene { WindowGroup { ContentView() } } }
0
0
62
4d
SwiftData History Tombstone Data is Unusable
After watching the WWDC video on the new history tracking in SwiftData, I started to update my app with this functionality. Unfortunately it seems that the current API in the first beta of Xcode 16 is rather useless in regards to tombstone data. The docs state that it would be possible to get the data from the tombstone by using a keyPath, there is no API for this however. The only thing I can do is iterate over the values (of type any) without any key information, so I do not know which data is what. Am I missing something or did we get a half finished implementation? There also does not seem to be any info on this in the release notes.
1
0
84
5d
SwiftData Document-based app produces strange write errors
I have a document app built using SwiftData because frankly I'm too lazy to learn how to use FileDocument. The app's title is "Artsheets," and I'm using a document type that my app owns: com.wannafedor4.ArtsheetsDoc. The exported type identifier has these values: Description: Artsheets Document Identifier: com.wannafedor4.ArtsheetsDoc Conforms to: com.apple.package Reference URL: (none) Extensions: artsheets MIME Types: (none) And the code: ArtsheetsApp.swift import SwiftUI import SwiftData @main struct ArtsheetsApp: App { var body: some Scene { DocumentGroup(editing: Sheet.self, contentType: .package) { EditorView() } } } Document.swift import SwiftUI import SwiftData import UniformTypeIdentifiers @Model final class Sheet { var titleKey: String @Relationship(deleteRule: .cascade) var columns: [Column] init(titleKey: String, columns: [Column]) { self.titleKey = titleKey self.columns = columns } } @Model final class Column: Identifiable { var titlekey: String var text: [String] init(titlekey: String, text: [String]) { self.titlekey = titlekey self.text = text } } extension UTType { static var artsheetsDoc = UTType(exportedAs: "com.wannafedor4.artsheetsDoc") } I compiling for my iPhone 13 works, but then when creating a document I get this error: Failed to create document. Error: Error Domain=com.apple.DocumentManager Code=2 "No location available to save “Untitled”." UserInfo={NSLocalizedDescription=No location available to save “Untitled”., NSLocalizedRecoverySuggestion=Enable at least one location to be able to save documents.}
0
0
84
6d
Subject: Assistance Needed: Xcode Suggesting Unnecessary @Transient Backing Data Code for SwiftData Model
Question: Hello, I'm encountering an issue with SwiftData in Xcode. Despite setting up my model classes correctly, Xcode is suggesting additional boilerplate code for handling backing data which I believe should not be necessary. Here are the details: Context: I'm working with SwiftData to persist my data models. I've set up my model container and schema correctly, and I'm only persisting final classes. However, Xcode is suggesting the following code for one of my model classes (LoanAccount): swift Copy code @Transient private var _$backingData: any SwiftData.BackingData = LoanAccount.createBackingData() public var persistentBackingData: any SwiftData.BackingData { get { _$backingData } set { _$backingData = newValue } } static var schemaMetadata: [SwiftData.Schema.PropertyMetadata] { return [ SwiftData.Schema.PropertyMetadata(name: "loanName", keypath: \LoanAccount.loanName, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "outstandingBalance", keypath: \LoanAccount.outstandingBalance, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "currentAssetValue", keypath: \LoanAccount.currentAssetValue, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "securedAssets", keypath: \LoanAccount.securedAssets, defaultValue: [], metadata: nil) ] } required init(backingData: any SwiftData.BackingData) { _loanName = _SwiftDataNoType() _outstandingBalance = _SwiftDataNoType() _currentAssetValue = _SwiftDataNoType() _securedAssets = _SwiftDataNoType() self.persistentBackingData = backingData } @Transient private let _$observationRegistrar = Observation.ObservationRegistrar() struct _SwiftDataNoType { } My Model Setup: Here's a brief overview of my model setup: swift Copy code import Foundation import SwiftData @Model class LoanAccount: LiabilityAccount { var loanName: String var outstandingBalance: Double? var currentAssetValue: Double? var securedAssets: [SecuredAsset] = [] required init( id: UUID = UUID(), institutionName: String, accountName: String, accountBalance: Double = 0, accountOwner: String, country: String = "UK", accountCurrency: String, risk: Int = 1, accountStatus: String = "Active", startDate: Date = Date(), maturityDate: Date = Date(), dateCreated: Date = Date(), dateUpdated: Date = Date(), addressline1: String? = nil, addressline2: String? = nil, county: String? = nil, zipcode: String? = nil, phoneNumber: String? = nil, email: String? = nil, contact1: String? = nil, contact2: String? = nil, link: String? = nil, notes: String? = nil, accountNumber: String? = nil, sortCode: String? = nil, accountFee: Double? = nil, interestRate: Double? = nil, loanName: String, outstandingBalance: Double? = nil, currentAssetValue: Double? = nil, securedAssets: [SecuredAsset] = [], activities: [AccountActivity] = [] ) { self.loanName = loanName self.outstandingBalance = outstandingBalance self.currentAssetValue = currentAssetValue self.securedAssets = securedAssets super.init( id: id, institutionName: institutionName, accountName: accountName, accountType: "Loan", icon: "default_icon", accountOwner: accountOwner, country: country, accountCurrency: accountCurrency, risk: risk, accountStatus: accountStatus, startDate: startDate, maturityDate: maturityDate, dateCreated: dateCreated, dateUpdated: dateUpdated, addressline1: addressline1, addressline2: addressline2, county: county, zipcode: zipcode, phoneNumber: phoneNumber, email: email, contact1: contact1, contact2: contact2, link: link, notes: notes, accountNumber: accountNumber, sortCode: sortCode, accountFee: accountFee, interestRate: interestRate, accountBalance: accountBalance, activities: activities ) } } Issue: Xcode is suggesting that I need to add the @Transient backing data code, even though my understanding is that this should be handled automatically by SwiftData when using the @Model attribute. Request: Can anyone provide insight into why Xcode is suggesting this code and if there's a configuration or setup step I might be missing? I want to ensure my data models are set up correctly without needing unnecessary boilerplate code. Thank you!
0
0
106
6d
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?
1
1
77
6d
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
76
1w
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
112
1w
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
100
1w
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
78
1w
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
85
1w
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
95
1w