In a lab this morning, it was suggested I look into SQLite. I liked the presentation on SwiftUI (WWDC20-10119) and was wondering if it would be possible to use SQLite as the model for data for a SwiftUI project (for the Sandwiches information in the model that was glossed over quickly in the presentation)?
Can SQLite be used with SwiftUI?
I don’t have much experience with SQLite directly on iOS, but if you’re referring to SQLite for persistence of a model, maybe CoreData is a way to do it given that it’s a framework provided by Apple itself. I haven’t tried it myself, and I usually use Realm for persistence. I am very curious how Realm’s reactive approach works with Swift UI’s observers.
Yes you can! Both Core Data and SQLite work within SwiftUI.
With respect to Core Data, if you create a new project in Xcode and select either the NSApplication Delegate or UIApplication Delegate life cycle, a “Use Core Data” checkbox will be available. Selecting this checkbox will cause additional code to be added to the new project to initialize a Core Data persistence stack and propagate an NSManagedObjectContext through the application for your UI code to use.
As for SQLite, it’s a lower-level API so there’ll be more work involved in deciding how best to wire it into and use it from your application, and there’s no provided templates demonstrating how to do this since most developers’ persistence needs should be well-addressed by Core Data.
With respect to Core Data, if you create a new project in Xcode and select either the NSApplication Delegate or UIApplication Delegate life cycle, a “Use Core Data” checkbox will be available. Selecting this checkbox will cause additional code to be added to the new project to initialize a Core Data persistence stack and propagate an NSManagedObjectContext through the application for your UI code to use.
As for SQLite, it’s a lower-level API so there’ll be more work involved in deciding how best to wire it into and use it from your application, and there’s no provided templates demonstrating how to do this since most developers’ persistence needs should be well-addressed by Core Data.