|
| 1 | +# Differences to SwiftData |
| 2 | + |
| 3 | +ManagedObjects tries to provide an API that is very close to SwiftData, |
| 4 | +but it is not exactly the same API. |
| 5 | + |
| 6 | +## Differences |
| 7 | + |
| 8 | +The key difference when converting SwiftData projects: |
| 9 | +- Import `ManagedModels` instead of `SwiftData`. |
| 10 | +- Let the models inherit from the CoreData |
| 11 | + [`NSManagedObject`](https://developer.apple.com/documentation/coredata/nsmanagedobject). |
| 12 | +- Use the CoreData |
| 13 | + [`@FetchRequest`](https://developer.apple.com/documentation/swiftui/fetchrequest) |
| 14 | + instead of SwiftData's |
| 15 | + [`@Query`](https://developer.apple.com/documentation/swiftdata/query). |
| 16 | + |
| 17 | + |
| 18 | +### Explicit Superclass |
| 19 | + |
| 20 | +ManagedModels classes must explicitly inherit from the CoreData |
| 21 | +[`NSManagedObject`](https://developer.apple.com/documentation/coredata/nsmanagedobject). |
| 22 | +Instead of just this in SwiftData: |
| 23 | +```swift |
| 24 | +@Model class Contact {} |
| 25 | +``` |
| 26 | +the superclass has to be specified w/ ManagedModels: |
| 27 | +```swift |
| 28 | +@Model class Contact: NSManagedObject {} |
| 29 | +``` |
| 30 | + |
| 31 | +> That is due to a limitation of |
| 32 | +> [Swift Macros](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros/). |
| 33 | +> A macro can add protocol conformances, but it cannot add a superclass to a |
| 34 | +> type. |
| 35 | +
|
| 36 | + |
| 37 | +### CoreData @FetchRequest instead of SwiftData @Query |
| 38 | + |
| 39 | +Instead of using the new SwiftUI |
| 40 | +[`@Query`](https://developer.apple.com/documentation/swiftdata/query) |
| 41 | +wrapper, the already available |
| 42 | +[`@FetchRequest`](https://developer.apple.com/documentation/swiftui/fetchrequest) |
| 43 | +property wrapper is used. |
| 44 | + |
| 45 | +SwiftData: |
| 46 | +```swift |
| 47 | +@Query var contacts : [ Contact ] |
| 48 | +``` |
| 49 | +ManagedModels: |
| 50 | +```swift |
| 51 | +@FetchRequest var contacts: FetchedResults<Contact> |
| 52 | +``` |
| 53 | + |
| 54 | +### Properties |
| 55 | + |
| 56 | +The properties work quite similar. |
| 57 | + |
| 58 | +Like SwiftData, ManagedModels provides implementations of the |
| 59 | +[`@Attribute`](https://developer.apple.com/documentation/swiftdata/attribute(_:originalname:hashmodifier:)), |
| 60 | +`@Relationship` and |
| 61 | +[`@Transient`](https://developer.apple.com/documentation/swiftdata/transient()) |
| 62 | +macros. |
| 63 | + |
| 64 | +#### Compound Properties |
| 65 | + |
| 66 | +More complex Swift types are always stored as JSON by ManagedModels, e.g. |
| 67 | +```swift |
| 68 | +@Model class Person: NSManagedObject { |
| 69 | + |
| 70 | + struct Address: Codable { |
| 71 | + var street : String? |
| 72 | + var city : String? |
| 73 | + } |
| 74 | + |
| 75 | + var privateAddress : Address |
| 76 | + var businessAddress : Address |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +SwiftData decomposes those structures in the database. |
| 81 | + |
| 82 | + |
| 83 | +#### RawRepresentable Properties |
| 84 | + |
| 85 | +Those end up working the same like in SwiftData, but are implemented |
| 86 | +differently. |
| 87 | +If a type is RawRepresentable by a CoreData base type (like `Int` or `String`), |
| 88 | +they get mapped to the same base type in the model. |
| 89 | + |
| 90 | +Example: |
| 91 | +```swift |
| 92 | +enum Color: String { |
| 93 | + case red, green, blue |
| 94 | +} |
| 95 | + |
| 96 | +enum Priority: Int { |
| 97 | + case high = 5, medium = 3, low = 1 |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | + |
| 102 | +### Initializers |
| 103 | + |
| 104 | +A CoreData object has to be initialized through some |
| 105 | +[very specific initializer](https://developer.apple.com/documentation/coredata/nsmanagedobject/1506357-init), |
| 106 | +while a SwiftData model class _must have_ an explicit `init`, |
| 107 | +but is otherwise pretty regular. |
| 108 | + |
| 109 | +The ManagedModels `@Model` macro generates a set of helper inits to deal with |
| 110 | +that. |
| 111 | +But the general recommendation is to use a `convenience init` like so: |
| 112 | +```swift |
| 113 | +convenience init(title: String, age: Int = 50) { |
| 114 | + self.init() |
| 115 | + title = title |
| 116 | + age = age |
| 117 | +} |
| 118 | +``` |
| 119 | +If the own init prefilles _all_ properties (i.e. can be called w/o arguments), |
| 120 | +the default `init` helper is not generated anymore, another one has to be used: |
| 121 | +```swift |
| 122 | +convenience init(title: String = "", age: Int = 50) { |
| 123 | + self.init(context: nil) |
| 124 | + title = title |
| 125 | + age = age |
| 126 | +} |
| 127 | +``` |
| 128 | +The same `init(context:)` can be used to insert into a specific context. |
| 129 | +Often necessary when setting up relationships (to make sure that they |
| 130 | +live in the same |
| 131 | +[`NSManagedObjectContext`](https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext)). |
| 132 | + |
| 133 | + |
| 134 | +### Migration |
| 135 | + |
| 136 | +Regular CoreData migration mechanisms have to be used. |
| 137 | +SwiftData specific migration APIs might be |
| 138 | +[added later](https://github.com/Data-swift/ManagedModels/issues/6). |
| 139 | + |
| 140 | + |
| 141 | +## Implementation Differences |
| 142 | + |
| 143 | +SwiftData completely wraps CoreData and doesn't expose the CoreData APIs. |
| 144 | + |
| 145 | +SwiftData relies on the |
| 146 | +[Observation](https://developer.apple.com/documentation/observation) |
| 147 | +framework (which requires iOS 17+). |
| 148 | +ManagedModels uses CoreData, which makes models conform to |
| 149 | +[ObservableObject](https://developer.apple.com/documentation/combine/observableobject) |
| 150 | +to integrate w/ SwiftUI. |
0 commit comments