mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-18 15:07:00 +01:00
tidy up
This commit is contained in:
@@ -406,11 +406,11 @@ extension Int8: SelectValueResultType {
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int8? {
|
||||
|
||||
if let value = (result as? NSNumber)?.longLongValue {
|
||||
guard let value = (result as? NSNumber)?.longLongValue else {
|
||||
|
||||
return numericCast(value) as Int8
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return numericCast(value) as Int8
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,11 +426,11 @@ extension Int16: SelectValueResultType {
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int16? {
|
||||
|
||||
if let value = (result as? NSNumber)?.longLongValue {
|
||||
guard let value = (result as? NSNumber)?.longLongValue else {
|
||||
|
||||
return numericCast(value) as Int16
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return numericCast(value) as Int16
|
||||
}
|
||||
}
|
||||
|
||||
@@ -446,11 +446,11 @@ extension Int32: SelectValueResultType {
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int32? {
|
||||
|
||||
if let value = (result as? NSNumber)?.longLongValue {
|
||||
guard let value = (result as? NSNumber)?.longLongValue else {
|
||||
|
||||
return numericCast(value) as Int32
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return numericCast(value) as Int32
|
||||
}
|
||||
}
|
||||
|
||||
@@ -482,11 +482,11 @@ extension Int: SelectValueResultType {
|
||||
|
||||
public static func fromResultObject(result: AnyObject) -> Int? {
|
||||
|
||||
if let value = (result as? NSNumber)?.longLongValue {
|
||||
guard let value = (result as? NSNumber)?.longLongValue else {
|
||||
|
||||
return numericCast(value) as Int
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return numericCast(value) as Int
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,11 +37,12 @@ internal final class MigrationManager: NSMigrationManager, NSProgressReporting {
|
||||
|
||||
super.didChangeValueForKey(key)
|
||||
|
||||
if key == "migrationProgress" {
|
||||
guard key == "migrationProgress" else {
|
||||
|
||||
let progress = self.progress
|
||||
progress.completedUnitCount = Int64(Float(progress.totalUnitCount) * self.migrationProgress)
|
||||
return
|
||||
}
|
||||
let progress = self.progress
|
||||
progress.completedUnitCount = Int64(Float(progress.totalUnitCount) * self.migrationProgress)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,22 +17,10 @@ internal extension NSFileManager {
|
||||
|
||||
internal func removeSQLiteStoreAtURL(fileURL: NSURL) {
|
||||
|
||||
do {
|
||||
|
||||
try self.removeItemAtURL(fileURL)
|
||||
}
|
||||
catch _ { }
|
||||
_ = try? self.removeItemAtURL(fileURL)
|
||||
|
||||
do {
|
||||
|
||||
try self.removeItemAtPath(fileURL.path!.stringByAppendingString("-shm"))
|
||||
}
|
||||
catch _ { }
|
||||
|
||||
do {
|
||||
|
||||
try self.removeItemAtPath(fileURL.path!.stringByAppendingString("-wal"))
|
||||
}
|
||||
catch _ { }
|
||||
let filePath = fileURL.path!
|
||||
_ = try? self.removeItemAtPath(filePath.stringByAppendingString("-shm"))
|
||||
_ = try? self.removeItemAtPath(filePath.stringByAppendingString("-wal"))
|
||||
}
|
||||
}
|
||||
@@ -61,14 +61,14 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
internal func entityDescriptionForEntityClass(entity: AnyClass) -> NSEntityDescription? {
|
||||
|
||||
if let entityName = self.parentStack?.entityNameForEntityClass(entity) {
|
||||
|
||||
return NSEntityDescription.entityForName(
|
||||
entityName,
|
||||
inManagedObjectContext: self
|
||||
)
|
||||
guard let entityName = self.parentStack?.entityNameForEntityClass(entity) else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return NSEntityDescription.entityForName(
|
||||
entityName,
|
||||
inManagedObjectContext: self
|
||||
)
|
||||
}
|
||||
|
||||
internal func setupForCoreStoreWithContextName(contextName: String) {
|
||||
@@ -83,7 +83,7 @@ internal extension NSManagedObjectContext {
|
||||
let context = note.object as! NSManagedObjectContext
|
||||
let insertedObjects = context.insertedObjects
|
||||
let numberOfInsertedObjects = insertedObjects.count
|
||||
if numberOfInsertedObjects <= 0 {
|
||||
guard numberOfInsertedObjects > 0 else {
|
||||
|
||||
return
|
||||
}
|
||||
@@ -91,7 +91,6 @@ internal extension NSManagedObjectContext {
|
||||
do {
|
||||
|
||||
try context.obtainPermanentIDsForObjects(Array(insertedObjects))
|
||||
return
|
||||
}
|
||||
catch {
|
||||
|
||||
|
||||
@@ -37,24 +37,17 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
if object.objectID.temporaryID {
|
||||
|
||||
var objectIDError: NSError?
|
||||
let didSucceed = withExtendedLifetime(self) { (context: NSManagedObjectContext) -> Bool in
|
||||
do {
|
||||
|
||||
do {
|
||||
try withExtendedLifetime(self) { (context: NSManagedObjectContext) -> Void in
|
||||
|
||||
try context.obtainPermanentIDsForObjects([object])
|
||||
return true
|
||||
}
|
||||
catch {
|
||||
|
||||
objectIDError = error as NSError
|
||||
return false
|
||||
}
|
||||
}
|
||||
if didSucceed != true {
|
||||
catch {
|
||||
|
||||
CoreStore.handleError(
|
||||
objectIDError ?? NSError(coreStoreErrorCode: .UnknownError),
|
||||
error as NSError,
|
||||
"Failed to obtain permanent ID for object."
|
||||
)
|
||||
return nil
|
||||
@@ -75,7 +68,6 @@ internal extension NSManagedObjectContext {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal func fetchOne<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> T? {
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ internal extension NSManagedObjectContext {
|
||||
}
|
||||
set {
|
||||
|
||||
if self.parentContext != nil {
|
||||
guard self.parentContext == nil else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
self.performBlockAndWait { [unowned self] () -> Void in
|
||||
|
||||
if !self.hasChanges {
|
||||
guard self.hasChanges else {
|
||||
|
||||
return
|
||||
}
|
||||
@@ -115,7 +115,7 @@ internal extension NSManagedObjectContext {
|
||||
|
||||
self.performBlock { () -> Void in
|
||||
|
||||
if !self.hasChanges {
|
||||
guard self.hasChanges else {
|
||||
|
||||
if let completion = completion {
|
||||
|
||||
|
||||
@@ -192,14 +192,15 @@ internal extension NSManagedObjectModel {
|
||||
|
||||
@nonobjc internal subscript(metadata: [String: AnyObject]) -> NSManagedObjectModel? {
|
||||
|
||||
if let modelHashes = metadata[NSStoreModelVersionHashesKey] as? [String : NSData] {
|
||||
guard let modelHashes = metadata[NSStoreModelVersionHashesKey] as? [String : NSData] else {
|
||||
|
||||
for modelVersion in self.modelVersions ?? [] {
|
||||
return nil
|
||||
}
|
||||
for modelVersion in self.modelVersions ?? [] {
|
||||
|
||||
if let versionModel = self[modelVersion] where modelHashes == versionModel.entityVersionHashesByName {
|
||||
|
||||
if let versionModel = self[modelVersion] where modelHashes == versionModel.entityVersionHashesByName {
|
||||
|
||||
return versionModel
|
||||
}
|
||||
return versionModel
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -117,34 +117,30 @@ public extension DataStack {
|
||||
let coordinator = self.coordinator;
|
||||
if let store = coordinator.persistentStoreForURL(fileURL) {
|
||||
|
||||
if store.type == NSSQLiteStoreType
|
||||
&& store.configurationName == (configuration ?? Into.defaultConfigurationName) {
|
||||
guard store.type == NSSQLiteStoreType
|
||||
&& store.configurationName == (configuration ?? Into.defaultConfigurationName) else {
|
||||
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(PersistentStoreResult(store))
|
||||
}
|
||||
return nil
|
||||
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
|
||||
CoreStore.handleError(
|
||||
error,
|
||||
"Failed to add SQLite \(typeName(NSPersistentStore)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
|
||||
CoreStore.handleError(
|
||||
error,
|
||||
"Failed to add SQLite \(typeName(NSPersistentStore)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
|
||||
)
|
||||
throw error
|
||||
GCDQueue.Main.async {
|
||||
|
||||
completion(PersistentStoreResult(store))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
do {
|
||||
|
||||
try fileManager.createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
}
|
||||
catch _ { }
|
||||
_ = try? fileManager.createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
|
||||
do {
|
||||
|
||||
|
||||
@@ -166,12 +166,10 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
var valid = true
|
||||
for version in elements {
|
||||
|
||||
if let lastVersion = lastVersion {
|
||||
|
||||
if let _ = versionTree.updateValue(version, forKey: lastVersion) {
|
||||
if let lastVersion = lastVersion,
|
||||
let _ = versionTree.updateValue(version, forKey: lastVersion) {
|
||||
|
||||
valid = false
|
||||
}
|
||||
}
|
||||
lastVersion = version
|
||||
}
|
||||
@@ -203,11 +201,11 @@ public struct MigrationChain: NilLiteralConvertible, StringLiteralConvertible, D
|
||||
|
||||
internal func nextVersionFrom(version: String) -> String? {
|
||||
|
||||
if let nextVersion = self.versionTree[version] where nextVersion != version {
|
||||
guard let nextVersion = self.versionTree[version] where nextVersion != version else {
|
||||
|
||||
return nextVersion
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return nextVersion
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -321,10 +321,11 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitorWillChange(monitor)
|
||||
return
|
||||
}
|
||||
observer.listMonitorWillChange(monitor)
|
||||
}
|
||||
)
|
||||
self.registerChangeNotification(
|
||||
@@ -333,10 +334,11 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitorDidChange(monitor)
|
||||
return
|
||||
}
|
||||
observer.listMonitorDidChange(monitor)
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -367,10 +369,11 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitorWillChange(monitor)
|
||||
return
|
||||
}
|
||||
observer.listMonitorWillChange(monitor)
|
||||
}
|
||||
)
|
||||
self.registerChangeNotification(
|
||||
@@ -379,10 +382,11 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitorDidChange(monitor)
|
||||
return
|
||||
}
|
||||
observer.listMonitorDidChange(monitor)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -392,14 +396,15 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object, indexPath, newIndexPath) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didInsertObject: object,
|
||||
toIndexPath: newIndexPath!
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didInsertObject: object,
|
||||
toIndexPath: newIndexPath!
|
||||
)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -408,14 +413,15 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object, indexPath, newIndexPath) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didDeleteObject: object,
|
||||
fromIndexPath: indexPath!
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didDeleteObject: object,
|
||||
fromIndexPath: indexPath!
|
||||
)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -424,14 +430,15 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object, indexPath, newIndexPath) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didUpdateObject: object,
|
||||
atIndexPath: indexPath!
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didUpdateObject: object,
|
||||
atIndexPath: indexPath!
|
||||
)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -440,15 +447,16 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object, indexPath, newIndexPath) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didMoveObject: object,
|
||||
fromIndexPath: indexPath!,
|
||||
toIndexPath: newIndexPath!
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didMoveObject: object,
|
||||
fromIndexPath: indexPath!,
|
||||
toIndexPath: newIndexPath!
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -479,10 +487,11 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitorWillChange(monitor)
|
||||
return
|
||||
}
|
||||
observer.listMonitorWillChange(monitor)
|
||||
}
|
||||
)
|
||||
self.registerChangeNotification(
|
||||
@@ -491,10 +500,11 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitorDidChange(monitor)
|
||||
return
|
||||
}
|
||||
observer.listMonitorDidChange(monitor)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -504,14 +514,15 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object, indexPath, newIndexPath) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didInsertObject: object,
|
||||
toIndexPath: newIndexPath!
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didInsertObject: object,
|
||||
toIndexPath: newIndexPath!
|
||||
)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -520,14 +531,15 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object, indexPath, newIndexPath) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didDeleteObject: object,
|
||||
fromIndexPath: indexPath!
|
||||
)
|
||||
guard let observer = observer else {
|
||||
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didDeleteObject: object,
|
||||
fromIndexPath: indexPath!
|
||||
)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -536,14 +548,15 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object, indexPath, newIndexPath) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didUpdateObject: object,
|
||||
atIndexPath: indexPath!
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didUpdateObject: object,
|
||||
atIndexPath: indexPath!
|
||||
)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -552,15 +565,16 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object, indexPath, newIndexPath) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didMoveObject: object,
|
||||
fromIndexPath: indexPath!,
|
||||
toIndexPath: newIndexPath!
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didMoveObject: object,
|
||||
fromIndexPath: indexPath!,
|
||||
toIndexPath: newIndexPath!
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -570,14 +584,15 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, sectionInfo, sectionIndex) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didInsertSection: sectionInfo,
|
||||
toSectionIndex: sectionIndex
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didInsertSection: sectionInfo,
|
||||
toSectionIndex: sectionIndex
|
||||
)
|
||||
}
|
||||
)
|
||||
self.registerSectionNotification(
|
||||
@@ -586,14 +601,15 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, sectionInfo, sectionIndex) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didDeleteSection: sectionInfo,
|
||||
fromSectionIndex: sectionIndex
|
||||
)
|
||||
return
|
||||
}
|
||||
observer.listMonitor(
|
||||
monitor,
|
||||
didDeleteSection: sectionInfo,
|
||||
fromSectionIndex: sectionIndex
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -686,10 +702,11 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
object: self,
|
||||
closure: { [weak self] (note) -> Void in
|
||||
|
||||
if let strongSelf = self {
|
||||
guard let strongSelf = self else {
|
||||
|
||||
callback(monitor: strongSelf)
|
||||
return
|
||||
}
|
||||
callback(monitor: strongSelf)
|
||||
}
|
||||
),
|
||||
forKey: notificationKey,
|
||||
@@ -705,17 +722,18 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
object: self,
|
||||
closure: { [weak self] (note) -> Void in
|
||||
|
||||
if let strongSelf = self,
|
||||
guard let strongSelf = self,
|
||||
let userInfo = note.userInfo,
|
||||
let object = userInfo[UserInfoKeyObject] as? T {
|
||||
let object = userInfo[UserInfoKeyObject] as? T else {
|
||||
|
||||
callback(
|
||||
monitor: strongSelf,
|
||||
object: object,
|
||||
indexPath: userInfo[UserInfoKeyIndexPath] as? NSIndexPath,
|
||||
newIndexPath: userInfo[UserInfoKeyNewIndexPath] as? NSIndexPath
|
||||
)
|
||||
return
|
||||
}
|
||||
callback(
|
||||
monitor: strongSelf,
|
||||
object: object,
|
||||
indexPath: userInfo[UserInfoKeyIndexPath] as? NSIndexPath,
|
||||
newIndexPath: userInfo[UserInfoKeyNewIndexPath] as? NSIndexPath
|
||||
)
|
||||
}
|
||||
),
|
||||
forKey: notificationKey,
|
||||
@@ -731,17 +749,18 @@ public final class ListMonitor<T: NSManagedObject> {
|
||||
object: self,
|
||||
closure: { [weak self] (note) -> Void in
|
||||
|
||||
if let strongSelf = self,
|
||||
guard let strongSelf = self,
|
||||
let userInfo = note.userInfo,
|
||||
let sectionInfo = userInfo[UserInfoKeySectionInfo] as? NSFetchedResultsSectionInfo,
|
||||
let sectionIndex = (userInfo[UserInfoKeySectionIndex] as? NSNumber)?.integerValue {
|
||||
let sectionIndex = (userInfo[UserInfoKeySectionIndex] as? NSNumber)?.integerValue else {
|
||||
|
||||
callback(
|
||||
monitor: strongSelf,
|
||||
sectionInfo: sectionInfo,
|
||||
sectionIndex: sectionIndex
|
||||
)
|
||||
return
|
||||
}
|
||||
callback(
|
||||
monitor: strongSelf,
|
||||
sectionInfo: sectionInfo,
|
||||
sectionIndex: sectionIndex
|
||||
)
|
||||
}
|
||||
),
|
||||
forKey: notificationKey,
|
||||
|
||||
@@ -28,20 +28,6 @@ import CoreData
|
||||
import GCDKit
|
||||
|
||||
|
||||
private let ObjectMonitorWillChangeObjectNotification = "ObjectMonitorWillChangeObjectNotification"
|
||||
private let ObjectMonitorDidDeleteObjectNotification = "ObjectMonitorDidDeleteObjectNotification"
|
||||
private let ObjectMonitorDidUpdateObjectNotification = "ObjectMonitorDidUpdateObjectNotification"
|
||||
|
||||
private let UserInfoKeyObject = "UserInfoKeyObject"
|
||||
|
||||
private struct NotificationKey {
|
||||
|
||||
static var willChangeObject: Void?
|
||||
static var didDeleteObject: Void?
|
||||
static var didUpdateObject: Void?
|
||||
}
|
||||
|
||||
|
||||
// MARK: - ObjectMonitor
|
||||
|
||||
/**
|
||||
@@ -100,10 +86,11 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor) -> Void in
|
||||
|
||||
if let object = monitor.object, let observer = observer {
|
||||
guard let object = monitor.object, let observer = observer else {
|
||||
|
||||
observer.objectMonitor(monitor, willUpdateObject: object)
|
||||
return
|
||||
}
|
||||
observer.objectMonitor(monitor, willUpdateObject: object)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -112,10 +99,11 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak observer] (monitor, object) -> Void in
|
||||
|
||||
if let observer = observer {
|
||||
guard let observer = observer else {
|
||||
|
||||
observer.objectMonitor(monitor, didDeleteObject: object)
|
||||
return
|
||||
}
|
||||
observer.objectMonitor(monitor, didDeleteObject: object)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -124,27 +112,29 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
toObserver: observer,
|
||||
callback: { [weak self, weak observer] (monitor, object) -> Void in
|
||||
|
||||
if let strongSelf = self, let observer = observer {
|
||||
guard let strongSelf = self, let observer = observer else {
|
||||
|
||||
let previousCommitedAttributes = strongSelf.lastCommittedAttributes
|
||||
let currentCommitedAttributes = object.committedValuesForKeys(nil) as! [String: NSObject]
|
||||
|
||||
var changedKeys = Set<String>()
|
||||
for key in currentCommitedAttributes.keys {
|
||||
|
||||
if previousCommitedAttributes[key] != currentCommitedAttributes[key] {
|
||||
|
||||
changedKeys.insert(key)
|
||||
}
|
||||
}
|
||||
|
||||
strongSelf.lastCommittedAttributes = currentCommitedAttributes
|
||||
observer.objectMonitor(
|
||||
monitor,
|
||||
didUpdateObject: object,
|
||||
changedPersistentKeys: changedKeys
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
let previousCommitedAttributes = strongSelf.lastCommittedAttributes
|
||||
let currentCommitedAttributes = object.committedValuesForKeys(nil) as! [String: NSObject]
|
||||
|
||||
var changedKeys = Set<String>()
|
||||
for key in currentCommitedAttributes.keys {
|
||||
|
||||
if previousCommitedAttributes[key] != currentCommitedAttributes[key] {
|
||||
|
||||
changedKeys.insert(key)
|
||||
}
|
||||
}
|
||||
|
||||
strongSelf.lastCommittedAttributes = currentCommitedAttributes
|
||||
observer.objectMonitor(
|
||||
monitor,
|
||||
didUpdateObject: object,
|
||||
changedPersistentKeys: changedKeys
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -222,10 +212,11 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
object: self,
|
||||
closure: { [weak self] (note) -> Void in
|
||||
|
||||
if let strongSelf = self {
|
||||
guard let strongSelf = self else {
|
||||
|
||||
callback(monitor: strongSelf)
|
||||
return
|
||||
}
|
||||
callback(monitor: strongSelf)
|
||||
}
|
||||
),
|
||||
forKey: notificationKey,
|
||||
@@ -241,15 +232,13 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
object: self,
|
||||
closure: { [weak self] (note) -> Void in
|
||||
|
||||
if let strongSelf = self,
|
||||
guard let strongSelf = self,
|
||||
let userInfo = note.userInfo,
|
||||
let object = userInfo[UserInfoKeyObject] as? T {
|
||||
let object = userInfo[UserInfoKeyObject] as? T else {
|
||||
|
||||
callback(
|
||||
monitor: strongSelf,
|
||||
object: object
|
||||
)
|
||||
return
|
||||
}
|
||||
callback(monitor: strongSelf, object: object)
|
||||
}
|
||||
),
|
||||
forKey: notificationKey,
|
||||
@@ -352,3 +341,17 @@ private final class FetchedResultsControllerDelegate: NSObject, NSFetchedResults
|
||||
self.fetchedResultsController?.delegate = nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private let ObjectMonitorWillChangeObjectNotification = "ObjectMonitorWillChangeObjectNotification"
|
||||
private let ObjectMonitorDidDeleteObjectNotification = "ObjectMonitorDidDeleteObjectNotification"
|
||||
private let ObjectMonitorDidUpdateObjectNotification = "ObjectMonitorDidUpdateObjectNotification"
|
||||
|
||||
private let UserInfoKeyObject = "UserInfoKeyObject"
|
||||
|
||||
private struct NotificationKey {
|
||||
|
||||
static var willChangeObject: Void?
|
||||
static var didDeleteObject: Void?
|
||||
static var didUpdateObject: Void?
|
||||
}
|
||||
|
||||
@@ -189,31 +189,27 @@ public final class DataStack {
|
||||
let coordinator = self.coordinator;
|
||||
if let store = coordinator.persistentStoreForURL(fileURL) {
|
||||
|
||||
if store.type == NSSQLiteStoreType
|
||||
&& store.configurationName == (configuration ?? Into.defaultConfigurationName) {
|
||||
guard store.type == NSSQLiteStoreType
|
||||
&& store.configurationName == (configuration ?? Into.defaultConfigurationName) else {
|
||||
|
||||
return store
|
||||
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
|
||||
CoreStore.handleError(
|
||||
error,
|
||||
"Failed to add SQLite \(typeName(NSPersistentStore)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
|
||||
)
|
||||
|
||||
throw error
|
||||
}
|
||||
|
||||
let error = NSError(coreStoreErrorCode: .DifferentPersistentStoreExistsAtURL)
|
||||
CoreStore.handleError(
|
||||
error,
|
||||
"Failed to add SQLite \(typeName(NSPersistentStore)) at \"\(fileURL)\" because a different \(typeName(NSPersistentStore)) at that URL already exists."
|
||||
)
|
||||
|
||||
throw error
|
||||
return store
|
||||
}
|
||||
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
do {
|
||||
|
||||
try fileManager.createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
}
|
||||
catch _ { }
|
||||
_ = try? fileManager.createDirectoryAtURL(
|
||||
fileURL.URLByDeletingLastPathComponent!,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
|
||||
var store: NSPersistentStore?
|
||||
var storeError: NSError?
|
||||
@@ -377,11 +373,7 @@ public final class DataStack {
|
||||
|
||||
for store in self.coordinator.persistentStores {
|
||||
|
||||
do {
|
||||
|
||||
try self.coordinator.removePersistentStore(store)
|
||||
}
|
||||
catch _ { }
|
||||
_ = try? self.coordinator.removePersistentStore(store)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user