I have been working on an app and using the SwiftData Model with the @Model macro. I noticed a while back that it doesn’t support the CKAsset
data type b/c it does not conform to the PersistentModel protocol. I’m also assuming that’s b/c it does not conform to the Codable protocol.
I was hoping to use CKAsset
as my data type b/c I’m uploading media; images, and movies. I’m using the photo picker to store and save media.
imageData = try? await item.loadTransferable(type: Data.self)
e.g. I save the value of imageData to my model to the record asset.
What I did instead was to use the data type Data
. So far during development, this has seemed to work ok. Here is my model: (P.S. I’m not sure if importing CloudKit here is necessary yet or not.)
import SwiftUI
import SwiftData
import CloudKit
@Model
class Media: Identifiable {
var id: UUID = UUID()
var albumID: UUID = UUID()
var asset: Data? = nil
var isCover: Bool = false
var contentType: String = ""
var timeStamp: Date = Date()
var album: Album?
init(albumID: UUID = UUID(), asset: Data? = nil, isCover: Bool = false, contentType: String = "", timeStamp: Date = Date()) {
self.albumID = albumID
self.asset = asset
self.isCover = isCover
self.contentType = contentType
self.timeStamp = timeStamp
}
}
I noticed last night all of the sudden it stopped using my defined asset
in my model and created a new record in CloudKit called asset_CKAsset
. Is this an issue or is Cloudkit just handling that conversion for me from Data
to CKSsset
? I wanted to make sure my app continues to work as intended, there aren’t any limitations to using a data type of Data
for this that I’m not aware of, etc.
Any thoughts or recommendations would be greatly appreciated.