Merge branch 'master' into corestore2_develop

# Conflicts:
#	CoreStore.podspec
#	Sources/Info.plist
#	Sources/Internal/NSManagedObjectContext+Transaction.swift
This commit is contained in:
John Rommel Estropia
2016-05-07 17:47:35 +08:00
5 changed files with 51 additions and 11 deletions

View File

@@ -100,14 +100,28 @@ internal extension NSManagedObjectContext {
object: rootContext,
closure: { [weak context] (note) -> Void in
context?.performBlock { () -> Void in
guard let rootContext = note.object as? NSManagedObjectContext,
let context = context else {
return
}
let mergeChanges = { () -> Void in
let updatedObjects = (note.userInfo?[NSUpdatedObjectsKey] as? Set<NSManagedObject>) ?? []
for object in updatedObjects {
context?.objectWithID(object.objectID).willAccessValueForKey(nil)
context.objectWithID(object.objectID).willAccessValueForKey(nil)
}
context?.mergeChangesFromContextDidSaveNotification(note)
context.mergeChangesFromContextDidSaveNotification(note)
}
if rootContext.isSavingSynchronously == true {
context.performBlockAndWait(mergeChanges)
}
else {
context.performBlock(mergeChanges)
}
}
)

View File

@@ -56,6 +56,27 @@ internal extension NSManagedObjectContext {
}
}
@nonobjc
internal var isSavingSynchronously: Bool? {
get {
let value: NSNumber? = getAssociatedObjectForKey(
&PropertyKeys.isSavingSynchronously,
inObject: self
)
return value?.boolValue
}
set {
setAssociatedWeakObject(
newValue.flatMap { NSNumber(bool: $0) },
forKey: &PropertyKeys.isSavingSynchronously,
inObject: self
)
}
}
@nonobjc
internal func isRunningInAllowedQueue() -> Bool {
@@ -93,7 +114,9 @@ internal extension NSManagedObjectContext {
do {
self.isSavingSynchronously = true
try self.save()
self.isSavingSynchronously = nil
}
catch {
@@ -142,7 +165,9 @@ internal extension NSManagedObjectContext {
do {
self.isSavingSynchronously = false
try self.save()
self.isSavingSynchronously = nil
}
catch {
@@ -173,7 +198,7 @@ internal extension NSManagedObjectContext {
}
@nonobjc
internal func refreshAllObjectsAsFaults() {
internal func refreshAndMergeAllObjects() {
if #available(iOS 8.3, OSX 10.11, *) {
@@ -181,7 +206,7 @@ internal extension NSManagedObjectContext {
}
else {
self.registeredObjects.forEach { self.refreshObject($0, mergeChanges: false) }
self.registeredObjects.forEach { self.refreshObject($0, mergeChanges: true) }
}
}
@@ -191,5 +216,6 @@ internal extension NSManagedObjectContext {
private struct PropertyKeys {
static var parentTransaction: Void?
static var isSavingSynchronously: Void?
}
}

View File

@@ -191,14 +191,14 @@ public /*abstract*/ class BaseDataTransaction {
/**
Refreshes all registered objects `NSManagedObject`s in the transaction.
*/
public func refreshAllObjectsAsFaults() {
public func refreshAndMergeAllObjects() {
CoreStore.assert(
self.isRunningInAllowedQueue(),
"Attempted to refresh entities outside their designated queue."
)
self.context.refreshAllObjectsAsFaults()
self.context.refreshAndMergeAllObjects()
}

View File

@@ -66,9 +66,9 @@ public extension CoreStore {
/**
Refreshes all registered objects `NSManagedObject`s in the `defaultStack`.
*/
public static func refreshAllObjectsAsFaults() {
public static func refreshAndMergeAllObjects() {
self.defaultStack.refreshAllObjectsAsFaults()
self.defaultStack.refreshAndMergeAllObjects()
}

View File

@@ -83,14 +83,14 @@ public extension DataStack {
/**
Refreshes all registered objects `NSManagedObject`s in the `DataStack`.
*/
public func refreshAllObjectsAsFaults() {
public func refreshAndMergeAllObjects() {
CoreStore.assert(
NSThread.isMainThread(),
"Attempted to refresh entities outside their designated queue."
)
self.mainContext.refreshAllObjectsAsFaults()
self.mainContext.refreshAndMergeAllObjects()
}