OSDN Git Service

CoreData関連を分割して整理
authormasakih <masakih@users.sourceforge.jp>
Fri, 27 Oct 2017 12:29:47 +0000 (21:29 +0900)
committermasakih <masakih@users.sourceforge.jp>
Fri, 27 Oct 2017 12:29:47 +0000 (21:29 +0900)
KCD.xcodeproj/project.pbxproj
KCD/CoreDataCore.swift
KCD/CoreDataManager.swift [new file with mode: 0644]
KCD/Entity.swift
KCD/ManagedObjectContextGenerator.swift

index 58b4634..db179fc 100644 (file)
                F4FF44D21FA0BB1C00039F21 /* TemporaryDataStoreAccessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF44D11FA0BB1C00039F21 /* TemporaryDataStoreAccessor.swift */; };
                F4FF44D41FA0BB4D00039F21 /* BookmarkDataStoreAccessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF44D31FA0BB4D00039F21 /* BookmarkDataStoreAccessor.swift */; };
                F4FF44D61FA0BB7200039F21 /* ResourceHistoryDataStoreAccessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF44D51FA0BB7200039F21 /* ResourceHistoryDataStoreAccessor.swift */; };
+               F4FF44D81FA21BF200039F21 /* CoreDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF44D71FA21BF200039F21 /* CoreDataManager.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
                F4FF44D11FA0BB1C00039F21 /* TemporaryDataStoreAccessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TemporaryDataStoreAccessor.swift; sourceTree = "<group>"; };
                F4FF44D31FA0BB4D00039F21 /* BookmarkDataStoreAccessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarkDataStoreAccessor.swift; sourceTree = "<group>"; };
                F4FF44D51FA0BB7200039F21 /* ResourceHistoryDataStoreAccessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResourceHistoryDataStoreAccessor.swift; sourceTree = "<group>"; };
+               F4FF44D71FA21BF200039F21 /* CoreDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataManager.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
                        children = (
                                F4BDEEB11E73FE2000D689AE /* Entity.swift */,
                                F4E471281E46ECF1009B5AF4 /* CoreDataCore.swift */,
+                               F4FF44D71FA21BF200039F21 /* CoreDataManager.swift */,
                                F44AA2661E814A4400EED8BE /* ManagedObjectContextGenerator.swift */,
                                F481D63E1F9105CB007E2CBE /* Entities.swift */,
                                F4FC3E8D1E4368BB00E41228 /* KCManagedObject.swift */,
                                F47C3EAC1E5F1AD900D97449 /* ShipMapper.swift in Sources */,
                                F44BC7351E2A295E004644E3 /* SetActionCommand.swift in Sources */,
                                F472159E1E1E6EE70083D3BC /* ShipMasterDetailWindowController.swift in Sources */,
+                               F4FF44D81FA21BF200039F21 /* CoreDataManager.swift in Sources */,
                                F44BC6D71E226CB1004644E3 /* HokyuChargeCommand.swift in Sources */,
                                F4CF25BC1E34F67400C02A66 /* KenzoDockStatus.swift in Sources */,
                                F44BC6F91E23DAC5004644E3 /* RealDestroyShipCommand.swift in Sources */,
index 8134add..c700d68 100644 (file)
@@ -8,14 +8,6 @@
 
 import Cocoa
 
-// MARK: enum
-enum CoreDataManagerType {
-    
-    case reader
-    case editor
-}
-
-// MARK: - struct
 struct CoreDataConfiguration {
     
     let modelName: String
@@ -74,133 +66,3 @@ struct CoreDataCore {
         return moc
     }
 }
