main code repository.
Revision | 2ce69aad339dfed0000450e43e6a9f5169b97ccc (tree) |
---|---|
Time | 2018-03-18 10:40:33 |
Author | masakih <masakih@user...> |
Commiter | masakih |
処理を簡素にした
@@ -83,61 +83,50 @@ extension CoreDataProvider { | ||
83 | 83 | |
84 | 84 | func save() throws { |
85 | 85 | |
86 | - var caughtError: Error? | |
87 | - context.performAndWait { | |
88 | - | |
89 | - guard context.commitEditing() else { | |
90 | - | |
91 | - caughtError = CoreDataError.couldNotSave("\(String(describing: type(of: self))) unable to commit editing before saveing") | |
92 | - return | |
93 | - } | |
86 | + // parentを辿ってsaveしていく | |
87 | + func propagateSaveAsync(_ context: NSManagedObjectContext) { | |
94 | 88 | |
95 | - do { | |
89 | + guard let parent = context.parent else { | |
96 | 90 | |
97 | - try context.save() | |
98 | - | |
99 | - } catch let error as NSError { | |
100 | - | |
101 | - caughtError = CoreDataError.couldNotSave(error.localizedDescription) | |
102 | 91 | return |
103 | 92 | } |
104 | 93 | |
105 | - guard let reader = context.parent else { return } | |
106 | - | |
107 | - // save parent context | |
108 | - var catchedError: NSError? = nil | |
109 | - reader.performAndWait { | |
94 | + parent.perform { | |
110 | 95 | |
111 | 96 | do { |
112 | 97 | |
113 | - try reader.save() | |
98 | + try parent.save() | |
114 | 99 | |
115 | - guard let writer = reader.parent else { | |
116 | - | |
117 | - throw CoreDataError.couldNotSave("Could not get writer context.") | |
118 | - } | |
100 | + propagateSaveAsync(parent) | |
119 | 101 | |
120 | - writer.performAndWait { | |
121 | - | |
122 | - do { | |
123 | - | |
124 | - try writer.save() | |
125 | - | |
126 | - } catch let error as NSError { | |
127 | - | |
128 | - catchedError = error | |
129 | - } | |
130 | - } | |
102 | + } catch { | |
131 | 103 | |
132 | - } catch let error as NSError { | |
133 | - | |
134 | - catchedError = error | |
104 | + Logger.shared.log("Could not save context as \(error)") | |
135 | 105 | } |
136 | 106 | } |
107 | + } | |
108 | + | |
109 | + var caughtError: Error? | |
110 | + context.performAndWait { | |
137 | 111 | |
138 | - if let error = catchedError { | |
112 | + guard context.commitEditing() else { | |
113 | + | |
114 | + caughtError = CoreDataError.couldNotSave("Unable to commit editing before saveing") | |
115 | + return | |
116 | + } | |
117 | + | |
118 | + do { | |
119 | + | |
120 | + try context.save() | |
121 | + | |
122 | + // save reader and writer context async. | |
123 | + // non throw exceptions. | |
124 | + propagateSaveAsync(context) | |
125 | + | |
126 | + } catch let error as NSError { | |
139 | 127 | |
140 | 128 | caughtError = CoreDataError.couldNotSave(error.localizedDescription) |
129 | + return | |
141 | 130 | } |
142 | 131 | } |
143 | 132 |