mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-31 06:33:06 +02:00
changed concurrency architecture. Transactions are now done on a direct child context of the root saving context (instead of a main context child)
This commit is contained in:
@@ -41,7 +41,7 @@ public extension DataStack {
|
|||||||
public func performTransaction(closure: (transaction: AsynchronousDataTransaction) -> Void) {
|
public func performTransaction(closure: (transaction: AsynchronousDataTransaction) -> Void) {
|
||||||
|
|
||||||
AsynchronousDataTransaction(
|
AsynchronousDataTransaction(
|
||||||
mainContext: self.mainContext,
|
mainContext: self.rootSavingContext,
|
||||||
queue: self.childTransactionQueue,
|
queue: self.childTransactionQueue,
|
||||||
closure: closure).perform()
|
closure: closure).perform()
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@ public extension DataStack {
|
|||||||
public func performTransactionAndWait(closure: (transaction: SynchronousDataTransaction) -> Void) -> SaveResult? {
|
public func performTransactionAndWait(closure: (transaction: SynchronousDataTransaction) -> Void) -> SaveResult? {
|
||||||
|
|
||||||
return SynchronousDataTransaction(
|
return SynchronousDataTransaction(
|
||||||
mainContext: self.mainContext,
|
mainContext: self.rootSavingContext,
|
||||||
queue: self.childTransactionQueue,
|
queue: self.childTransactionQueue,
|
||||||
closure: closure).performAndWait()
|
closure: closure).performAndWait()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,11 +45,14 @@ public class DataStack {
|
|||||||
// MARK: Public
|
// MARK: Public
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Initializes a DataStack from merged model in the app bundle.
|
Initializes a DataStack from a model created by merging all the models found in all bundles.
|
||||||
*/
|
*/
|
||||||
public convenience init() {
|
public convenience init() {
|
||||||
|
|
||||||
self.init(managedObjectModel: NSManagedObjectModel.mergedModelFromBundles(NSBundle.allBundles())!)
|
let mergedModel: NSManagedObjectModel! = NSManagedObjectModel.mergedModelFromBundles(NSBundle.allBundles())
|
||||||
|
HardcoreData.assert(mergedModel != nil, "Could not create a merged <\(NSManagedObjectModel.self)> from all bundles.")
|
||||||
|
|
||||||
|
self.init(managedObjectModel: mergedModel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,8 +62,11 @@ public class DataStack {
|
|||||||
*/
|
*/
|
||||||
public convenience init(modelName: String) {
|
public convenience init(modelName: String) {
|
||||||
|
|
||||||
let modelFilePath = NSBundle.mainBundle().pathForResource(modelName, ofType: "momd")!
|
let modelFilePath: String! = NSBundle.mainBundle().pathForResource(modelName, ofType: "momd")
|
||||||
let managedObjectModel = NSManagedObjectModel(contentsOfURL: NSURL(fileURLWithPath: modelFilePath)!)!
|
HardcoreData.assert(modelFilePath != nil, "Could not find a \"momd\" resource from the main bundle.")
|
||||||
|
|
||||||
|
let managedObjectModel: NSManagedObjectModel! = NSManagedObjectModel(contentsOfURL: NSURL(fileURLWithPath: modelFilePath)!)
|
||||||
|
HardcoreData.assert(managedObjectModel != nil, "Could not create an <\(NSManagedObjectModel.self)> from the resource at path \"\(modelFilePath)\".")
|
||||||
|
|
||||||
self.init(managedObjectModel: managedObjectModel)
|
self.init(managedObjectModel: managedObjectModel)
|
||||||
}
|
}
|
||||||
@@ -218,7 +224,8 @@ public class DataStack {
|
|||||||
where (
|
where (
|
||||||
resetStoreOnMigrationFailure
|
resetStoreOnMigrationFailure
|
||||||
&& (error.code == NSPersistentStoreIncompatibleVersionHashError
|
&& (error.code == NSPersistentStoreIncompatibleVersionHashError
|
||||||
|| error.code == NSMigrationMissingSourceModelError)
|
|| error.code == NSMigrationMissingSourceModelError
|
||||||
|
|| error.code == NSMigrationError)
|
||||||
&& error.domain == NSCocoaErrorDomain
|
&& error.domain == NSCocoaErrorDomain
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@@ -259,6 +266,7 @@ public class DataStack {
|
|||||||
|
|
||||||
// MARK: Internal
|
// MARK: Internal
|
||||||
|
|
||||||
|
internal let rootSavingContext: NSManagedObjectContext
|
||||||
internal let mainContext: NSManagedObjectContext
|
internal let mainContext: NSManagedObjectContext
|
||||||
internal let childTransactionQueue: GCDQueue = .createSerial("com.hardcoredata.datastack.childtransactionqueue")
|
internal let childTransactionQueue: GCDQueue = .createSerial("com.hardcoredata.datastack.childtransactionqueue")
|
||||||
|
|
||||||
@@ -274,6 +282,5 @@ public class DataStack {
|
|||||||
private typealias EntityNameType = String
|
private typealias EntityNameType = String
|
||||||
|
|
||||||
private let coordinator: NSPersistentStoreCoordinator
|
private let coordinator: NSPersistentStoreCoordinator
|
||||||
private let rootSavingContext: NSManagedObjectContext
|
|
||||||
private let entityNameMapping: [EntityClassNameType: EntityNameType]
|
private let entityNameMapping: [EntityClassNameType: EntityNameType]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,15 @@ internal extension NSManagedObject {
|
|||||||
if objectID.temporaryID {
|
if objectID.temporaryID {
|
||||||
|
|
||||||
var error: NSError?
|
var error: NSError?
|
||||||
if !context.obtainPermanentIDsForObjects([self], error: &error) {
|
var didSucceed: Bool?
|
||||||
|
if let managedObjectContext = self.managedObjectContext {
|
||||||
|
|
||||||
|
managedObjectContext.performBlockAndWait {
|
||||||
|
|
||||||
|
didSucceed = managedObjectContext.obtainPermanentIDsForObjects([self], error: &error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if didSucceed != true {
|
||||||
|
|
||||||
HardcoreData.handleError(
|
HardcoreData.handleError(
|
||||||
error ?? NSError(hardcoreDataErrorCode: .UnknownError),
|
error ?? NSError(hardcoreDataErrorCode: .UnknownError),
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ internal extension NSManagedObjectContext {
|
|||||||
context.parentContext = self
|
context.parentContext = self
|
||||||
context.parentStack = self.parentStack
|
context.parentStack = self.parentStack
|
||||||
context.setupForHardcoreDataWithContextName("com.hardcoredata.temporarycontext")
|
context.setupForHardcoreDataWithContextName("com.hardcoredata.temporarycontext")
|
||||||
context.shouldCascadeSavesToParent = (self.concurrencyType == .MainQueueConcurrencyType)
|
context.shouldCascadeSavesToParent = (self.parentStack?.rootSavingContext == self)
|
||||||
|
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user