ListMonitor and ObjectMonitor objective C bridge

This commit is contained in:
John Estropia
2016-03-29 14:15:57 +09:00
parent 9d7960e674
commit e99d19d2ac
15 changed files with 718 additions and 103 deletions

View File

@@ -33,7 +33,7 @@ import CoreData
public extension CoreStore {
/**
Using the `defaultStack`, creates a `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
Using the `defaultStack`, creates an `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
- parameter object: the `NSManagedObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object`
@@ -52,9 +52,9 @@ public extension CoreStore {
- returns: a `ListMonitor` instance that monitors changes to the list
*/
@warn_unused_result
public static func monitorList<T: NSManagedObject>(from: From<T>, _ queryClauses: FetchClause...) -> ListMonitor<T> {
public static func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
return self.defaultStack.monitorList(from, queryClauses)
return self.defaultStack.monitorList(from, fetchClauses)
}
/**
@@ -65,9 +65,9 @@ public extension CoreStore {
- returns: a `ListMonitor` instance that monitors changes to the list
*/
@warn_unused_result
public static func monitorList<T: NSManagedObject>(from: From<T>, _ queryClauses: [FetchClause]) -> ListMonitor<T> {
public static func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
return self.defaultStack.monitorList(from, queryClauses)
return self.defaultStack.monitorList(from, fetchClauses)
}
/**

View File

@@ -36,7 +36,7 @@ import CoreData
public extension DataStack {
/**
Creates a `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
Creates an `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
- parameter object: the `NSManagedObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object`
@@ -48,11 +48,7 @@ public extension DataStack {
NSThread.isMainThread(),
"Attempted to observe objects from \(typeName(self)) outside the main thread."
)
return ObjectMonitor(
dataStack: self,
object: object
)
return ObjectMonitor(dataStack: self, object: object)
}
/**
@@ -83,15 +79,17 @@ public extension DataStack {
"Attempted to observe objects from \(typeName(self)) outside the main thread."
)
CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0,
fetchClauses.contains { $0 is OrderBy },
"A ListMonitor requires an OrderBy clause."
)
return ListMonitor(
dataStack: self,
from: from,
sectionBy: nil,
fetchClauses: fetchClauses
applyFetchClauses: { fetchRequest in
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
}
)
}
@@ -121,15 +119,17 @@ public extension DataStack {
"Attempted to observe objects from \(typeName(self)) outside the main thread."
)
CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0,
fetchClauses.contains { $0 is OrderBy },
"A ListMonitor requires an OrderBy clause."
)
_ = ListMonitor(
dataStack: self,
from: from,
sectionBy: nil,
fetchClauses: fetchClauses,
applyFetchClauses: { fetchRequest in
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
},
createAsynchronously: createAsynchronously
)
}
@@ -164,7 +164,7 @@ public extension DataStack {
"Attempted to observe objects from \(typeName(self)) outside the main thread."
)
CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0,
fetchClauses.contains { $0 is OrderBy },
"A ListMonitor requires an OrderBy clause."
)
@@ -172,7 +172,10 @@ public extension DataStack {
dataStack: self,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses
applyFetchClauses: { fetchRequest in
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
}
)
}
@@ -204,7 +207,7 @@ public extension DataStack {
"Attempted to observe objects from \(typeName(self)) outside the main thread."
)
CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0,
fetchClauses.contains { $0 is OrderBy },
"A ListMonitor requires an OrderBy clause."
)
@@ -212,7 +215,10 @@ public extension DataStack {
dataStack: self,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses,
applyFetchClauses: { fetchRequest in
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
},
createAsynchronously: createAsynchronously
)
}

View File

@@ -647,50 +647,50 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
internal var didInsertSectionKey: Void?
internal var didDeleteSectionKey: Void?
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause]) {
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
self.init(
context: dataStack.mainContext,
transactionQueue: dataStack.childTransactionQueue,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses,
applyFetchClauses: applyFetchClauses,
createAsynchronously: nil
)
}
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause], createAsynchronously: (ListMonitor<T>) -> Void) {
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void, createAsynchronously: (ListMonitor<T>) -> Void) {
self.init(
context: dataStack.mainContext,
transactionQueue: dataStack.childTransactionQueue,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses,
applyFetchClauses: applyFetchClauses,
createAsynchronously: createAsynchronously
)
}
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause]) {
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
self.init(
context: unsafeTransaction.context,
transactionQueue: unsafeTransaction.transactionQueue,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses,
applyFetchClauses: applyFetchClauses,
createAsynchronously: nil
)
}
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause], createAsynchronously: (ListMonitor<T>) -> Void) {
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void, createAsynchronously: (ListMonitor<T>) -> Void) {
self.init(
context: unsafeTransaction.context,
transactionQueue: unsafeTransaction.transactionQueue,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses,
applyFetchClauses: applyFetchClauses,
createAsynchronously: createAsynchronously
)
}
@@ -984,7 +984,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
setAssociatedRetainedObject(nilValue, forKey: &self.didDeleteSectionKey, inObject: observer)
}
internal func refetch(applyClauses: (fetchRequest: NSFetchRequest) -> Void) {
internal func refetch(applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
CoreStore.assert(
NSThread.isMainThread(),
@@ -1000,6 +1000,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
object: self
)
}
self.applyFetchClauses = applyFetchClauses
self.taskGroup.notify(.Main) { [weak self] () -> Void in
@@ -1009,7 +1010,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
}
self.fetchedResultsControllerDelegate.enabled = false
applyClauses(fetchRequest: self.fetchedResultsController.fetchRequest)
self.applyFetchClauses(fetchRequest: self.fetchedResultsController.fetchRequest)
self.transactionQueue.async { [weak self] in
@@ -1055,6 +1056,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
private var observerForDidChangePersistentStore: NotificationObserver!
private let taskGroup = GCDGroup()
private let transactionQueue: GCDQueue
private var applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void
private var isPersistentStoreChanging: Bool = false {
@@ -1077,7 +1079,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
}
}
private init(context: NSManagedObjectContext, transactionQueue: GCDQueue, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause], createAsynchronously: ((ListMonitor<T>) -> Void)?) {
private init(context: NSManagedObjectContext, transactionQueue: GCDQueue, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void, createAsynchronously: ((ListMonitor<T>) -> Void)?) {
let fetchRequest = NSFetchRequest()
fetchRequest.fetchLimit = 0
@@ -1091,7 +1093,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
fetchRequest: fetchRequest,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses
applyFetchClauses: applyFetchClauses
)
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate()
@@ -1108,6 +1110,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
self.sectionIndexTransformer = { $0 }
}
self.transactionQueue = transactionQueue
self.applyFetchClauses = applyFetchClauses
fetchedResultsControllerDelegate.handler = self
fetchedResultsControllerDelegate.fetchedResultsController = fetchedResultsController
@@ -1134,7 +1137,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
return
}
self.refetch(fetchClauses)
self.refetch(self.applyFetchClauses)
}
)
@@ -1157,7 +1160,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
if previousStores != currentStores {
self.refetch(fetchClauses)
self.refetch(self.applyFetchClauses)
}
}

View File

@@ -74,13 +74,63 @@ public final class ObjectMonitor<T: NSManagedObject> {
*/
public func addObserver<U: ObjectObserver where U.ObjectEntityType == T>(observer: U) {
self.unregisterObserver(observer)
self.registerObserver(
observer,
willChangeObject: { (observer, monitor, object) in
observer.objectMonitor(monitor, willUpdateObject: object)
},
didDeleteObject: { (observer, monitor, object) in
observer.objectMonitor(monitor, didDeleteObject: object)
},
didUpdateObject: { (observer, monitor, object, changedPersistentKeys) in
observer.objectMonitor(monitor, didUpdateObject: object, changedPersistentKeys: changedPersistentKeys)
}
)
}
/**
Unregisters an `ObjectObserver` from receiving notifications for changes to the receiver's `object`.
For thread safety, this method needs to be called from the main thread. An assertion failure will occur (on debug builds only) if called from any thread other than the main thread.
- parameter observer: an `ObjectObserver` to unregister notifications to
*/
public func removeObserver<U: ObjectObserver where U.ObjectEntityType == T>(observer: U) {
self.unregisterObserver(observer)
}
// MARK: Hashable
public var hashValue: Int {
return ObjectIdentifier(self).hashValue
}
// MARK: Internal
internal convenience init(dataStack: DataStack, object: T) {
self.init(context: dataStack.mainContext, object: object)
}
internal convenience init(unsafeTransaction: UnsafeDataTransaction, object: T) {
self.init(context: unsafeTransaction.context, object: object)
}
internal func registerObserver<U: AnyObject>(observer: U, willChangeObject: (observer: U, monitor: ObjectMonitor<T>, object: T) -> Void, didDeleteObject: (observer: U, monitor: ObjectMonitor<T>, object: T) -> Void, didUpdateObject: (observer: U, monitor: ObjectMonitor<T>, object: T, changedPersistentKeys: Set<String>) -> Void) {
CoreStore.assert(
NSThread.isMainThread(),
"Attempted to add an observer of type \(typeName(observer)) outside the main thread."
)
self.removeObserver(observer)
self.registerChangeNotification(
&self.willChangeObjectKey,
name: ObjectMonitorWillChangeObjectNotification,
@@ -91,7 +141,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
return
}
observer.objectMonitor(monitor, willUpdateObject: object)
willChangeObject(observer: observer, monitor: monitor, object: object)
}
)
self.registerObjectNotification(
@@ -104,7 +154,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
return
}
observer.objectMonitor(monitor, didDeleteObject: object)
didDeleteObject(observer: observer, monitor: monitor, object: object)
}
)
self.registerObjectNotification(
@@ -131,23 +181,17 @@ public final class ObjectMonitor<T: NSManagedObject> {
}
self.lastCommittedAttributes = currentCommitedAttributes
observer.objectMonitor(
monitor,
didUpdateObject: object,
didUpdateObject(
observer: observer,
monitor: monitor,
object: object,
changedPersistentKeys: changedKeys
)
}
)
}
/**
Unregisters an `ObjectObserver` from receiving notifications for changes to the receiver's `object`.
For thread safety, this method needs to be called from the main thread. An assertion failure will occur (on debug builds only) if called from any thread other than the main thread.
- parameter observer: an `ObjectObserver` to unregister notifications to
*/
public func removeObserver<U: ObjectObserver where U.ObjectEntityType == T>(observer: U) {
internal func unregisterObserver(observer: AnyObject) {
CoreStore.assert(
NSThread.isMainThread(),
@@ -160,45 +204,9 @@ public final class ObjectMonitor<T: NSManagedObject> {
setAssociatedRetainedObject(nilValue, forKey: &self.didUpdateObjectKey, inObject: observer)
}
// MARK: Internal
internal convenience init(dataStack: DataStack, object: T) {
internal func upcast() -> ObjectMonitor<NSManagedObject> {
self.init(context: dataStack.mainContext, object: object)
}
internal convenience init(unsafeTransaction: UnsafeDataTransaction, object: T) {
self.init(context: unsafeTransaction.context, object: object)
}
private init(context: NSManagedObjectContext, object: T) {
let fetchRequest = NSFetchRequest()
fetchRequest.entity = object.entity
fetchRequest.fetchLimit = 0
fetchRequest.resultType = .ManagedObjectResultType
fetchRequest.sortDescriptors = []
fetchRequest.includesPendingChanges = false
fetchRequest.shouldRefreshRefetchedObjects = true
let fetchedResultsController = CoreStoreFetchedResultsController(
context: context,
fetchRequest: fetchRequest,
fetchClauses: [Where("SELF", isEqualTo: object.objectID)]
)
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate()
self.fetchedResultsController = fetchedResultsController
self.fetchedResultsControllerDelegate = fetchedResultsControllerDelegate
fetchedResultsControllerDelegate.handler = self
fetchedResultsControllerDelegate.fetchedResultsController = fetchedResultsController
try! fetchedResultsController.performFetchFromSpecifiedStores()
self.lastCommittedAttributes = (self.object?.committedValuesForKeys(nil) as? [String: NSObject]) ?? [:]
return unsafeBitCast(self, ObjectMonitor<NSManagedObject>.self)
}
deinit {
@@ -217,6 +225,35 @@ public final class ObjectMonitor<T: NSManagedObject> {
private var didDeleteObjectKey: Void?
private var didUpdateObjectKey: Void?
private init(context: NSManagedObjectContext, object: T) {
let fetchRequest = NSFetchRequest()
fetchRequest.entity = object.entity
fetchRequest.fetchLimit = 0
fetchRequest.resultType = .ManagedObjectResultType
fetchRequest.sortDescriptors = []
fetchRequest.includesPendingChanges = false
fetchRequest.shouldRefreshRefetchedObjects = true
let objectID = object.objectID
let fetchedResultsController = CoreStoreFetchedResultsController(
context: context,
fetchRequest: fetchRequest,
applyFetchClauses: Where("SELF", isEqualTo: objectID).applyToFetchRequest
)
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate()
self.fetchedResultsController = fetchedResultsController
self.fetchedResultsControllerDelegate = fetchedResultsControllerDelegate
fetchedResultsControllerDelegate.handler = self
fetchedResultsControllerDelegate.fetchedResultsController = fetchedResultsController
try! fetchedResultsController.performFetchFromSpecifiedStores()
self.lastCommittedAttributes = (self.object?.committedValuesForKeys(nil) as? [String: NSObject]) ?? [:]
}
private func registerChangeNotification(notificationKey: UnsafePointer<Void>, name: String, toObserver observer: AnyObject, callback: (monitor: ObjectMonitor<T>) -> Void) {
setAssociatedRetainedObject(

View File

@@ -54,6 +54,7 @@ public struct SectionBy {
/**
Initializes a `SectionBy` clause with the key path to use to group `ListMonitor` objects into sections, and a closure to transform the value for the key path to an appropriate section name
- Important: Some utilities (such as `ListMonitor`s) may keep `SectionBy`s in memory and may thus introduce retain cycles if reference captures are not handled properly.
- parameter sectionKeyPath: the key path to use to group the objects into sections
- parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name

View File

@@ -82,7 +82,10 @@ public extension UnsafeDataTransaction {
unsafeTransaction: self,
from: from,
sectionBy: nil,
fetchClauses: fetchClauses
applyFetchClauses: { fetchRequest in
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
}
)
}
@@ -116,7 +119,10 @@ public extension UnsafeDataTransaction {
unsafeTransaction: self,
from: from,
sectionBy: nil,
fetchClauses: fetchClauses,
applyFetchClauses: { fetchRequest in
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
},
createAsynchronously: createAsynchronously
)
}
@@ -155,7 +161,10 @@ public extension UnsafeDataTransaction {
unsafeTransaction: self,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses
applyFetchClauses: { fetchRequest in
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
}
)
}
@@ -191,7 +200,10 @@ public extension UnsafeDataTransaction {
unsafeTransaction: self,
from: from,
sectionBy: sectionBy,
fetchClauses: fetchClauses,
applyFetchClauses: { fetchRequest in
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
},
createAsynchronously: createAsynchronously
)
}