Merge branch 'corestore2_develop' into corestore2_develop_objc

# Conflicts:
#	CoreStore.xcodeproj/project.pbxproj
This commit is contained in:
John Estropia
2016-03-24 15:16:13 +09:00
14 changed files with 531 additions and 69 deletions

View File

@@ -74,8 +74,17 @@ internal extension NSManagedObjectContext {
}
internal func setupForCoreStoreWithContextName(contextName: String) {
self.name = contextName
#if USE_FRAMEWORKS
self.name = contextName
#else
if #available(iOS 8.0, *) {
self.name = contextName
}
#endif
self.observerForWillSaveNotification = NotificationObserver(
notificationName: NSManagedObjectContextWillSaveNotification,

View File

@@ -2,7 +2,7 @@
// NSPersistentStoreCoordinator+Setup.swift
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
// Copyright © 2016 John Rommel Estropia. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -26,43 +26,122 @@
import Foundation
import CoreData
#if USE_FRAMEWORKS
import GCDKit
#endif
// MARK: - NSPersistentStoreCoordinator
internal extension NSPersistentStoreCoordinator {
// MARK: Internal
internal func performAsynchronously(closure: () -> Void) {
#if USE_FRAMEWORKS
self.performBlock(closure)
#else
if #available(iOS 8.0, *) {
self.performBlock(closure)
}
else {
self.lock()
GCDQueue.Default.async {
closure()
self.unlock()
}
}
#endif
}
@nonobjc internal func performBlockAndWait<T>(block: () throws -> T) throws -> T {
internal func performSynchronously(closure: () -> Void) {
#if USE_FRAMEWORKS
self.performBlockAndWait(closure)
#else
if #available(iOS 8.0, *) {
self.performBlockAndWait(closure)
}
else {
self.lock()
autoreleasepool(closure)
self.unlock()
}
#endif
}
internal func performSynchronously<T>(closure: () throws -> T) throws -> T {
var result: T?
var closureError: ErrorType?
self.performBlockAndWait {
var result: T?
#if USE_FRAMEWORKS
do {
self.performBlockAndWait {
result = try block()
do {
result = try closure()
}
catch {
closureError = error
}
}
catch {
#else
if #available(iOS 8.0, *) {
closureError = error
self.performBlockAndWait {
do {
result = try closure()
}
catch {
closureError = error
}
}
}
else {
self.lock()
autoreleasepool {
do {
result = try closure()
}
catch {
closureError = error
}
}
self.unlock()
}
#endif
if let closureError = closureError {
throw closureError
}
if let result = result {
return result
}
throw closureError!
return result!
}
@nonobjc internal func addPersistentStoreSynchronously(storeType: String, configuration: String?, URL storeURL: NSURL?, options: [NSObject : AnyObject]?) throws -> NSPersistentStore {
var store: NSPersistentStore?
var storeError: NSError?
self.performBlockAndWait {
self.performSynchronously {
do {

View File

@@ -75,7 +75,7 @@ public extension DataStack {
*/
public func addStorage<T: StorageInterface>(storage: T, completion: (SetupResult<T>) -> Void) throws -> NSProgress? {
self.coordinator.performBlock {
self.coordinator.performAsynchronously {
if let _ = self.persistentStoreForStorage(storage) {
@@ -143,7 +143,7 @@ public extension DataStack {
Asynchronously adds a `LocalStorage` to the stack. Migrations are also initiated by default.
```
try dataStack.addStorage(
SQLiteStore(configuration: "Config1"),
SQLiteStore(configuration: "Config1"),
completion: { result in
switch result {
case .Success(let storage): // ...
@@ -164,7 +164,7 @@ public extension DataStack {
"The specified URL for the \(typeName(storage)) is invalid: \"\(fileURL)\""
)
return try self.coordinator.performBlockAndWait {
return try self.coordinator.performSynchronously {
if let _ = self.persistentStoreForStorage(storage) {
@@ -284,7 +284,7 @@ public extension DataStack {
*/
public func upgradeStorageIfNeeded<T: LocalStorage>(storage: T, completion: (MigrationResult) -> Void) throws -> NSProgress? {
return try self.coordinator.performBlockAndWait {
return try self.coordinator.performSynchronously {
let fileURL = storage.fileURL
do {
@@ -326,7 +326,7 @@ public extension DataStack {
@warn_unused_result
public func requiredMigrationsForStorage<T: LocalStorage>(storage: T) throws -> [MigrationType] {
return try self.coordinator.performBlockAndWait {
return try self.coordinator.performSynchronously {
let fileURL = storage.fileURL
@@ -486,7 +486,16 @@ public extension DataStack {
}
let migrationOperation = NSBlockOperation()
migrationOperation.qualityOfService = .Utility
#if USE_FRAMEWORKS
migrationOperation.qualityOfService = .Utility
#else
if #available(iOS 8.0, *) {
migrationOperation.qualityOfService = .Utility
}
#endif
operations.forEach { migrationOperation.addDependency($0) }
migrationOperation.addExecutionBlock { () -> Void in
@@ -533,18 +542,18 @@ public extension DataStack {
fromBundles: storage.mappingModelBundles,
forSourceModel: sourceModel,
destinationModel: destinationModel) {
migrationSteps.append(
(
sourceModel: sourceModel,
destinationModel: destinationModel,
mappingModel: mappingModel,
migrationType: .Heavyweight(
sourceVersion: currentVersion,
destinationVersion: nextVersion
)
migrationSteps.append(
(
sourceModel: sourceModel,
destinationModel: destinationModel,
mappingModel: mappingModel,
migrationType: .Heavyweight(
sourceVersion: currentVersion,
destinationVersion: nextVersion
)
)
)
}
else {
@@ -682,7 +691,7 @@ public extension DataStack {
public func addInMemoryStore(configuration configuration: String? = nil, completion: (PersistentStoreResult) -> Void) {
do {
try self.addStorage(
InMemoryStore(configuration: configuration),
completion: { result in

View File

@@ -151,7 +151,7 @@ public final class DataStack {
do {
return try self.coordinator.performBlockAndWait { () -> T in
return try self.coordinator.performSynchronously {
if let _ = self.persistentStoreForStorage(storage) {
@@ -202,7 +202,7 @@ public final class DataStack {
*/
public func addStorageAndWait<T: LocalStorage>(storage: T) throws -> T {
return try self.coordinator.performBlockAndWait {
return try self.coordinator.performSynchronously {
let fileURL = storage.fileURL
CoreStore.assert(
@@ -299,8 +299,18 @@ public final class DataStack {
let migrationQueue = NSOperationQueue()
migrationQueue.maxConcurrentOperationCount = 1
migrationQueue.name = "com.coreStore.migrationOperationQueue"
migrationQueue.qualityOfService = .Utility
migrationQueue.underlyingQueue = dispatch_queue_create("com.coreStore.migrationQueue", DISPATCH_QUEUE_SERIAL)
#if USE_FRAMEWORKS
migrationQueue.qualityOfService = .Utility
migrationQueue.underlyingQueue = dispatch_queue_create("com.coreStore.migrationQueue", DISPATCH_QUEUE_SERIAL)
#else
if #available(iOS 8.0, *) {
migrationQueue.qualityOfService = .Utility
migrationQueue.underlyingQueue = dispatch_queue_create("com.coreStore.migrationQueue", DISPATCH_QUEUE_SERIAL)
}
#endif
return migrationQueue
}()