|
9 | 9 | import CoreData
|
10 | 10 |
|
11 | 11 | public class CoreDataStack: NSObject {
|
12 |
| - /// Persistent store coordinator used as backing for the contexts |
13 |
| - public let persistentStoreCoordinator: NSPersistentStoreCoordinator |
14 |
| - |
15 |
| - /// Root context that is directly associated with the `persistentStoreCoordinator` and does it work on a background queue; Do not use directly |
16 |
| - public let rootContext: NSManagedObjectContext |
17 |
| - |
18 |
| - /// Context with concurrency type `NSMainQueueConcurrencyType`; Use only for read actions directly tied to the UI (e.g. NSFetchedResultsController) |
19 |
| - public let mainThreadContext: NSManagedObjectContext |
20 |
| - |
21 |
| - /// Child context of `rootContext` with concurrency type `PrivateQueueConcurrencyType`; Perform all read/write actions on this context |
22 |
| - public let backgroundContext: NSManagedObjectContext |
23 |
| - |
24 |
| - /** |
25 |
| - Create a stack based on the given `NSPersistentStoreCoordinator`. |
26 |
| - |
27 |
| - - parameter persistentStoreCoordinator: The coordinator that will be coordinate the persistent store of this stack |
28 |
| - */ |
29 |
| - public init(persistentStoreCoordinator: NSPersistentStoreCoordinator) { |
30 |
| - self.persistentStoreCoordinator = persistentStoreCoordinator |
31 |
| - |
32 |
| - self.rootContext = NSManagedObjectContext(persistentStoreCoordinator: self.persistentStoreCoordinator) |
33 |
| - self.rootContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy |
34 |
| - |
35 |
| - self.mainThreadContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType, parentContext: rootContext) |
36 |
| - |
37 |
| - self.backgroundContext = self.mainThreadContext.createChildContext() |
38 |
| - |
39 |
| - super.init() |
40 |
| - |
41 |
| - // TODO: In de huidige setup, nobody cares, want main context zit tussen de saves in en krijgt vanzelf de notificaties |
42 |
| - //NSNotificationCenter.defaultCenter().addObserver(self, selector: "rootContextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: rootContext) |
43 |
| - } |
44 |
| - |
45 |
| -// deinit { |
46 |
| -// NSNotificationCenter.defaultCenter().removeObserver(self) |
47 |
| -// } |
48 |
| - |
49 |
| -// MARK: Convenience methods |
50 |
| - |
51 |
| - /** |
52 |
| - Performs the given block on the `backgroundContect` |
53 |
| - |
54 |
| - - parameter block: Block that performs the changes on the given context that should be saved |
55 |
| - - parameter completion: Completion block to run after changes are saved |
56 |
| - |
57 |
| - :see: NSManagedObjectContext.performBlock() |
58 |
| - */ |
59 |
| - public func performBlockOnBackgroundContext(block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) { |
60 |
| - backgroundContext.performBlock(block, completionHandler: completionHandler) |
61 |
| - } |
62 |
| - |
63 |
| - public func performBlockOnBackgroundContext(block: PerformBlock) { |
64 |
| - backgroundContext.performBlock(block, completionHandler: nil) |
65 |
| - } |
66 |
| - |
67 |
| - /** |
68 |
| - Dumps some debug info about this stack to the console |
69 |
| - */ |
70 |
| - public func dumpStack() { |
71 |
| - CDK.sharedLogger(.DEBUG, "Stores: \(persistentStoreCoordinator.persistentStores)") |
72 |
| - CDK.sharedLogger(.DEBUG, " - Store coordinator: \(persistentStoreCoordinator.debugDescription)") |
73 |
| - CDK.sharedLogger(.DEBUG, " |- Root context: \(rootContext.debugDescription)") |
74 |
| - CDK.sharedLogger(.DEBUG, " |- Main thread context: \(mainThreadContext.debugDescription)") |
75 |
| - CDK.sharedLogger(.DEBUG, " |- Background context: \(backgroundContext.debugDescription)") |
76 |
| - } |
77 |
| - |
78 |
| -// MARK: Notification observers |
79 |
| - |
80 |
| -// func rootContextDidSave(notification: NSNotification) { |
81 |
| -// if NSThread.isMainThread() { |
82 |
| -// if let updatedObjects = notification.userInfo?[NSUpdatedObjectsKey] as? NSSet { |
83 |
| -// for _object in updatedObjects { |
84 |
| -// let object = _object as! NSManagedObject |
85 |
| -// mainThreadContext.objectWithID(object.objectID).willAccessValueForKey(nil) |
86 |
| -// } |
87 |
| -// } |
88 |
| -// |
89 |
| -// mainThreadContext.mergeChangesFromContextDidSaveNotification(notification) |
90 |
| -// } else { |
91 |
| -// dispatch_async(dispatch_get_main_queue()) { |
92 |
| -// self.rootContextDidSave(notification) |
93 |
| -// } |
94 |
| -// } |
95 |
| -// } |
| 12 | + /// Persistent store coordinator used as backing for the contexts |
| 13 | + public let persistentStoreCoordinator: NSPersistentStoreCoordinator |
| 14 | + |
| 15 | + /// Root context that is directly associated with the `persistentStoreCoordinator` and does it work on a background queue; Do not use directly |
| 16 | + public let rootContext: NSManagedObjectContext |
| 17 | + |
| 18 | + /// Context with concurrency type `NSMainQueueConcurrencyType`; Use only for read actions directly tied to the UI (e.g. NSFetchedResultsController) |
| 19 | + public let mainThreadContext: NSManagedObjectContext |
| 20 | + |
| 21 | + /// Child context of `rootContext` with concurrency type `PrivateQueueConcurrencyType`; Perform all read/write actions on this context |
| 22 | + public let backgroundContext: NSManagedObjectContext |
| 23 | + |
| 24 | + /** |
| 25 | + Create a stack based on the given `NSPersistentStoreCoordinator`. |
| 26 | + |
| 27 | + - parameter persistentStoreCoordinator: The coordinator that will be coordinate the persistent store of this stack |
| 28 | + */ |
| 29 | + public init(persistentStoreCoordinator: NSPersistentStoreCoordinator) { |
| 30 | + self.persistentStoreCoordinator = persistentStoreCoordinator |
| 31 | + |
| 32 | + self.rootContext = NSManagedObjectContext(persistentStoreCoordinator: self.persistentStoreCoordinator) |
| 33 | + self.rootContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy |
| 34 | + |
| 35 | + self.mainThreadContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType, parentContext: rootContext) |
| 36 | + |
| 37 | + self.backgroundContext = self.mainThreadContext.createChildContext() |
| 38 | + |
| 39 | + super.init() |
| 40 | + |
| 41 | + // TODO: In de huidige setup, nobody cares, want main context zit tussen de saves in en krijgt vanzelf de notificaties |
| 42 | + //NSNotificationCenter.defaultCenter().addObserver(self, selector: "rootContextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: rootContext) |
| 43 | + } |
| 44 | + |
| 45 | + // deinit { |
| 46 | + // NSNotificationCenter.defaultCenter().removeObserver(self) |
| 47 | + // } |
| 48 | + |
| 49 | + // MARK: Convenience methods |
| 50 | + |
| 51 | + /** |
| 52 | + Performs the given block on the `backgroundContect` |
| 53 | + |
| 54 | + - parameter block: Block that performs the changes on the given context that should be saved |
| 55 | + - parameter completion: Completion block to run after changes are saved |
| 56 | + |
| 57 | + :see: NSManagedObjectContext.performBlock() |
| 58 | + */ |
| 59 | + public func performBlockOnBackgroundContext(block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) { |
| 60 | + backgroundContext.performBlock(block, completionHandler: completionHandler) |
| 61 | + } |
| 62 | + |
| 63 | + public func performBlockOnBackgroundContext(block: PerformBlock) { |
| 64 | + backgroundContext.performBlock(block, completionHandler: nil) |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + Dumps some debug info about this stack to the console |
| 69 | + */ |
| 70 | + public func dumpStack() { |
| 71 | + CDK.sharedLogger(.DEBUG, "Stores: \(persistentStoreCoordinator.persistentStores)") |
| 72 | + CDK.sharedLogger(.DEBUG, " - Store coordinator: \(persistentStoreCoordinator.debugDescription)") |
| 73 | + CDK.sharedLogger(.DEBUG, " |- Root context: \(rootContext.debugDescription)") |
| 74 | + CDK.sharedLogger(.DEBUG, " |- Main thread context: \(mainThreadContext.debugDescription)") |
| 75 | + CDK.sharedLogger(.DEBUG, " |- Background context: \(backgroundContext.debugDescription)") |
| 76 | + } |
| 77 | + |
| 78 | + // MARK: Notification observers |
| 79 | + |
| 80 | + // func rootContextDidSave(notification: NSNotification) { |
| 81 | + // if NSThread.isMainThread() { |
| 82 | + // if let updatedObjects = notification.userInfo?[NSUpdatedObjectsKey] as? NSSet { |
| 83 | + // for _object in updatedObjects { |
| 84 | + // let object = _object as! NSManagedObject |
| 85 | + // mainThreadContext.objectWithID(object.objectID).willAccessValueForKey(nil) |
| 86 | + // } |
| 87 | + // } |
| 88 | + // |
| 89 | + // mainThreadContext.mergeChangesFromContextDidSaveNotification(notification) |
| 90 | + // } else { |
| 91 | + // dispatch_async(dispatch_get_main_queue()) { |
| 92 | + // self.rootContextDidSave(notification) |
| 93 | + // } |
| 94 | + // } |
| 95 | + // } |
96 | 96 | }
|
0 commit comments