ETA: Apologies! SwiftData like SwiftUI is probably better to take over to the apple dev forums! Got thrown by the name there for second.
New to SwiftData, but I was thinking of adding it to a small project as a persistence layer. The existing persistence layer is actually custom text files saved to the bundle and I was about to rewrite that anyway to sync across devices.
SwiftData looks like its at its best when it's baked in from the beginning to a SwiftUI project and you treat the newish (WWDC 2024 Create a custom data store) DataStore
as your PersistanceService-swappable item and not all of SwiftData itself. I want to acknowledge that from the beginning. It was crazy fast to get some demo projects working with relational data. Thumbs up.
That said, I am considering bolting it in to something existing, and since part of the point of using SwiftData is to take advantage of the almost turnkey CloudKit-Sync-Accross-Devices aspect I'm stuck with Optionals/Default Values which will be a real pain to integrate into existing code. I am tempted to do something along the lines of:
//all the things: Codeable, Sendable, Hashable, Identifiable, Equatable
struct ExistingStruct {
let myNumber:Int
let myText:String
let myTime:Date
//etc, etc... all codeable/sendable/hashable, no collections.
func someLogic(with someX:Int) -> String { ... }
//etc
}
@Model
//TODO: actor?
final class ModelWrappedExistingStruct {
var wrappedStruct:ExistingStruct?
//some convenience accessors to the underlying struct as needed
}
leaving all of my existing code that interacts with the struct interacting with one held by the class or an explicit copy of it.
I've implemented a little bit of that in a demo project and while I have to get fancy with some of the SwiftUI @Bindable code it does seem to work to edit, add and delete items across devices in a simplistic project.
My question to the SwiftData heads out there is this a known valid approach given how straightforward the struct is or are there easily foreseeable problems?