2021-02-08 11:29:27 +01:00
|
|
|
//
|
|
|
|
// ManagedObjectContextObjectsDidChange.swift
|
|
|
|
// CoreDataStack
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/8.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
public struct ManagedObjectContextObjectsDidChangeNotification {
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
public let notification: Foundation.Notification
|
2021-02-08 11:29:27 +01:00
|
|
|
public let managedObjectContext: NSManagedObjectContext
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
public init?(notification: Foundation.Notification) {
|
2021-02-08 11:29:27 +01:00
|
|
|
guard notification.name == .NSManagedObjectContextObjectsDidChange,
|
|
|
|
let managedObjectContext = notification.object as? NSManagedObjectContext else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
self.notification = notification
|
|
|
|
self.managedObjectContext = managedObjectContext
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ManagedObjectContextObjectsDidChangeNotification {
|
|
|
|
|
|
|
|
public var insertedObjects: Set<NSManagedObject> {
|
|
|
|
return objects(forKey: NSInsertedObjectsKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var updatedObjects: Set<NSManagedObject> {
|
|
|
|
return objects(forKey: NSUpdatedObjectsKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var deletedObjects: Set<NSManagedObject> {
|
|
|
|
return objects(forKey: NSDeletedObjectsKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var refreshedObjects: Set<NSManagedObject> {
|
|
|
|
return objects(forKey: NSRefreshedObjectsKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var invalidedObjects: Set<NSManagedObject> {
|
|
|
|
return objects(forKey: NSInvalidatedObjectsKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var invalidatedAllObjects: Bool {
|
|
|
|
return notification.userInfo?[NSInvalidatedAllObjectsKey] != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ManagedObjectContextObjectsDidChangeNotification {
|
|
|
|
|
|
|
|
private func objects(forKey key: String) -> Set<NSManagedObject> {
|
|
|
|
return notification.userInfo?[key] as? Set<NSManagedObject> ?? Set()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|