-
-// MARK: - protocol
-protocol CoreDataProvider {
-    
-    init(type: CoreDataManagerType)
-    
-    static var core: CoreDataCore { get }
-    
-    var context: NSManagedObjectContext { get }
-    
-    func save()
-    func removeDataFile()
-}
-
-protocol CoreDataAccessor: CoreDataProvider {
-    
-    func insertNewObject<T>(for entity: Entity<T>) -> T?
-    func delete(_ object: NSManagedObject)
-    func object<T>(of entity: Entity<T>, with objectId: NSManagedObjectID) -> T?
-    func objects<T>(of entity: Entity<T>, sortDescriptors: [NSSortDescriptor]?, predicate: NSPredicate?) throws -> [T]
-    
-    func object(with objectId: NSManagedObjectID) -> NSManagedObject
-}
-
-protocol CoreDataManager: CoreDataAccessor {
-    
-    associatedtype InstanceType = Self
-    
-    static var `default`: InstanceType { get }
-    
-    static func oneTimeEditor() -> InstanceType
-}
-
-// MARK: - Extension
-extension CoreDataProvider {
-    
-    func save() {
-        
-        if !context.commitEditing() {
-            
-            print("\(String(describing: type(of: self))) unable to commit editing before saveing")
-            
-            return
-        }
-        
-        do {
-            
-            try context.save()
-            
-        } catch {
-            
-            presentOnMainThread(error)
-        }
-        
-        if let p = context.parent {
-            
-            p.performAndWait {
-                do {
-                    
-                    try p.save()
-                    
-                } catch {
-                    
-                    self.presentOnMainThread(error)
-                }
-            }
-        }
-    }
-    
-    func removeDataFile() {
-        
-        remove(name: type(of: self).core.config.fileName)
-    }
-    
-    private func presentOnMainThread(_ error: Error) {
-        
-        if Thread.isMainThread {
-            
-            NSApp.presentError(error)
-            
-        } else {
-            
-            DispatchQueue.main.sync {
-                
-                _ = NSApp.presentError(error)
-            }
-        }
-    }
-    
-    static func context(for type: CoreDataManagerType) -> NSManagedObjectContext {
-        
-        switch type {
-        case .reader: return core.parentContext
-            
-        case .editor: return core.editorContext()
-        }
-    }
-}
-
-extension CoreDataAccessor {
-    
-    func insertNewObject<T>(for entity: Entity<T>) -> T? {
-        
-        return NSEntityDescription.insertNewObject(forEntityName: entity.name, into: context) as? T
-    }
-    
-    func delete(_ object: NSManagedObject) {
-        
-        context.delete(object)
-    }
-    
-    func object<T>(of entity: Entity<T>, with objectId: NSManagedObjectID) -> T? {
-        
-        return context.object(with: objectId) as? T
-    }
-    
-    func objects<T>(of entity: Entity<T>, sortDescriptors: [NSSortDescriptor]? = nil, predicate: NSPredicate? = nil) throws -> [T] {
-        
-        let req = NSFetchRequest<T>(entityName: entity.name)
-        req.sortDescriptors = sortDescriptors
-        req.predicate = predicate
-        
-        return try context.fetch(req)
-    }
-    
-    func object(with objectId: NSManagedObjectID) -> NSManagedObject {
-        
-        return context.object(with: objectId)
-    }
-}
diff --git a/KCD/CoreDataManager.swift b/KCD/CoreDataManager.swift
new file mode 100644 (file)
index 0000000..3fcf333
--- /dev/null
@@ -0,0 +1,151 @@
+//
+//  CoreDataManager.swift
+//  KCD
+//
+//  Created by Hori,Masaki on 2017/10/26.
+//  Copyright © 2017年 Hori,Masaki. All rights reserved.
+//
+
+import Cocoa
+
+enum CoreDataManagerType {
+    
+    case reader
+    case editor
+}
+
+enum CoreDataError: Error {
+    
+    case applicationDirectoryIsFile
+    case couldNotCreateModel
+    case couldNotCreateCoordinator(String)
+}
+
+protocol CoreDataProvider {
+    
+    init(type: CoreDataManagerType)
+    
+    static var core: CoreDataCore { get }
+    
+    var context: NSManagedObjectContext { get }
+    
+    func save()
+    func removeDataFile()
+}
+
+protocol CoreDataAccessor: CoreDataProvider {
+    
+    func insertNewObject<T>(for entity: Entity<T>) -> T?
+    func delete(_ object: NSManagedObject)
+    func object<T>(of entity: Entity<T>, with objectId: NSManagedObjectID) -> T?
+    func objects<T>(of entity: Entity<T>, sortDescriptors: [NSSortDescriptor]?, predicate: NSPredicate?) throws -> [T]
+    
+    func object(with objectId: NSManagedObjectID) -> NSManagedObject
+}
+
+protocol CoreDataManager: CoreDataAccessor {
+    
+    associatedtype InstanceType = Self
+    
+    static var `default`: InstanceType { get }
+    
+    static func oneTimeEditor() -> InstanceType
+}
+
+// MARK: - Extension
+extension CoreDataProvider {
+    
+    func save() {
+        
+        if !context.commitEditing() {
+            
+            print("\(String(describing: type(of: self))) unable to commit editing before saveing")
+            
+            return
+        }
+        
+        do {
+            
+            try context.save()
+            
+        } catch {
+            
+            presentOnMainThread(error)
+        }
+        
+        if let p = context.parent {
+            
+            p.performAndWait {
+                do {
+                    
+                    try p.save()
+                    
+                } catch {
+                    
+                    self.presentOnMainThread(error)
+                }
+            }
+        }
+    }
+    
+    func removeDataFile() {
+        
+        removeAllDataFile(named: type(of: self).core.config.fileName)
+    }
+    
+    private func presentOnMainThread(_ error: Error) {
+        
+        if Thread.isMainThread {
+            
+            NSApp.presentError(error)
+            
+        } else {
+            
+            DispatchQueue.main.sync {
+                
+                _ = NSApp.presentError(error)
+            }
+        }
+    }
+    
+    static func context(for type: CoreDataManagerType) -> NSManagedObjectContext {
+        
+        switch type {
+        case .reader: return core.parentContext
+            
+        case .editor: return core.editorContext()
+        }
+    }
+}
+
+extension CoreDataAccessor {
+    
+    func insertNewObject<T>(for entity: Entity<T>) -> T? {
+        
+        return NSEntityDescription.insertNewObject(forEntityName: entity.name, into: context) as? T
+    }
+    
+    func delete(_ object: NSManagedObject) {
+        
+        context.delete(object)
+    }
+    
+    func object<T>(of entity: Entity<T>, with objectId: NSManagedObjectID) -> T? {
+        
+        return context.object(with: objectId) as? T
+    }
+    
+    func objects<T>(of entity: Entity<T>, sortDescriptors: [NSSortDescriptor]? = nil, predicate: NSPredicate? = nil) throws -> [T] {
+        
+        let req = NSFetchRequest<T>(entityName: entity.name)
+        req.sortDescriptors = sortDescriptors
+        req.predicate = predicate
+        
+        return try context.fetch(req)
+    }
+    
+    func object(with objectId: NSManagedObjectID) -> NSManagedObject {
+        
+        return context.object(with: objectId)
+    }
+}
index 3fdeea4..e20afc1 100644 (file)
@@ -29,7 +29,6 @@ extension EntityProvider {
     }
 }
 
