Skip to content

Commit fb70830

Browse files
authored
Update README.md
1 parent 20cea52 commit fb70830

File tree

1 file changed

+1
-242
lines changed

1 file changed

+1
-242
lines changed

README.md

Lines changed: 1 addition & 242 deletions
Original file line numberDiff line numberDiff line change
@@ -13,250 +13,9 @@ Lightweight Active-record-ish pattern.
1313

1414
[Go to documentation](https://coredataplus.readme.io/)
1515

16-
***
17-
18-
# Installation
19-
20-
```swift
21-
import CoreDataPlus
22-
23-
// An existing NSManagedObject
24-
public class Drawing: NSManagedObject { }
25-
26-
extension Drawing: ManagedObjectPlus {
27-
28-
}
29-
```
30-
31-
You can also import just the features you want to use by replacing `ManagedObjectPlus` with one or more of the available `ManagedObject*` protocols:
32-
33-
```swift
34-
extension Drawing: ManagedObjectDeletable,
35-
ManagedObjectCountable,
36-
ManagedObjectSearchable,
37-
ManagedObjectFindOrCreateBy {
38-
39-
}
40-
```
41-
42-
# Example App
43-
44-
Open `Example App/Example App.xcodeproj`.
45-
46-
<img src="https://user-images.githubusercontent.com/11250/207498227-0009e1e4-4201-449a-b205-d9b9cae78f89.png" height="200" />
47-
48-
# Documentation
49-
50-
## `Predicate`
51-
52-
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.
73-
>
74-
> Before: `@FetchRequest(sortDescriptors: [], predicate: nil)`
75-
> After: `@FetchRequest(sortDescriptors: [], predicate: Predicate.empty())`
76-
77-
78-
79-
80-
## `NSManagedObject` Extensions
81-
### `findOrCreate`
82-
83-
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-
if let 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-
import SwiftUI
171-
172-
@main
173-
struct MyFancyApp: App {
174-
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
175-
176-
var body: some Scene {
177-
WindowGroup {
178-
ContentView()
179-
}
180-
}
181-
}
182-
183-
184-
// A new file called AppDelegate.swift
185-
import Foundation
186-
import UIKit
187-
import CoreDataPlus
188-
189-
class AppDelegate: NSObject, UIApplicationDelegate {
190-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
191-
192-
CoreDataPlus.Logger.configure(logHandler: { message in
193-
print("A log message from the data layer: \(message)")
194-
})
195-
196-
return true
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-
import SwiftUI
221-
222-
@main
223-
struct MyFancyApp: App {
224-
let persistenceController = PersistenceController.shared // from the default Xcode new project setup
225-
226-
var body: some Scene {
227-
WindowGroup {
228-
ContentView()
229-
.environment(\.managedObjectContext, persistenceController.container.viewContext)
230-
}
231-
}
232-
}
233-
234-
```
235-
236-
ContentView file:
237-
238-
```swift
239-
import SwiftUI
240-
import CoreData
241-
import CoreDataPlus
242-
243-
struct ContentView: View {
244-
@Environment(\.managedObjectContext) private var viewContext
245-
246-
var body: some View {
247-
VStack {
248-
Button("Hello", action: {
249-
Drawing.destroyAll(context: self.viewContext)
250-
})
251-
}
252-
}
253-
}
254-
```
255-
256-
25716
## Todo
25817
- [x] Unit tests
25918
- [x] Option to disable automatic saving when using `destroyAll`
260-
- [ ] Add docs on `NSPersistentContainer` agnosticity
19+
- [x] Add docs on `NSPersistentContainer` agnosticity
26120
- [ ] Have an idea? Open an issue!
26221

0 commit comments

Comments
 (0)