main code repository.
Revision | bffee1debad5eda2a130353ba091b22564716d54 (tree) |
---|---|
Time | 2018-03-23 22:06:50 |
Author | masakih <masakih@user...> |
Commiter | masakih |
NSManagedObjectIDを明に使用しないように変更
@@ -154,8 +154,7 @@ extension BookmarkListViewController: NSTableViewDelegate, NSTableViewDataSource | ||
154 | 154 | |
155 | 155 | guard let data = $0.element.data(forType: .bookmarkItem) else { return } |
156 | 156 | guard let uri = NSKeyedUnarchiver.unarchiveObject(with: data) as? URL else { return } |
157 | - guard let oID = self.managedObjectContext.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: uri) else { return } | |
158 | - guard let bookmark = store.object(of: Bookmark.entity, with: oID) else { return } | |
157 | + guard let bookmark = store.object(of: Bookmark.entity, forURIRepresentation: uri) else { return } | |
159 | 158 | |
160 | 159 | bookmark.order = targetOrder + $0.offset + 1 |
161 | 160 | } |
@@ -41,10 +41,15 @@ protocol CoreDataAccessor: CoreDataProvider { | ||
41 | 41 | |
42 | 42 | func insertNewObject<T>(for entity: Entity<T>) -> T? |
43 | 43 | func delete(_ object: NSManagedObject) |
44 | - func object<T>(of entity: Entity<T>, with objectId: NSManagedObjectID) -> T? | |
45 | 44 | func objects<T>(of entity: Entity<T>, sortDescriptors: [NSSortDescriptor]?, predicate: NSPredicate?) throws -> [T] |
46 | 45 | |
47 | - func object(with objectId: NSManagedObjectID) -> NSManagedObject | |
46 | + func object<T>(of entity: Entity<T>, forURIRepresentation: URL) -> T? | |
47 | + | |
48 | + /// NSManagedObectを自身が管理するNSManagedObjectに変換する | |
49 | + /// | |
50 | + /// - Parameter object: 変換するNSManagedObject | |
51 | + /// - Returns: 自身が管理するNSManagedObject。自身の管理下に該当オブジェクトがなければnilを返す。 | |
52 | + func exchange<T: NSManagedObject>(_ object: T) -> T? | |
48 | 53 | } |
49 | 54 | |
50 | 55 | protocol CoreDataManager: CoreDataAccessor { |
@@ -161,11 +166,6 @@ extension CoreDataAccessor { | ||
161 | 166 | context.delete(object) |
162 | 167 | } |
163 | 168 | |
164 | - func object<T>(of entity: Entity<T>, with objectId: NSManagedObjectID) -> T? { | |
165 | - | |
166 | - return context.object(with: objectId) as? T | |
167 | - } | |
168 | - | |
169 | 169 | func objects<T>(of entity: Entity<T>, sortDescriptors: [NSSortDescriptor]? = nil, predicate: NSPredicate? = nil) throws -> [T] { |
170 | 170 | |
171 | 171 | let req = NSFetchRequest<T>(entityName: entity.name) |
@@ -191,13 +191,24 @@ extension CoreDataAccessor { | ||
191 | 191 | return result ?? [] |
192 | 192 | } |
193 | 193 | |
194 | - func object(with objectId: NSManagedObjectID) -> NSManagedObject { | |
194 | + func object<T>(of entity: Entity<T>, forURIRepresentation uri: URL) -> T? { | |
195 | + | |
196 | + guard let oID = context.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: uri) else { return nil } | |
197 | + | |
198 | + var result: NSManagedObject? | |
199 | + sync { | |
200 | + result = self.context.object(with: oID) | |
201 | + } | |
202 | + return result as? T | |
203 | + } | |
204 | + | |
205 | + func exchange<T: NSManagedObject>(_ obj: T) -> T? { | |
195 | 206 | |
196 | 207 | var result: NSManagedObject? |
197 | 208 | sync { |
198 | - result = self.context.object(with: objectId) | |
209 | + result = self.context.object(with: obj.objectID) | |
199 | 210 | } |
200 | - return result! | |
211 | + return result as? T | |
201 | 212 | } |
202 | 213 | } |
203 | 214 |
@@ -51,8 +51,7 @@ class HistoryTableViewController: NSViewController { | ||
51 | 51 | let selectedIndex = controller.selectionIndex |
52 | 52 | store.sync { |
53 | 53 | selection |
54 | - .map { $0.objectID } | |
55 | - .map(store.object(with:)) | |
54 | + .flatMap(store.exchange) | |
56 | 55 | .forEach(store.delete) |
57 | 56 | } |
58 | 57 |
@@ -51,7 +51,8 @@ final class MasterShipMapper: JSONMapper { | ||
51 | 51 | return Logger.shared.log("MasterShipMapper: Can not find MasterSType") |
52 | 52 | } |
53 | 53 | |
54 | - guard let masterSType = configuration.editorStore.object(of: MasterSType.entity, with: stype.objectID) else { | |
54 | + // FUCK: 型推論がバカなのでダウンキャストしてるんだ!!! | |
55 | + guard let masterSType = configuration.editorStore.exchange(stype) as? MasterSType else { | |
55 | 56 | |
56 | 57 | return Logger.shared.log("MasterShipMapper: Can not convert to current moc object masterSType") |
57 | 58 | } |
@@ -169,7 +169,7 @@ final class ShipMapper: JSONMapper { | ||
169 | 169 | private func setMaster(_ masterId: Int, to ship: Ship) { |
170 | 170 | |
171 | 171 | guard let mShip = masterShips.binarySearch(comparator: { $0.id ==? masterId }), |
172 | - let masterShip = store?.object(of: MasterShip.entity, with: mShip.objectID) else { | |
172 | + let masterShip = store?.exchange(mShip) else { | |
173 | 173 | |
174 | 174 | return Logger.shared.log("Can not convert to current moc object masterShip") |
175 | 175 | } |
@@ -188,7 +188,7 @@ final class ShipMapper: JSONMapper { | ||
188 | 188 | .flatMap { item in |
189 | 189 | |
190 | 190 | guard let found = self.slotItems.binarySearch(comparator: { $0.id ==? item }), |
191 | - let slotItem = store.object(of: SlotItem.entity, with: found.objectID) else { | |
191 | + let slotItem = store.exchange(found) else { | |
192 | 192 | |
193 | 193 | let maxV = convertedSlotItems.last |
194 | 194 | if maxV != nil, maxV! < item { |
@@ -213,7 +213,7 @@ final class ShipMapper: JSONMapper { | ||
213 | 213 | return |
214 | 214 | } |
215 | 215 | guard let found = slotItems.binarySearch(comparator: { $0.id ==? exSlotItem }), |
216 | - let ex = store?.object(of: SlotItem.entity, with: found.objectID) else { | |
216 | + let ex = store?.exchange(found) else { | |
217 | 217 | |
218 | 218 | return Logger.shared.log("Can not convert to current moc object") |
219 | 219 | } |
@@ -69,8 +69,8 @@ final class ShipMasterDetailWindowController: NSWindowController { | ||
69 | 69 | |
70 | 70 | let store = ServerDataStore.oneTimeEditor() |
71 | 71 | store.sync { |
72 | - guard let i = self.selectedShip?.objectID else { return } | |
73 | - guard let ship = store.object(of: Ship.entity, with: i) else { return } | |
72 | + guard let selectedShip = self.selectedShip else { return } | |
73 | + guard let ship = store.exchange(selectedShip) else { return } | |
74 | 74 | // |
75 | 75 | // ship.sally_area = sally.integerValue as NSNumber |
76 | 76 |
@@ -97,7 +97,8 @@ final class SlotItemMapper: JSONMapper { | ||
97 | 97 | return Logger.shared.log("Can not find MasterSlotItem") |
98 | 98 | } |
99 | 99 | |
100 | - guard let masterSlotItem = configuration.editorStore.object(of: MasterSlotItem.entity, with: mSlotItem.objectID) else { | |
100 | + // FUCK: 型推論がバカなのでダウンキャストしてるんだ!!! | |
101 | + guard let masterSlotItem = configuration.editorStore.exchange(mSlotItem) as? MasterSlotItem else { | |
101 | 102 | |
102 | 103 | return Logger.shared.log("Can not convert to current moc object") |
103 | 104 | } |