Unify generics usage in ListMonitor and ObjectMonitor

This commit is contained in:
John Estropia
2017-04-20 10:42:47 +09:00
parent b0e2655bdf
commit fd14a18248
7 changed files with 93 additions and 93 deletions

View File

@@ -546,7 +546,7 @@ public final class CSListMonitor: NSObject {
// MARK: - ListMonitor // MARK: - ListMonitor
@available(OSX 10.12, *) @available(OSX 10.12, *)
extension ListMonitor where T: NSManagedObject { extension ListMonitor where ListMonitor.ObjectType: NSManagedObject {
// MARK: CoreStoreSwiftType // MARK: CoreStoreSwiftType

View File

@@ -138,7 +138,7 @@ public final class CSObjectMonitor: NSObject {
// MARK: - ObjectMonitor // MARK: - ObjectMonitor
@available(OSX 10.12, *) @available(OSX 10.12, *)
extension ObjectMonitor where EntityType: NSManagedObject { extension ObjectMonitor where ObjectMonitor.ObjectType: NSManagedObject {
// MARK: CoreStoreSwiftType // MARK: CoreStoreSwiftType

View File

@@ -38,7 +38,7 @@ public extension CoreStore {
- parameter object: the `NSManagedObject` to observe changes from - parameter object: the `NSManagedObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object` - returns: a `ObjectMonitor` that monitors changes to `object`
*/ */
public static func monitorObject<T: NSManagedObject>(_ object: T) -> ObjectMonitor<T> { public static func monitorObject<T: DynamicObject>(_ object: T) -> ObjectMonitor<T> {
return self.defaultStack.monitorObject(object) return self.defaultStack.monitorObject(object)
} }
@@ -50,7 +50,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public static func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> { public static func monitorList<T: DynamicObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
return self.defaultStack.monitorList(from, fetchClauses) return self.defaultStack.monitorList(from, fetchClauses)
} }
@@ -62,7 +62,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public static func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> { public static func monitorList<T: DynamicObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
return self.defaultStack.monitorList(from, fetchClauses) return self.defaultStack.monitorList(from, fetchClauses)
} }
@@ -74,7 +74,7 @@ public extension CoreStore {
- parameter from: a `From` clause indicating the entity type - parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public static func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) { public static func monitorList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses) self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
} }
@@ -86,7 +86,7 @@ public extension CoreStore {
- parameter from: a `From` clause indicating the entity type - parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public static func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) { public static func monitorList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses) self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
} }
@@ -99,7 +99,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public static func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> { public static func monitorSectionedList<T: DynamicObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
return self.defaultStack.monitorSectionedList(from, sectionBy, fetchClauses) return self.defaultStack.monitorSectionedList(from, sectionBy, fetchClauses)
} }
@@ -112,7 +112,7 @@ public extension CoreStore {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public static func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> { public static func monitorSectionedList<T: DynamicObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
return self.defaultStack.monitorSectionedList(from, sectionBy, fetchClauses) return self.defaultStack.monitorSectionedList(from, sectionBy, fetchClauses)
} }
@@ -125,7 +125,7 @@ public extension CoreStore {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections. - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public static func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { public static func monitorSectionedList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses) self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
} }
@@ -138,7 +138,7 @@ public extension CoreStore {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections. - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public static func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { public static func monitorSectionedList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses) self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
} }

View File

