• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

main code repository.


Commit MetaInfo

Revisionbffee1debad5eda2a130353ba091b22564716d54 (tree)
Time2018-03-23 22:06:50
Authormasakih <masakih@user...>
Commitermasakih

Log Message

NSManagedObjectIDを明に使用しないように変更

Change Summary

Incremental Difference

--- a/KCD/BookmarkListViewController.swift
+++ b/KCD/BookmarkListViewController.swift
@@ -154,8 +154,7 @@ extension BookmarkListViewController: NSTableViewDelegate, NSTableViewDataSource
154154
155155 guard let data = $0.element.data(forType: .bookmarkItem) else { return }
156156 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 }
159158
160159 bookmark.order = targetOrder + $0.offset + 1
161160 }
--- a/KCD/CoreDataManager.swift
+++ b/KCD/CoreDataManager.swift
@@ -41,10 +41,15 @@ protocol CoreDataAccessor: CoreDataProvider {
4141
4242 func insertNewObject<T>(for entity: Entity<T>) -> T?
4343 func delete(_ object: NSManagedObject)
44- func object<T>(of entity: Entity<T>, with objectId: NSManagedObjectID) -> T?
4544 func objects<T>(of entity: Entity<T>, sortDescriptors: [NSSortDescriptor]?, predicate: NSPredicate?) throws -> [T]
4645
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?
4853 }
4954
5055 protocol CoreDataManager: CoreDataAccessor {
@@ -161,11 +166,6 @@ extension CoreDataAccessor {
161166 context.delete(object)
162167 }
163168
164- func object<T>(of entity: Entity<T>, with objectId: NSManagedObjectID) -> T? {
165-
166- return context.object(with: objectId) as? T
167- }
168-
169169 func objects<T>(of entity: Entity<T>, sortDescriptors: [NSSortDescriptor]? = nil, predicate: NSPredicate? = nil) throws -> [T] {
170170
171171 let req = NSFetchRequest<T>(entityName: entity.name)
@@ -191,13 +191,24 @@ extension CoreDataAccessor {
191191 return result ?? []
192192 }
193193
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? {
195206
196207 var result: NSManagedObject?
197208 sync {
198- result = self.context.object(with: objectId)
209+ result = self.context.object(with: obj.objectID)
199210 }
200- return result!
211+ return result as? T
201212 }
202213 }
203214
--- a/KCD/HistoryTableViewController.swift
+++ b/KCD/HistoryTableViewController.swift
@@ -51,8 +51,7 @@ class HistoryTableViewController: NSViewController {
5151 let selectedIndex = controller.selectionIndex
5252 store.sync {
5353 selection
54- .map { $0.objectID }
55- .map(store.object(with:))
54+ .flatMap(store.exchange)
5655 .forEach(store.delete)
5756 }
5857
--- a/KCD/MasterShipMapper.swift
+++ b/KCD/MasterShipMapper.swift
@@ -51,7 +51,8 @@ final class MasterShipMapper: JSONMapper {
5151 return Logger.shared.log("MasterShipMapper: Can not find MasterSType")
5252 }
5353
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 {
5556
5657 return Logger.shared.log("MasterShipMapper: Can not convert to current moc object masterSType")
5758 }
--- a/KCD/ShipMapper.swift
+++ b/KCD/ShipMapper.swift
@@ -169,7 +169,7 @@ final class ShipMapper: JSONMapper {
169169 private func setMaster(_ masterId: Int, to ship: Ship) {
170170
171171 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 {
173173
174174 return Logger.shared.log("Can not convert to current moc object masterShip")
175175 }
@@ -188,7 +188,7 @@ final class ShipMapper: JSONMapper {
188188 .flatMap { item in
189189
190190 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 {
192192
193193 let maxV = convertedSlotItems.last
194194 if maxV != nil, maxV! < item {
@@ -213,7 +213,7 @@ final class ShipMapper: JSONMapper {
213213 return
214214 }
215215 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 {
217217
218218 return Logger.shared.log("Can not convert to current moc object")
219219 }
--- a/KCD/ShipMasterDetailWindowController.swift
+++ b/KCD/ShipMasterDetailWindowController.swift
@@ -69,8 +69,8 @@ final class ShipMasterDetailWindowController: NSWindowController {
6969
7070 let store = ServerDataStore.oneTimeEditor()
7171 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 }
7474 //
7575 // ship.sally_area = sally.integerValue as NSNumber
7676
--- a/KCD/SlotItemMapper.swift
+++ b/KCD/SlotItemMapper.swift
@@ -97,7 +97,8 @@ final class SlotItemMapper: JSONMapper {
9797 return Logger.shared.log("Can not find MasterSlotItem")
9898 }
9999
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 {
101102
102103 return Logger.shared.log("Can not convert to current moc object")
103104 }