Slide 1

Slide 1 text

kishikawa katsumi My SwiftData Review SwiftDataͷධՁʹؔ͢Δࢲݟ

Slide 2

Slide 2 text

What Is SwiftData? SwiftDataͱ͸ʁ • Introduced at WWDC 2023 • Data persistence framework • Works great with SwiftUI

Slide 3

Slide 3 text

Tech Event Manager Sample App

Slide 4

Slide 4 text

Tech Event Manager Sample App

Slide 5

Slide 5 text

Address Book Sample App

Slide 6

Slide 6 text

Schema Definition

Slide 7

Slide 7 text

Display the Data

Slide 8

Slide 8 text

What Is SwiftData? SwiftDataͱ͸ʁ • Introduced at WWDC 2023 • Data persistence framework • Works great with SwiftUI • Core Data wrapper 🔑

Slide 9

Slide 9 text

What Is SwiftData? SwiftDataͱ͸ʁ • Introduced at WWDC 2023 • Data persistence framework • Works great with SwiftUI • Core Data wrapper 🔑

Slide 10

Slide 10 text

SwiftData Is Just Wrapper of Core Data SwiftData͸Core DataͷϥούʔϥΠϒϥϦͰ͋Δ • Not reworked Core Data • Provides a more user-friendly API for working with Core Data in Swift • Schemas de fi ned in code • No separate mapping fi le (*.xcdatamodeld) • Type safe queries • Eliminate string-based Key Paths

Slide 11

Slide 11 text

SwiftData Simplifies the Core Data API Core DataͷAPI͕ΑΓγϯϓϦʹ࢖͍΍͘͢ • NSPersistentContainer => ModelContainer • NSManagedObjectModel => PersistentModel • NSManagedObjectContext => ModelContext • NSPredicate => #Predicate • NSPersistentStoreCoordinator => 🗑 • *.xcdatamodeld => 🗑

Slide 12

Slide 12 text

Also brings legacy issues طଘͷ໰୊఺΋ͦͷ··࢒͍ͬͯΔ

Slide 13

Slide 13 text

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 di ffi cult to control and is a black box
 that causes mysterious errors and crashes.

Slide 14

Slide 14 text

Why argue for and against Core Data? ͳͥCore DataͷධՁ͕෼͔ΕΔͷ͔ • CoreData is not relational database • However, many expect Core Data to be SQLite ORM

Slide 15

Slide 15 text

Core Data Is Not Relational Database (Nor ORM) Core Data͸Relational DatabaseͰ͸ͳ͍ʢORMͰ΋ͳ͍ʣ • CoreData Is Object Graph Manager

Slide 16

Slide 16 text

Misconception that CoreData is not RDB CoreData͸RDBͰ͸ͳ͍ͱ͍͏͜ͱ΁ͷޡղ • Apple just says it for marketing purposes.

Slide 17

Slide 17 text

Misconception that CoreData is not RDB CoreData͸RDBͰ͸ͳ͍ͱ͍͏͜ͱ΁ͷޡղ • Apple just says it for marketing purposes.

Slide 18

Slide 18 text

Misconception that CoreData is not RDB CoreData͸RDBͰ͸ͳ͍ͱ͍͏͜ͱ΁ͷޡղ • 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.

Slide 19

Slide 19 text

Misconception that CoreData is not RDB CoreData͸RDBͰ͸ͳ͍ͱ͍͏͜ͱ΁ͷޡղ • 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.

Slide 20

Slide 20 text

Misconception that CoreData is not RDB CoreData͸RDBͰ͸ͳ͍ͱ͍͏͜ͱ΁ͷޡղ • 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 di ff erent, technically • CoreData is object graph manager • Object graph is groups of objects form a network through their relationships with each other.
 In short, it is a collection of objects.

Slide 21

Slide 21 text

For Relational Database Many to Many Relations Event Attendee

Slide 22

Slide 22 text

For Relational Database Event Attendee Event-Attendee Many to Many Relations

Slide 23

Slide 23 text

For Core Data

Slide 24

Slide 24 text

For Core Data

Slide 25

Slide 25 text

CoreData Is Not Relational Database CoreData Is Object Graph Manager • Many to Many relationships • No foreign key required

