You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds a new, more concise, initializer for `NSPredicate`. Aliased as `Predicate`.
53
-
54
-
You can type
55
-
56
-
```swift
57
-
Predicate("color = %@ and city = %@", "Blue", city)
58
-
```
59
-
60
-
Instead of
61
-
62
-
```swift
63
-
NSPredicate(format:"color = %@ and city = %@", argumentArray:["Blue", city])
64
-
```
65
-
66
-
### `empty()`
67
-
68
-
Returns a new, non-nil, `NSPredicate` object that will match every single row in the database.
69
-
70
-
> This is especially helpful with:
71
-
> 1. The [@FetchRequest property wrapper](https://developer.apple.com/documentation/swiftui/fetchrequest). Using a nil predicate can cause unexpected behavior if you dynamically assign or edit the predicate at runtime.
72
-
> 2. Other Core Data quirks around nil predicates and updating when you do fancy stuff.
Finds an instance of the `NSManagedObject` that has a column (property) matching the passed value. If it doesn't exist in the database, creates one, and returns it.
84
-
85
-
```swift
86
-
let d = Drawing.findOrCreate(column: "my-column", value: "123456789", context: viewContext)
87
-
d.timestamp=Date()
88
-
```
89
-
90
-
The idea is that each object has a unique, deterministic, human-readable, identifier. So if you have a `User` with an `id` of `123`, you can do `let user = User.findOrCreate(id: "123")` whenever and wherever you need access to the user object.
91
-
92
-
#### Shortcut:
93
-
If your xcdatamodel has a `String` property called `id`, you can use this shortcut:
94
-
95
-
```swift
96
-
let d = Drawing.findOrCreate(id: "123", context: viewContext)
97
-
```
98
-
99
-
*Tip: If you're using SwiftUI, adding a `String` column `id` plays nicely with `Identifiable`*
100
-
101
-
102
-
### `findButDoNotCreate`
103
-
104
-
Same as `findOrCreate`, but returns `nil` if there is no object in the database.
105
-
106
-
```swift
107
-
iflet d = Drawing.findButDoNotCreate(id: "10001", context: viewContext) {
108
-
d.timestamp=Date()
109
-
d.title="My title"
110
-
}
111
-
```
112
-
113
-
### `countFor`
114
-
115
-
Gets a count of objects matching the passed `Predicate`
116
-
117
-
```swift
118
-
let count = Drawing.countFor(Predicate("someField = %@", true), context: viewContext)
119
-
```
120
-
121
-
### `searchFor`
122
-
123
-
Gets all objects matching the passed `Predicate`
124
-
125
-
```swift
126
-
let results = Drawing.searchFor(Predicate("foo = %@", "bar"), context: viewContext)
127
-
128
-
print("\(results.count) objects found")
129
-
130
-
for drawing in results {
131
-
dump(drawing)
132
-
}
133
-
```
134
-
135
-
136
-
### `destroyAll`
137
-
138
-
Removes all objects from Core Data.
139
-
*Deletes them from the context.*
140
-
141
-
```swift
142
-
Drawing.destroyAll(context: viewContext)
143
-
```
144
-
145
-
146
-
You can also specify a predicate to only delete some items:
147
-
```swift
148
-
let withSpanishLanguage =Predicate("languages contains %@", es)
149
-
Country.destroyAll(matching: withSpanishLanguage)
150
-
```
151
-
152
-
Remember to save the context after deleting objects.
153
-
154
-
155
-
156
-
## Logging
157
-
158
-
By default, log messages do not appear. To take action when there's a problem, use `CoreDataPlus.Logger.configure(...)`
159
-
160
-
```swift
161
-
CoreDataPlus.Logger.configure(logHandler: { message in
162
-
print("A log message from the data layer: \(message)")
163
-
})
164
-
```
165
-
166
-
*Recommended: set the `logHandler` in your AppDelegate*. Example below:
167
-
168
-
```swift
169
-
// Your app's main .swift file:
170
-
importSwiftUI
171
-
172
-
@main
173
-
structMyFancyApp: App {
174
-
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
CoreDataPlus.Logger.configure(logHandler: { message in
193
-
print("A log message from the data layer: \(message)")
194
-
})
195
-
196
-
returntrue
197
-
}
198
-
}
199
-
```
200
-
201
-
202
-
## Automatic Context Management
203
-
204
-
Optional. If you specify a view context and a background context using `CoreDataPlus.setup(...)`, you can simplify passing of `NSManagedObjectContext` objects.
205
-
206
-
For example, `Book.findOrCreate(id:"123", context: foo)` becomes `Book.findOrCreate(id:"123")` and `Author.findOrCreate(id:"123", context: bar)` becomes `Author.findOrCreate(id:"123", using: .background)`. See the included example app for more examples.
207
-
208
-
209
-
210
-
## Quick Start with SwiftUI
211
-
212
-
You can pass the `viewContext` variable as the `context` for all this library's methods.
213
-
214
-
Most SwiftUI quickstart guides use `.environment(\.managedObjectContext, foo)` on the view, along with
215
-
216
-
217
-
Main app file:
218
-
219
-
```swift
220
-
importSwiftUI
221
-
222
-
@main
223
-
structMyFancyApp: App {
224
-
let persistenceController = PersistenceController.shared// from the default Xcode new project setup
0 commit comments