From cb479d18b2cbdb03f8b93020c41e41c1cace876f Mon Sep 17 00:00:00 2001 From: Vanderlei Martinelli Date: Fri, 22 May 2015 21:35:52 -0300 Subject: [PATCH 1/2] Better handling for merging background context changes --- Source/AlecrimCoreData/Classes/Stack.swift | 34 +++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Source/AlecrimCoreData/Classes/Stack.swift b/Source/AlecrimCoreData/Classes/Stack.swift index 4742de7..7cc0de5 100644 --- a/Source/AlecrimCoreData/Classes/Stack.swift +++ b/Source/AlecrimCoreData/Classes/Stack.swift @@ -309,7 +309,14 @@ private final class StackBackgroundManagedObjectContext: NSManagedObjectContext object: self.stack.coordinator ) } - + + NSNotificationCenter.defaultCenter().addObserver( + self, + selector: Selector("backgroundManagedObjectContextWillSave:"), + name: NSManagedObjectContextWillSaveNotification, + object: self + ) + NSNotificationCenter.defaultCenter().addObserver( self, selector: Selector("backgroundManagedObjectContextDidSave:"), @@ -319,6 +326,7 @@ private final class StackBackgroundManagedObjectContext: NSManagedObjectContext } private func removeObservers() { + NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextWillSaveNotification, object: self) NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextDidSaveNotification, object: self) if self.stack.contextOptions.stackType == .SQLite && self.stack.contextOptions.ubiquityEnabled { @@ -329,9 +337,33 @@ private final class StackBackgroundManagedObjectContext: NSManagedObjectContext } + @objc private func backgroundManagedObjectContextWillSave(notification: NSNotification) { + if let context = notification.object as? NSManagedObjectContext { + if let insertedObjects = context.insertedObjects as? Set { + if insertedObjects.count > 0 { + var error: NSError? = nil + if !context.obtainPermanentIDsForObjects((insertedObjects as NSSet).allObjects, error: &error) { + println(error) + } + } + } + } + } + @objc private func backgroundManagedObjectContextDidSave(notification: NSNotification) { if let mainContext = self.stack.mainManagedObjectContext { mainContext.performBlockAndWait { + if let userInfo = notification.userInfo { + let dict = userInfo as NSDictionary + if let updatedObjects = dict[NSUpdatedObjectsKey] as? Set { + if updatedObjects.count > 0 { + for object in updatedObjects { + mainContext.objectWithID(object.objectID).willAccessValueForKey(nil) // ensure that a fault has been fired + } + } + } + } + mainContext.mergeChangesFromContextDidSaveNotification(notification) } } From fac792222aa58f146a7926effc8416f43f98627c Mon Sep 17 00:00:00 2001 From: Vanderlei Martinelli Date: Mon, 25 May 2015 19:53:14 -0300 Subject: [PATCH 2/2] Updated README and pod spec file --- AlecrimCoreData.podspec | 2 +- README.md | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/AlecrimCoreData.podspec b/AlecrimCoreData.podspec index 8e8481a..65653be 100644 --- a/AlecrimCoreData.podspec +++ b/AlecrimCoreData.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "AlecrimCoreData" - s.version = "3.0" + s.version = "3.0.1" s.summary = "A framework to easily access Core Data objects in Swift." s.homepage = "https://github.com/Alecrim/AlecrimCoreData" diff --git a/README.md b/README.md index b7e85de..1e6d13d 100644 --- a/README.md +++ b/README.md @@ -404,11 +404,9 @@ If you want to contribute, please feel free to fork the repository and send pull ### Version History -- 3.0 - Swift framework: added attributes support and many other improvements -- 2.1 - Swift framework: added CocoaPods and Carthage support -- 2.0 - Swift framework: first public release as open source -- 1.1 - Objective-C framework: private Alecrim team use -- 1.0 - Objective-C framework: private Alecrim team use +- 3.x - Swift framework: added attributes support and many other improvements +- 2.x - Swift framework: public open source release +- 1.x - Objective-C framework: private Alecrim team use ---