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
{{ message }}
This repository was archived by the owner on Feb 8, 2023. It is now read-only.
@@ -33,32 +33,35 @@ For CoreDataKit to be able to use your NSManagedObject subclass, such as Car in
33
33
```swift
34
34
classCar: NSManagedObject, NamedManagedObject {
35
35
36
-
staticvar entityName ="Car"//corresponding to your Enity name in your xcdatamodeld
36
+
staticvar entityName ="Car"//corresponding to your Entity name in your xcdatamodeld
37
37
38
38
@NSManagedvar color: String
39
39
@NSManagedvar model: String
40
-
41
40
}
42
41
43
42
```
44
43
45
44
From here you are able to use the shared stack. For example to create and save an entity, this example performs a block an a background context, saves it to the persistent store and executes a completion handler:
46
45
```swift
47
46
CDK.performBlockOnBackgroundContext({ context in
48
-
iflet car = context.create(Car.self).value() {
49
-
car.color="Hammerhead Silver"
50
-
car.model="Aston Martin DB9"
51
-
}
52
-
53
-
return .SaveToPersistentStore
54
-
}, completionHandler: { result, _in
55
-
switch result {
56
-
case .Success:
57
-
println("Car saved, time to update the interface!")
58
-
59
-
caselet .Failure(error):
60
-
println("Saving Harvey Specters car failed with error: \(error)")
61
-
}
47
+
do {
48
+
let car =try context.create(Car.self)
49
+
car.color="Hammerhead Silver"
50
+
car.model="Aston Martin DB9"
51
+
52
+
return .SaveToPersistentStore
53
+
}
54
+
catch {
55
+
return .DoNothing
56
+
}
57
+
}, completionHandler: { result in
58
+
do {
59
+
tryresult()
60
+
print("Car saved, time to update the interface!")
61
+
}
62
+
catch {
63
+
print("Saving Harvey Specters car failed with error: \(error)")
64
+
}
62
65
})
63
66
```
64
67
@@ -67,19 +70,25 @@ CDK.performBlockOnBackgroundContext({ context in
67
70
If you prefer using promises, instead of the callback style of this library, you can use the [Promissum](https://github.com/tomlokhorst/Promissum) library with CoreDataKit. Using the [CoreDataKit+Promise](https://github.com/tomlokhorst/Promissum/blob/develop/extensions/PromissumExtensions/CoreDataKit%2BPromise.swift) extension, the example from above can be rewritten as such:
68
71
```swift
69
72
let createPromise = CDK.performBlockOnBackgroundContextPromise { context in
70
-
iflet car = context.create(Car.self).value() {
71
-
car.color="Hammerhead Silver"
72
-
car.model="Aston Martin DB9"
73
-
}
74
-
75
-
return .SaveToPersistentStore
73
+
do {
74
+
let car =try context.create(Car.self)
75
+
car.color="Hammerhead Silver"
76
+
car.model="Aston Martin DB9"
77
+
78
+
return .SaveToPersistentStore
79
+
}
80
+
catch {
81
+
return .DoNothing
82
+
}
76
83
}
77
84
78
-
createPromise.then { _in
79
-
println("Car saved, time to update the interface!")
80
-
}.catch { error in
81
-
println("Saving Harvey Specters car failed with error: \(error)")
82
-
}
85
+
createPromise
86
+
.then { _in
87
+
print("Car saved, time to update the interface!")
88
+
}
89
+
.trap { error in
90
+
print("Saving Harvey Specters car failed with error: \(error)")
0 commit comments