Slide 26

Slide 26 text

All Operations Are on Memory

Slide 27

Slide 27 text

All Operations Are on Memory

Slide 28

Slide 28 text

CoreData Is Not Relational Database All Operations Are on Memory

Slide 29

Slide 29 text

CoreData Is Not Relational Database All Operations Are on Memory

Slide 30

Slide 30 text

CoreData Is Not Relational Database Batch Update

Slide 31

Slide 31 text

CoreData Is Not Relational Database Batch Update

Slide 32

Slide 32 text

CoreData Is Not Relational Database Batch Update

Slide 33

Slide 33 text

CoreData Is Not Relational Database Batch Update

Slide 34

Slide 34 text

CoreData Is Not Relational Database Batch Update

Slide 35

Slide 35 text

CoreData Is Not Relational Database Batch Update

Slide 36

Slide 36 text

Available Data Types อଘͰ͖ΔϓϩύςΟͷܕ • Primitive Types • Int, Float, Double, String, Bool • URL, UUID • Codable

Slide 37

Slide 37 text

Codable Composite Type (Core Data)

Slide 38

Slide 38 text

Codable (Composite) Types Are Queryable CodableܕͰอଘͨ͠σʔλΛݕࡧ৚݅ʹͰ͖Δ

Slide 39

Slide 39 text

Codable (Composite) Types Are Queryable CodableܕͰอଘͨ͠σʔλΛݕࡧ৚݅ʹͰ͖Δ

Slide 40

Slide 40 text

Enums Cannot Be Stored Even If Codable Enum͸CodableͰ͋ͬͯ΋อଘͰ͖ͳ͍

Slide 41

Slide 41 text

Available Data Types อଘͰ͖ΔϓϩύςΟͷܕ • Primitive Types • Int, Float, Double, String, Bool • ❌ CGFloat • URL, UUID • Codable • ❌ Enums

Slide 42

Slide 42 text

Auto Save Is Enabled, No Way to Detect Errors Auto Save͕༗ޮͳͱ͖ɺอଘࣦഊΛ஌Δ͢΂͕ͳ͍ • Seems to work correctly, but data is lost on exit • Migration failures silently erase existing data

Slide 43

Slide 43 text

Compiler Is Silent Despite Unsaved Models อଘͰ͖ͳ͍σʔλϞσϧͰ΋ίϯύΠϧ͕௨Δ

Slide 44

Slide 44 text

Compiler silent despite incomplete initialisers ෆ׬શͳΠχγϟϥΠβͰ΋ίϯύΠϧ͕௨Δ (Runtime Error)

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

#Predicate Macro Has Many Unsupported Features #PredicateϚΫϩ͸ະαϙʔτͷػೳ͕ଟ͍

Slide 48

Slide 48 text

#Predicate Macro Has Many Unsupported Features #PredicateϚΫϩ͸ະαϙʔτͷػೳ͕ଟ͍

Slide 49

Slide 49 text

#Predicate Macro Has Many Unsupported Features #PredicateϚΫϩ͸ະαϙʔτͷػೳ͕ଟ͍

Slide 50

Slide 50 text

#Predicate Macro Has Many Unsupported Features #PredicateϚΫϩ͸ະαϙʔτͷػೳ͕ଟ͍

Slide 51

Slide 51 text

#Predicate Macro Has Many Unsupported Features #PredicateϚΫϩ͸ະαϙʔτͷػೳ͕ଟ͍ • Case Insensitive Search • Su ffi x Search • Date formatting and Compare • Max/Min • Composite Unique Constraint

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

Conclusion ݁࿦ • SwiftDataͷ࠾༻͸৻ॏΛཁ͢Δ • Core Dataͷܦݧ͕ඞཁ • ORMΛٻΊ͍ͯΔͳΒORMΛ୳ͦ͏ • ແ੍ݶʹϢʔβʔ͕σʔλΛ࡞੒͢ΔΑ͏ͳ༻్ʹ͸޲͔ͳ͍ • ͍ͭͰ΋σʔλΛফͤΔΩϟογϡͳͲͰࢼͦ͏

Slide 54

Slide 54 text

Tips