@@ -38,7 +38,7 @@ public extension DataStack {
- parameter object: the `NSManagedObject` to observe changes from - parameter object: the `NSManagedObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object` - returns: a `ObjectMonitor` that monitors changes to `object`
*/ */
public func monitorObject<T: NSManagedObject>(_ object: T) -> ObjectMonitor<T> { public func monitorObject<T: DynamicObject>(_ object: T) -> ObjectMonitor<T> {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,
@@ -54,7 +54,7 @@ public extension DataStack {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> { public func monitorList<T: DynamicObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
return self.monitorList(from, fetchClauses) return self.monitorList(from, fetchClauses)
} }
@@ -66,7 +66,7 @@ public extension DataStack {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> { public func monitorList<T: DynamicObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,
@@ -95,7 +95,7 @@ public extension DataStack {
- parameter from: a `From` clause indicating the entity type - parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) { public func monitorList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses) self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
} }
@@ -107,7 +107,7 @@ public extension DataStack {
- parameter from: a `From` clause indicating the entity type - parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) { public func monitorList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,
@@ -138,7 +138,7 @@ public extension DataStack {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> { public func monitorSectionedList<T: DynamicObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
return self.monitorSectionedList(from, sectionBy, fetchClauses) return self.monitorSectionedList(from, sectionBy, fetchClauses)
} }
@@ -151,7 +151,7 @@ public extension DataStack {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> { public func monitorSectionedList<T: DynamicObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,
@@ -182,7 +182,7 @@ public extension DataStack {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections. - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { public func monitorSectionedList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
self.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses) self.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
} }
@@ -195,7 +195,7 @@ public extension DataStack {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections. - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { public func monitorSectionedList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,

View File

@@ -67,14 +67,14 @@ import CoreData
In the example above, both `person1` and `person2` will contain the object at section=2, index=3. In the example above, both `person1` and `person2` will contain the object at section=2, index=3.
*/ */
@available(OSX 10.12, *) @available(OSX 10.12, *)
public final class ListMonitor<T: DynamicObject>: Hashable { public final class ListMonitor<D: DynamicObject>: Hashable {
// MARK: Public (Accessors) // MARK: Public (Accessors)
/** /**
The type for the objects contained bye the `ListMonitor` The type for the objects contained bye the `ListMonitor`
*/ */
public typealias ObjectType = T public typealias ObjectType = D
/** /**
Returns the object at the given index within the first section. This subscript indexer is typically used for `ListMonitor`s created with `monitorList(_:)`. Returns the object at the given index within the first section. This subscript indexer is typically used for `ListMonitor`s created with `monitorList(_:)`.
@@ -82,7 +82,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter index: the index of the object. Using an index above the valid range will raise an exception. - parameter index: the index of the object. Using an index above the valid range will raise an exception.
- returns: the `NSManagedObject` at the specified index - returns: the `NSManagedObject` at the specified index
*/ */
public subscript(index: Int) -> T { public subscript(index: Int) -> ObjectType {
CoreStore.assert( CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread, !self.isPendingRefetch || Thread.isMainThread,
@@ -90,7 +90,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
if self.isSectioned { if self.isSectioned {
return T.cs_fromRaw(object: self.fetchedResultsController.fetchedObjects![index]) return ObjectType.cs_fromRaw(object: self.fetchedResultsController.fetchedObjects![index])
} }
return self[0, index] return self[0, index]
} }
@@ -101,14 +101,14 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter index: the index for the object. Using an index above the valid range will return `nil`. - parameter index: the index for the object. Using an index above the valid range will return `nil`.
- returns: the `NSManagedObject` at the specified index, or `nil` if out of bounds - returns: the `NSManagedObject` at the specified index, or `nil` if out of bounds
*/ */
public subscript(safeIndex index: Int) -> T? { public subscript(safeIndex index: Int) -> ObjectType? {
if self.isSectioned { if self.isSectioned {
let fetchedObjects = self.fetchedResultsController.fetchedObjects! let fetchedObjects = self.fetchedResultsController.fetchedObjects!
if index < fetchedObjects.count && index >= 0 { if index < fetchedObjects.count && index >= 0 {
return T.cs_fromRaw(object: fetchedObjects[index]) return ObjectType.cs_fromRaw(object: fetchedObjects[index])
} }
return nil return nil
} }
@@ -122,7 +122,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter itemIndex: the index for the object within the section. Using an `itemIndex` with an invalid range will raise an exception. - parameter itemIndex: the index for the object within the section. Using an `itemIndex` with an invalid range will raise an exception.
- returns: the `NSManagedObject` at the specified section and item index - returns: the `NSManagedObject` at the specified section and item index
*/ */
public subscript(sectionIndex: Int, itemIndex: Int) -> T { public subscript(sectionIndex: Int, itemIndex: Int) -> ObjectType {
return self[NSIndexPath(indexes: [sectionIndex, itemIndex], length: 2) as IndexPath] return self[NSIndexPath(indexes: [sectionIndex, itemIndex], length: 2) as IndexPath]
} }
@@ -134,7 +134,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter itemIndex: the index for the object within the section. Using an `itemIndex` with an invalid range will return `nil`. - parameter itemIndex: the index for the object within the section. Using an `itemIndex` with an invalid range will return `nil`.
- returns: the `NSManagedObject` at the specified section and item index, or `nil` if out of bounds - returns: the `NSManagedObject` at the specified section and item index, or `nil` if out of bounds
*/ */
public subscript(safeSectionIndex sectionIndex: Int, safeItemIndex itemIndex: Int) -> T? { public subscript(safeSectionIndex sectionIndex: Int, safeItemIndex itemIndex: Int) -> ObjectType? {
guard let section = self.sectionInfoAtIndex(safeSectionIndex: sectionIndex) else { guard let section = self.sectionInfoAtIndex(safeSectionIndex: sectionIndex) else {
@@ -144,7 +144,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
return nil return nil
} }
return T.cs_fromRaw(object: section.objects![itemIndex] as! NSManagedObject) return ObjectType.cs_fromRaw(object: section.objects![itemIndex] as! NSManagedObject)
} }
/** /**
@@ -153,13 +153,13 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter indexPath: the `NSIndexPath` for the object. Using an `indexPath` with an invalid range will raise an exception. - parameter indexPath: the `NSIndexPath` for the object. Using an `indexPath` with an invalid range will raise an exception.
- returns: the `NSManagedObject` at the specified index path - returns: the `NSManagedObject` at the specified index path
*/ */
public subscript(indexPath: IndexPath) -> T { public subscript(indexPath: IndexPath) -> ObjectType {
CoreStore.assert( CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread, !self.isPendingRefetch || Thread.isMainThread,
"Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress." "Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress."
) )
return T.cs_fromRaw(object: self.fetchedResultsController.object(at: indexPath)) return ObjectType.cs_fromRaw(object: self.fetchedResultsController.object(at: indexPath))
} }
/** /**
@@ -168,7 +168,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter indexPath: the `NSIndexPath` for the object. Using an `indexPath` with an invalid range will return `nil`. - parameter indexPath: the `NSIndexPath` for the object. Using an `indexPath` with an invalid range will return `nil`.
- returns: the `NSManagedObject` at the specified index path, or `nil` if out of bounds - returns: the `NSManagedObject` at the specified index path, or `nil` if out of bounds
*/ */
public subscript(safeIndexPath indexPath: IndexPath) -> T? { public subscript(safeIndexPath indexPath: IndexPath) -> ObjectType? {
return self[ return self[
safeSectionIndex: indexPath[0], safeSectionIndex: indexPath[0],
@@ -345,7 +345,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter object: the `NSManagedObject` to search the index of - parameter object: the `NSManagedObject` to search the index of
- returns: the index of the `NSManagedObject` if it exists in the `ListMonitor`'s fetched objects, or `nil` if not found. - returns: the index of the `NSManagedObject` if it exists in the `ListMonitor`'s fetched objects, or `nil` if not found.
*/ */
public func indexOf(_ object: T) -> Int? { public func indexOf(_ object: ObjectType) -> Int? {
CoreStore.assert( CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread, !self.isPendingRefetch || Thread.isMainThread,
@@ -364,7 +364,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter object: the `NSManagedObject` to search the index of - parameter object: the `NSManagedObject` to search the index of
- returns: the `NSIndexPath` of the `NSManagedObject` if it exists in the `ListMonitor`'s fetched objects, or `nil` if not found. - returns: the `NSIndexPath` of the `NSManagedObject` if it exists in the `ListMonitor`'s fetched objects, or `nil` if not found.
*/ */
public func indexPathOf(_ object: T) -> IndexPath? { public func indexPathOf(_ object: ObjectType) -> IndexPath? {
CoreStore.assert( CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread, !self.isPendingRefetch || Thread.isMainThread,
@@ -387,7 +387,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter observer: a `ListObserver` to send change notifications to - parameter observer: a `ListObserver` to send change notifications to
*/ */
public func addObserver<U: ListObserver>(_ observer: U) where U.ListEntityType == T { public func addObserver<U: ListObserver>(_ observer: U) where U.ListEntityType == ObjectType {
self.unregisterObserver(observer) self.unregisterObserver(observer)
self.registerObserver( self.registerObserver(
@@ -422,7 +422,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter observer: a `ListObjectObserver` to send change notifications to - parameter observer: a `ListObjectObserver` to send change notifications to
*/ */
public func addObserver<U: ListObjectObserver>(_ observer: U) where U.ListEntityType == T { public func addObserver<U: ListObjectObserver>(_ observer: U) where U.ListEntityType == ObjectType {
self.unregisterObserver(observer) self.unregisterObserver(observer)
self.registerObserver( self.registerObserver(
@@ -476,7 +476,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter observer: a `ListSectionObserver` to send change notifications to - parameter observer: a `ListSectionObserver` to send change notifications to
*/ */
public func addObserver<U: ListSectionObserver>(_ observer: U) where U.ListEntityType == T { public func addObserver<U: ListSectionObserver>(_ observer: U) where U.ListEntityType == ObjectType {
self.unregisterObserver(observer) self.unregisterObserver(observer)
self.registerObserver( self.registerObserver(
@@ -537,7 +537,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
- parameter observer: a `ListObserver` to unregister notifications to - parameter observer: a `ListObserver` to unregister notifications to
*/ */
public func removeObserver<U: ListObserver>(_ observer: U) where U.ListEntityType == T { public func removeObserver<U: ListObserver>(_ observer: U) where U.ListEntityType == ObjectType {
self.unregisterObserver(observer) self.unregisterObserver(observer)
} }
@@ -580,7 +580,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
// MARK: Equatable // MARK: Equatable
public static func == (lhs: ListMonitor<T>, rhs: ListMonitor<T>) -> Bool { public static func == (lhs: ListMonitor<ObjectType>, rhs: ListMonitor<ObjectType>) -> Bool {
return lhs.fetchedResultsController === rhs.fetchedResultsController return lhs.fetchedResultsController === rhs.fetchedResultsController
} }
@@ -590,7 +590,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
return lhs.fetchedResultsController === rhs.fetchedResultsController return lhs.fetchedResultsController === rhs.fetchedResultsController
} }
public static func ~= (lhs: ListMonitor<T>, rhs: ListMonitor<T>) -> Bool { public static func ~= (lhs: ListMonitor<ObjectType>, rhs: ListMonitor<ObjectType>) -> Bool {
return lhs.fetchedResultsController === rhs.fetchedResultsController return lhs.fetchedResultsController === rhs.fetchedResultsController
} }
@@ -611,7 +611,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
// MARK: Internal // MARK: Internal
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) { internal convenience init(dataStack: DataStack, from: From<ObjectType>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) {
self.init( self.init(
context: dataStack.mainContext, context: dataStack.mainContext,
@@ -623,7 +623,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void, createAsynchronously: @escaping (ListMonitor<T>) -> Void) { internal convenience init(dataStack: DataStack, from: From<ObjectType>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void, createAsynchronously: @escaping (ListMonitor<ObjectType>) -> Void) {
self.init( self.init(
context: dataStack.mainContext, context: dataStack.mainContext,
@@ -635,7 +635,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) { internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<ObjectType>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void) {
self.init( self.init(
context: unsafeTransaction.context, context: unsafeTransaction.context,
@@ -647,7 +647,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void, createAsynchronously: @escaping (ListMonitor<T>) -> Void) { internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<ObjectType>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void, createAsynchronously: @escaping (ListMonitor<ObjectType>) -> Void) {
self.init( self.init(
context: unsafeTransaction.context, context: unsafeTransaction.context,
@@ -659,7 +659,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor<T>) -> Void) { internal func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor<ObjectType>) -> Void) {
cs_setAssociatedRetainedObject( cs_setAssociatedRetainedObject(
NotificationObserver( NotificationObserver(
@@ -679,7 +679,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor<T>, _ object: T, _ indexPath: IndexPath?, _ newIndexPath: IndexPath?) -> Void) { internal func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor<ObjectType>, _ object: ObjectType, _ indexPath: IndexPath?, _ newIndexPath: IndexPath?) -> Void) {
cs_setAssociatedRetainedObject( cs_setAssociatedRetainedObject(
NotificationObserver( NotificationObserver(
@@ -689,7 +689,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
guard let `self` = self, guard let `self` = self,
let userInfo = note.userInfo, let userInfo = note.userInfo,
let object = userInfo[String(describing: NSManagedObject.self)] as? T else { let object = userInfo[String(describing: NSManagedObject.self)] as? ObjectType else {
return return
} }
@@ -706,7 +706,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal func registerSectionNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor<T>, _ sectionInfo: NSFetchedResultsSectionInfo, _ sectionIndex: Int) -> Void) { internal func registerSectionNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor<ObjectType>, _ sectionInfo: NSFetchedResultsSectionInfo, _ sectionIndex: Int) -> Void) {
cs_setAssociatedRetainedObject( cs_setAssociatedRetainedObject(
NotificationObserver( NotificationObserver(
@@ -729,7 +729,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal func registerObserver<U: AnyObject>(_ observer: U, willChange: @escaping (_ observer: U, _ monitor: ListMonitor<T>) -> Void, didChange: @escaping (_ observer: U, _ monitor: ListMonitor<T>) -> Void, willRefetch: @escaping (_ observer: U, _ monitor: ListMonitor<T>) -> Void, didRefetch: @escaping (_ observer: U, _ monitor: ListMonitor<T>) -> Void) { internal func registerObserver<U: AnyObject>(_ observer: U, willChange: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>) -> Void, didChange: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>) -> Void, willRefetch: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>) -> Void, didRefetch: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>) -> Void) {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,
@@ -789,7 +789,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal func registerObserver<U: AnyObject>(_ observer: U, didInsertObject: @escaping (_ observer: U, _ monitor: ListMonitor<T>, _ object: T, _ toIndexPath: IndexPath) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ListMonitor<T>, _ object: T, _ fromIndexPath: IndexPath) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ListMonitor<T>, _ object: T, _ atIndexPath: IndexPath) -> Void, didMoveObject: @escaping (_ observer: U, _ monitor: ListMonitor<T>, _ object: T, _ fromIndexPath: IndexPath, _ toIndexPath: IndexPath) -> Void) { internal func registerObserver<U: AnyObject>(_ observer: U, didInsertObject: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>, _ object: ObjectType, _ toIndexPath: IndexPath) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>, _ object: ObjectType, _ fromIndexPath: IndexPath) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>, _ object: ObjectType, _ atIndexPath: IndexPath) -> Void, didMoveObject: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>, _ object: ObjectType, _ fromIndexPath: IndexPath, _ toIndexPath: IndexPath) -> Void) {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,
@@ -850,7 +850,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
) )
} }
internal func registerObserver<U: AnyObject>(_ observer: U, didInsertSection: @escaping (_ observer: U, _ monitor: ListMonitor<T>, _ sectionInfo: NSFetchedResultsSectionInfo, _ toIndex: Int) -> Void, didDeleteSection: @escaping (_ observer: U, _ monitor: ListMonitor<T>, _ sectionInfo: NSFetchedResultsSectionInfo, _ fromIndex: Int) -> Void) { internal func registerObserver<U: AnyObject>(_ observer: U, didInsertSection: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>, _ sectionInfo: NSFetchedResultsSectionInfo, _ toIndex: Int) -> Void, didDeleteSection: @escaping (_ observer: U, _ monitor: ListMonitor<ObjectType>, _ sectionInfo: NSFetchedResultsSectionInfo, _ fromIndex: Int) -> Void) {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,
@@ -1017,7 +1017,7 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
} }
} }
private init(context: NSManagedObjectContext, transactionQueue: DispatchQueue, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void, createAsynchronously: ((ListMonitor<T>) -> Void)?) { private init(context: NSManagedObjectContext, transactionQueue: DispatchQueue, from: From<ObjectType>, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest<NSManagedObject>) -> Void, createAsynchronously: ((ListMonitor<ObjectType>) -> Void)?) {
self.isSectioned = (sectionBy != nil) self.isSectioned = (sectionBy != nil)
@@ -1129,23 +1129,23 @@ public final class ListMonitor<T: DynamicObject>: Hashable {
} }
// MARK: - ListMonitor where T: NSManagedObject // MARK: - ListMonitor where ListMonitor.ObjectType: NSManagedObject
@available(OSX 10.12, *) @available(OSX 10.12, *)
extension ListMonitor where T: NSManagedObject { extension ListMonitor where ListMonitor.ObjectType: NSManagedObject {
/** /**
Returns all objects in all sections Returns all objects in all sections
- returns: all objects in all sections - returns: all objects in all sections
*/ */
public func objectsInAllSections() -> [T] { public func objectsInAllSections() -> [ObjectType] {
CoreStore.assert( CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread, !self.isPendingRefetch || Thread.isMainThread,
"Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress." "Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress."
) )
return (self.fetchedResultsController.dynamicCast() as NSFetchedResultsController<T>).fetchedObjects ?? [] return (self.fetchedResultsController.dynamicCast() as NSFetchedResultsController<ObjectType>).fetchedObjects ?? []
} }
/** /**
@@ -1154,9 +1154,9 @@ extension ListMonitor where T: NSManagedObject {
- parameter section: the section index. Using an index outside the valid range will raise an exception. - parameter section: the section index. Using an index outside the valid range will raise an exception.
- returns: all objects in the specified section - returns: all objects in the specified section
*/ */
public func objectsInSection(_ section: Int) -> [T] { public func objectsInSection(_ section: Int) -> [ObjectType] {
return (self.sectionInfoAtIndex(section).objects as! [T]?) ?? [] return (self.sectionInfoAtIndex(section).objects as! [ObjectType]?) ?? []
} }
/** /**
@@ -1165,31 +1165,31 @@ extension ListMonitor where T: NSManagedObject {
- parameter section: the section index. Using an index outside the valid range will return `nil`. - parameter section: the section index. Using an index outside the valid range will return `nil`.
- returns: all objects in the specified section - returns: all objects in the specified section
*/ */
public func objectsInSection(safeSectionIndex section: Int) -> [T]? { public func objectsInSection(safeSectionIndex section: Int) -> [ObjectType]? {
return self.sectionInfoAtIndex(safeSectionIndex: section)?.objects as! [T]? return self.sectionInfoAtIndex(safeSectionIndex: section)?.objects as! [ObjectType]?
} }
} }
// MARK: - ListMonitor where T: CoreStoreObject // MARK: - ListMonitor where ListMonitor.ObjectType: CoreStoreObject
@available(OSX 10.12, *) @available(OSX 10.12, *)
extension ListMonitor where T: CoreStoreObject { extension ListMonitor where ListMonitor.ObjectType: CoreStoreObject {
/** /**
Returns all objects in all sections Returns all objects in all sections
- returns: all objects in all sections - returns: all objects in all sections
*/ */
public func objectsInAllSections() -> [T] { public func objectsInAllSections() -> [ObjectType] {
CoreStore.assert( CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread, !self.isPendingRefetch || Thread.isMainThread,
"Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress." "Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress."
) )
return (self.fetchedResultsController.fetchedObjects ?? []) return (self.fetchedResultsController.fetchedObjects ?? [])
.map(T.cs_fromRaw) .map(ObjectType.cs_fromRaw)
} }
/** /**
@@ -1198,10 +1198,10 @@ extension ListMonitor where T: CoreStoreObject {
- parameter section: the section index. Using an index outside the valid range will raise an exception. - parameter section: the section index. Using an index outside the valid range will raise an exception.
- returns: all objects in the specified section - returns: all objects in the specified section
*/ */
public func objectsInSection(_ section: Int) -> [T] { public func objectsInSection(_ section: Int) -> [ObjectType] {
return (self.sectionInfoAtIndex(section).objects ?? []) return (self.sectionInfoAtIndex(section).objects ?? [])
.map({ T.cs_fromRaw(object: $0 as! NSManagedObject) }) .map({ ObjectType.cs_fromRaw(object: $0 as! NSManagedObject) })
} }
/** /**
@@ -1210,10 +1210,10 @@ extension ListMonitor where T: CoreStoreObject {
- parameter section: the section index. Using an index outside the valid range will return `nil`. - parameter section: the section index. Using an index outside the valid range will return `nil`.
- returns: all objects in the specified section - returns: all objects in the specified section
*/ */
public func objectsInSection(safeSectionIndex section: Int) -> [T]? { public func objectsInSection(safeSectionIndex section: Int) -> [ObjectType]? {
return (self.sectionInfoAtIndex(safeSectionIndex: section)?.objects)? return (self.sectionInfoAtIndex(safeSectionIndex: section)?.objects)?
.map({ T.cs_fromRaw(object: $0 as! NSManagedObject) }) .map({ ObjectType.cs_fromRaw(object: $0 as! NSManagedObject) })
} }
} }

View File

@@ -40,22 +40,22 @@ import CoreData
Observers registered via `addObserver(_:)` are not retained. `ObjectMonitor` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles. Observers registered via `addObserver(_:)` are not retained. `ObjectMonitor` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles.
*/ */
@available(OSX 10.12, *) @available(OSX 10.12, *)
public final class ObjectMonitor<EntityType: DynamicObject>: Equatable { public final class ObjectMonitor<D: DynamicObject>: Equatable {
/** /**
The type for the object contained by the `ObjectMonitor` The type for the object contained by the `ObjectMonitor`
*/ */
public typealias ObjectType = EntityType public typealias ObjectType = D
/** /**
Returns the `NSManagedObject` instance being observed, or `nil` if the object was already deleted. Returns the `NSManagedObject` instance being observed, or `nil` if the object was already deleted.
*/ */
public var object: EntityType? { public var object: ObjectType? {
return self.fetchedResultsController return self.fetchedResultsController
.fetchedObjects? .fetchedObjects?
.first .first
.flatMap({ EntityType.cs_fromRaw(object: $0) }) .flatMap({ ObjectType.cs_fromRaw(object: $0) })
} }
/** /**
@@ -77,7 +77,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
- parameter observer: an `ObjectObserver` to send change notifications to - parameter observer: an `ObjectObserver` to send change notifications to
*/ */
public func addObserver<U: ObjectObserver>(_ observer: U) where U.ObjectEntityType == EntityType { public func addObserver<U: ObjectObserver>(_ observer: U) where U.ObjectEntityType == ObjectType {
self.unregisterObserver(observer) self.unregisterObserver(observer)
self.registerObserver( self.registerObserver(
@@ -104,7 +104,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
- parameter observer: an `ObjectObserver` to unregister notifications to - parameter observer: an `ObjectObserver` to unregister notifications to
*/ */
public func removeObserver<U: ObjectObserver>(_ observer: U) where U.ObjectEntityType == EntityType { public func removeObserver<U: ObjectObserver>(_ observer: U) where U.ObjectEntityType == ObjectType {
self.unregisterObserver(observer) self.unregisterObserver(observer)
} }
@@ -112,7 +112,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
// MARK: Equatable // MARK: Equatable
public static func == <T: DynamicObject>(lhs: ObjectMonitor<T>, rhs: ObjectMonitor<T>) -> Bool { public static func == (lhs: ObjectMonitor<ObjectType>, rhs: ObjectMonitor<ObjectType>) -> Bool {
return lhs === rhs return lhs === rhs
} }
@@ -122,7 +122,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
return lhs.fetchedResultsController === rhs.fetchedResultsController return lhs.fetchedResultsController === rhs.fetchedResultsController
} }
public static func ~= <T: DynamicObject>(lhs: ObjectMonitor<T>, rhs: ObjectMonitor<T>) -> Bool { public static func ~= (lhs: ObjectMonitor<ObjectType>, rhs: ObjectMonitor<ObjectType>) -> Bool {
return lhs === rhs return lhs === rhs
} }
@@ -143,17 +143,17 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
// MARK: Internal // MARK: Internal
internal convenience init(dataStack: DataStack, object: EntityType) { internal convenience init(dataStack: DataStack, object: ObjectType) {
self.init(context: dataStack.mainContext, object: object) self.init(context: dataStack.mainContext, object: object)
} }
internal convenience init(unsafeTransaction: UnsafeDataTransaction, object: EntityType) { internal convenience init(unsafeTransaction: UnsafeDataTransaction, object: ObjectType) {
self.init(context: unsafeTransaction.context, object: object) self.init(context: unsafeTransaction.context, object: object)
} }
internal func registerObserver<U: AnyObject>(_ observer: U, willChangeObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<EntityType>, _ object: EntityType) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<EntityType>, _ object: EntityType) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<EntityType>, _ object: EntityType, _ changedPersistentKeys: Set<String>) -> Void) { internal func registerObserver<U: AnyObject>(_ observer: U, willChangeObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<ObjectType>, _ object: ObjectType) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<ObjectType>, _ object: ObjectType) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ObjectMonitor<ObjectType>, _ object: ObjectType, _ changedPersistentKeys: Set<String>) -> Void) {
CoreStore.assert( CoreStore.assert(
Thread.isMainThread, Thread.isMainThread,
@@ -243,7 +243,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
private var didDeleteObjectKey: Void? private var didDeleteObjectKey: Void?
private var didUpdateObjectKey: Void? private var didUpdateObjectKey: Void?
private init(context: NSManagedObjectContext, object: EntityType) { private init(context: NSManagedObjectContext, object: ObjectType) {
let rawObject = object.cs_toRaw() let rawObject = object.cs_toRaw()
let fetchRequest = CoreStoreFetchRequest() let fetchRequest = CoreStoreFetchRequest()
@@ -258,7 +258,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
let fetchedResultsController = CoreStoreFetchedResultsController( let fetchedResultsController = CoreStoreFetchedResultsController(
context: context, context: context,
fetchRequest: fetchRequest.dynamicCast(), fetchRequest: fetchRequest.dynamicCast(),
from: nil as From<EntityType>?, from: nil as From<ObjectType>?,
applyFetchClauses: Where("SELF", isEqualTo: objectID).applyToFetchRequest applyFetchClauses: Where("SELF", isEqualTo: objectID).applyToFetchRequest
) )
@@ -274,7 +274,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
self.lastCommittedAttributes = (self.object?.cs_toRaw().committedValues(forKeys: nil) as? [String: NSObject]) ?? [:] self.lastCommittedAttributes = (self.object?.cs_toRaw().committedValues(forKeys: nil) as? [String: NSObject]) ?? [:]
} }
private func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor<EntityType>) -> Void) { private func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor<ObjectType>) -> Void) {
cs_setAssociatedRetainedObject( cs_setAssociatedRetainedObject(
NotificationObserver( NotificationObserver(
@@ -294,7 +294,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
) )
} }
private func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor<EntityType>, _ object: EntityType) -> Void) { private func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor<ObjectType>, _ object: ObjectType) -> Void) {
cs_setAssociatedRetainedObject( cs_setAssociatedRetainedObject(
NotificationObserver( NotificationObserver(
@@ -308,7 +308,7 @@ public final class ObjectMonitor<EntityType: DynamicObject>: Equatable {
return return
} }
callback(self, EntityType.cs_fromRaw(object: object)) callback(self, ObjectType.cs_fromRaw(object: object))
} }
), ),
forKey: notificationKey, forKey: notificationKey,

View File

@@ -38,7 +38,7 @@ public extension UnsafeDataTransaction {
- parameter object: the `NSManagedObject` to observe changes from - parameter object: the `NSManagedObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object` - returns: a `ObjectMonitor` that monitors changes to `object`
*/ */
public func monitorObject<T: NSManagedObject>(_ object: T) -> ObjectMonitor<T> { public func monitorObject<T: DynamicObject>(_ object: T) -> ObjectMonitor<T> {
return ObjectMonitor( return ObjectMonitor(
unsafeTransaction: self, unsafeTransaction: self,
@@ -53,7 +53,7 @@ public extension UnsafeDataTransaction {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> { public func monitorList<T: DynamicObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
return self.monitorList(from, fetchClauses) return self.monitorList(from, fetchClauses)
} }
@@ -65,7 +65,7 @@ public extension UnsafeDataTransaction {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public func monitorList<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> { public func monitorList<T: DynamicObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
CoreStore.assert( CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0, fetchClauses.filter { $0 is OrderBy }.count > 0,
@@ -90,7 +90,7 @@ public extension UnsafeDataTransaction {
- parameter from: a `From` clause indicating the entity type - parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) { public func monitorList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: FetchClause...) {
self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses) self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses)
} }
@@ -102,7 +102,7 @@ public extension UnsafeDataTransaction {
- parameter from: a `From` clause indicating the entity type - parameter from: a `From` clause indicating the entity type
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public func monitorList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) { public func monitorList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ fetchClauses: [FetchClause]) {
CoreStore.assert( CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0, fetchClauses.filter { $0 is OrderBy }.count > 0,
@@ -129,7 +129,7 @@ public extension UnsafeDataTransaction {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> { public func monitorSectionedList<T: DynamicObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
return self.monitorSectionedList(from, sectionBy, fetchClauses) return self.monitorSectionedList(from, sectionBy, fetchClauses)
} }
@@ -142,7 +142,7 @@ public extension UnsafeDataTransaction {
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
- returns: a `ListMonitor` instance that monitors changes to the list - returns: a `ListMonitor` instance that monitors changes to the list
*/ */
public func monitorSectionedList<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> { public func monitorSectionedList<T: DynamicObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
CoreStore.assert( CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0, fetchClauses.filter { $0 is OrderBy }.count > 0,
@@ -168,7 +168,7 @@ public extension UnsafeDataTransaction {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections. - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { public func monitorSectionedList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) {
self.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses) self.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses)
} }
@@ -181,7 +181,7 @@ public extension UnsafeDataTransaction {
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections. - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
*/ */
public func monitorSectionedList<T: NSManagedObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { public func monitorSectionedList<T: DynamicObject>(createAsynchronously: @escaping (ListMonitor<T>) -> Void, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) {
CoreStore.assert( CoreStore.assert(
fetchClauses.filter { $0 is OrderBy }.count > 0, fetchClauses.filter { $0 is OrderBy }.count > 0,