Upgrade to Pro — share decks privately, control downloads, hide ads and more …

My SwiftData Review

My SwiftData Review

My SwiftData Review
SwiftDataの評価に関する私見

Kishikawa Katsumi

July 14, 2023
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

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

    View Slide

  2. What Is SwiftData?
    SwiftDataͱ͸ʁ
    • Introduced at WWDC 2023

    • Data persistence framework

    • Works great with SwiftUI

    View Slide

  3. Tech Event Manager Sample App

    View Slide

  4. Tech Event Manager Sample App

    View Slide

  5. Address Book Sample App

    View Slide

  6. Schema Definition

    View Slide

  7. Display the Data

    View Slide

  8. What Is SwiftData?
    SwiftDataͱ͸ʁ
    • Introduced at WWDC 2023

    • Data persistence framework

    • Works great with SwiftUI

    • Core Data wrapper 🔑

    View Slide

  9. What Is SwiftData?
    SwiftDataͱ͸ʁ
    • Introduced at WWDC 2023

    • Data persistence framework

    • Works great with SwiftUI

    • Core Data wrapper 🔑

    View Slide

  10. 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

    View Slide

  11. SwiftData Simplifies the Core Data API
    Core DataͷAPI͕ΑΓγϯϓϦʹ࢖͍΍͘͢
    • NSPersistentContainer => ModelContainer

    • NSManagedObjectModel => PersistentModel

    • NSManagedObjectContext => ModelContext

    • NSPredicate => #Predicate

    • NSPersistentStoreCoordinator => 🗑

    • *.xcdatamodeld => 🗑

    View Slide

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

    View Slide

  13. 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.

    View Slide

  14. Why argue for and against Core Data?
    ͳͥCore DataͷධՁ͕෼͔ΕΔͷ͔
    • CoreData is not relational database

    • However, many expect Core Data to be SQLite ORM

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  18. 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.

    View Slide

  19. 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.

    View Slide

  20. 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.

    View Slide

  21. For Relational Database
    Many to Many Relations
    Event Attendee

    View Slide

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

    View Slide

  23. For Core Data

    View Slide

  24. For Core Data

    View Slide

  25. CoreData Is Not Relational Database
    CoreData Is Object Graph Manager
    • Many to Many relationships

    • No foreign key required

    View Slide

  26. All Operations Are on Memory

    View Slide

  27. All Operations Are on Memory

    View Slide

  28. CoreData Is Not Relational Database
    All Operations Are on Memory

    View Slide

  29. CoreData Is Not Relational Database
    All Operations Are on Memory

    View Slide

  30. CoreData Is Not Relational Database
    Batch Update

    View Slide

  31. CoreData Is Not Relational Database
    Batch Update

    View Slide

  32. CoreData Is Not Relational Database
    Batch Update

    View Slide

  33. CoreData Is Not Relational Database
    Batch Update

    View Slide

  34. CoreData Is Not Relational Database
    Batch Update

    View Slide

  35. CoreData Is Not Relational Database
    Batch Update

    View Slide

  36. Available Data Types
    อଘͰ͖ΔϓϩύςΟͷܕ
    • Primitive Types

    • Int, Float, Double, String, Bool

    • URL, UUID

    • Codable

    View Slide

  37. Codable
    Composite Type (Core Data)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  41. Available Data Types
    อଘͰ͖ΔϓϩύςΟͷܕ
    • Primitive Types

    • Int, Float, Double, String, Bool

    • ❌ CGFloat

    • URL, UUID

    • Codable

    • ❌ Enums

    View Slide

  42. 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

    View Slide

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

    View Slide

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

    View Slide

  45. View Slide

  46. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  51. #Predicate Macro Has Many Unsupported Features
    #PredicateϚΫϩ͸ະαϙʔτͷػೳ͕ଟ͍
    • Case Insensitive Search

    • Su
    ffi
    x Search

    • Date formatting and Compare

    • Max/Min

    • Composite Unique Constraint

    View Slide

  52. 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

    View Slide

  53. Conclusion
    ݁࿦
    • SwiftDataͷ࠾༻͸৻ॏΛཁ͢Δ

    • Core Dataͷܦݧ͕ඞཁ

    • ORMΛٻΊ͍ͯΔͳΒORMΛ୳ͦ͏

    • ແ੍ݶʹϢʔβʔ͕σʔλΛ࡞੒͢ΔΑ͏ͳ༻్ʹ͸޲͔ͳ͍

    • ͍ͭͰ΋σʔλΛফͤΔΩϟογϡͳͲͰࢼͦ͏

    View Slide

  54. Tips

    View Slide