My SwiftData Review SwiftDataの評価に関する私見
kishikawa katsumiMy SwiftData ReviewSwiftDataͷධՁʹؔ͢Δࢲݟ
View Slide
What Is SwiftData?SwiftDataͱʁ• Introduced at WWDC 2023• Data persistence framework• Works great with SwiftUI
Tech Event Manager Sample App
Address Book Sample App
Schema Definition
Display the Data
What Is SwiftData?SwiftDataͱʁ• Introduced at WWDC 2023• Data persistence framework• Works great with SwiftUI• Core Data wrapper 🔑
SwiftData Is Just Wrapper of Core DataSwiftDataCore DataͷϥούʔϥΠϒϥϦͰ͋Δ• Not reworked Core Data• Provides a more user-friendly API for working with Core Data in Swift• Schemas defined in code• No separate mappingfile (*.xcdatamodeld)• Type safe queries• Eliminate string-based Key Paths
SwiftData Simplifies the Core Data APICore DataͷAPI͕ΑΓγϯϓϦʹ͍͘͢• NSPersistentContainer => ModelContainer• NSManagedObjectModel => PersistentModel• NSManagedObjectContext => ModelContext• NSPredicate => #Predicate• NSPersistentStoreCoordinator => 🗑• *.xcdatamodeld => 🗑
Also brings legacy issuesطଘͷͦͷ··͍ͬͯΔ
The issue is Core Data݁ہͷͱ͜Ζɺ૪Core Data• For those familiar with Core Data,• Core Data is proven, stable, simple and convenient.• For those unfamiliar with Core Data,• Core Data is difficult to control and is a black box that causes mysterious errors and crashes.
Why argue for and against Core Data?ͳͥCore DataͷධՁ͕͔ΕΔͷ͔• CoreData is not relational database• However, many expect Core Data to be SQLite ORM
Core Data Is Not Relational Database (Nor ORM)Core DataRelational DatabaseͰͳ͍ʢORMͰͳ͍ʣ• CoreData Is Object Graph Manager
Misconception that CoreData is not RDBCoreDataRDBͰͳ͍ͱ͍͏͜ͱͷޡղ• Apple just says it for marketing purposes.
Misconception that CoreData is not RDBCoreDataRDBͰͳ͍ͱ͍͏͜ͱͷޡղ• Apple just says it for marketing purposes• Support for data stores other than SQLite (Binary, XML, In-Memory)• SwiftData supports SQLite only. Therefore, it is a wrapper for SQLite.
Misconception that CoreData is not RDBCoreDataRDBͰͳ͍ͱ͍͏͜ͱͷޡղ• Apple just says it for marketing purposes• Support for data stores other than SQLite (Binary, XML, In-Memory)• SwiftData supports SQLite only. Therefore, it is a wrapper for SQLite.• Completely different, technically• CoreData is object graph manager• Object graph is groups of objects form a network through their relationshipswith each other. In short, it is a collection of objects.
For Relational DatabaseMany to Many RelationsEvent Attendee
For Relational DatabaseEvent AttendeeEvent-AttendeeMany to Many Relations
For Core Data
CoreData Is Not Relational DatabaseCoreData Is Object Graph Manager• Many to Many relationships• No foreign key required
All Operations Are on Memory
CoreData Is Not Relational DatabaseAll Operations Are on Memory
CoreData Is Not Relational DatabaseBatch Update
Available Data TypesอଘͰ͖ΔϓϩύςΟͷܕ• Primitive Types• Int, Float, Double, String, Bool• URL, UUID• Codable
CodableComposite Type (Core Data)
Codable (Composite) Types Are QueryableCodableܕͰอଘͨ͠σʔλΛݕࡧ݅ʹͰ͖Δ
Enums Cannot Be Stored Even If CodableEnumCodableͰ͋ͬͯอଘͰ͖ͳ͍
Available Data TypesอଘͰ͖ΔϓϩύςΟͷܕ• Primitive Types• Int, Float, Double, String, Bool• ❌ CGFloat• URL, UUID• Codable• ❌ Enums
Auto Save Is Enabled, No Way to Detect ErrorsAuto Save͕༗ޮͳͱ͖ɺอଘࣦഊΛΔ͕͢ͳ͍• Seems to work correctly, but data is lost on exit• Migration failures silently erase existing data
Compiler Is Silent Despite Unsaved ModelsอଘͰ͖ͳ͍σʔλϞσϧͰίϯύΠϧ͕௨Δ
Compiler silent despite incomplete initialisersෆશͳΠχγϟϥΠβͰίϯύΠϧ͕௨Δ (Runtime Error)
#Predicate Macro Has Many Unsupported Features#PredicateϚΫϩະαϙʔτͷػೳ͕ଟ͍
#Predicate Macro Has Many Unsupported Features#PredicateϚΫϩະαϙʔτͷػೳ͕ଟ͍• Case Insensitive Search• Suffix Search• Date formatting and Compare• Max/Min• Composite Unique Constraint
Is SwiftData Production Ready?SwiftData࣮༻Մೳ͔ʁ• Depends on how familiar you are with Core Data.• No Fine-Grained CoreData Controls• No Faulting• No Parent/Child Context• No Merge Policy• SwiftData Itself Is Also Incomplete
Conclusion݁• SwiftDataͷ࠾༻৻ॏΛཁ͢Δ• Core Dataͷܦݧ͕ඞཁ• ORMΛٻΊ͍ͯΔͳΒORMΛ୳ͦ͏• ແ੍ݶʹϢʔβʔ͕σʔλΛ࡞͢ΔΑ͏ͳ༻్ʹ͔ͳ͍• ͍ͭͰσʔλΛফͤΔΩϟογϡͳͲͰࢼͦ͏
Tips