-// MARK: - Implementations
 extension NSManagedObject {
     
     class var entityName: String { return String(describing: self) }
index 688e10e..c77d3fb 100644 (file)
@@ -8,14 +8,16 @@
 
 import Cocoa
 
-enum CoreDataError: Error {
+func genarate(_ config: CoreDataConfiguration) throws -> (model: NSManagedObjectModel, coordinator: NSPersistentStoreCoordinator, moc: NSManagedObjectContext) {
+    
+    let model = try createModel(config)
+    let coordinator = try getCoordinator(config, model)
+    let moc = createContext(coordinator)
     
-    case applicationDirectoryIsFile
-    case couldNotCreateModel
-    case couldNotCreateCoordinator(String)
+    return (model: model, coordinator: coordinator, moc: moc)
 }
 
-func remove(name: String) {
+func removeAllDataFile(named name: String) {
     
     ["", "-wal", "-shm"]
         .map { name + $0 }
@@ -23,22 +25,6 @@ func remove(name: String) {
         .forEach(removeDataFile)
 }
 
-func genarate(_ config: CoreDataConfiguration) throws -> (model: NSManagedObjectModel, coordinator: NSPersistentStoreCoordinator, moc: NSManagedObjectContext) {
-    
-    do {
-        
-        let model = try createModel(config)
-        let coordinator = try getCoordinator(config, model)
-        let moc = createContext(coordinator)
-        
-        return (model: model, coordinator: coordinator, moc: moc)
-        
-    } catch {
-        
-        throw error
-    }
-}
-
 private func removeDataFile(at url: URL) {
     
     do {
@@ -62,6 +48,7 @@ private  func createModel(_ config: CoreDataConfiguration) throws -> NSManagedOb
     
     return model
 }
+
 private func getCoordinator(_ config: CoreDataConfiguration, _ model: NSManagedObjectModel) throws -> NSPersistentStoreCoordinator {
     
     do {
@@ -75,7 +62,7 @@ private func getCoordinator(_ config: CoreDataConfiguration, _ model: NSManagedO
             (error.code == 134130 || error.code == 134110),
             config.tryRemake {
             
-            remove(name: config.fileName)
+            removeAllDataFile(named: config.fileName)
             
             do {
                 
@@ -83,7 +70,7 @@ private func getCoordinator(_ config: CoreDataConfiguration, _ model: NSManagedO
                 
             } catch {
                 
-                print("Fail crrate NSPersistentStoreCoordinator twice.")
+                print("Fail to create NSPersistentStoreCoordinator twice.")
             }
         }
         
@@ -103,17 +90,10 @@ private func createCoordinator(_ config: CoreDataConfiguration, _ model: NSManag
     let coordinator: NSPersistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
     let url = ApplicationDirecrories.support.appendingPathComponent(config.fileName)
     
-    do {
-        
-        try coordinator.addPersistentStore(ofType: config.type,
-                                           configurationName: nil,
-                                           at: url,
-                                           options: config.options)
-        
-    } catch {
-        
-        throw error
-    }
+    try coordinator.addPersistentStore(ofType: config.type,
+                                       configurationName: nil,
+                                       at: url,
+                                       options: config.options)
     
     return coordinator
 }