diff --git a/Sources/ObjectiveC/CSListMonitor.swift b/Sources/ObjectiveC/CSListMonitor.swift index 071210f..6e109d6 100644 --- a/Sources/ObjectiveC/CSListMonitor.swift +++ b/Sources/ObjectiveC/CSListMonitor.swift @@ -546,7 +546,7 @@ public final class CSListMonitor: NSObject { // MARK: - ListMonitor @available(OSX 10.12, *) -extension ListMonitor where T: NSManagedObject { +extension ListMonitor where ListMonitor.ObjectType: NSManagedObject { // MARK: CoreStoreSwiftType diff --git a/Sources/ObjectiveC/CSObjectMonitor.swift b/Sources/ObjectiveC/CSObjectMonitor.swift index aca5a49..52e58dd 100644 --- a/Sources/ObjectiveC/CSObjectMonitor.swift +++ b/Sources/ObjectiveC/CSObjectMonitor.swift @@ -138,7 +138,7 @@ public final class CSObjectMonitor: NSObject { // MARK: - ObjectMonitor @available(OSX 10.12, *) -extension ObjectMonitor where EntityType: NSManagedObject { +extension ObjectMonitor where ObjectMonitor.ObjectType: NSManagedObject { // MARK: CoreStoreSwiftType diff --git a/Sources/Observing/CoreStore+Observing.swift b/Sources/Observing/CoreStore+Observing.swift index 86c5a14..0bc78be 100644 --- a/Sources/Observing/CoreStore+Observing.swift +++ b/Sources/Observing/CoreStore+Observing.swift @@ -38,7 +38,7 @@ public extension CoreStore { - parameter object: the `NSManagedObject` to observe changes from - returns: a `ObjectMonitor` that monitors changes to `object` */ - public static func monitorObject(_ object: T) -> ObjectMonitor { + public static func monitorObject(_ object: T) -> ObjectMonitor { 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public static func monitorList(_ from: From, _ fetchClauses: FetchClause...) -> ListMonitor { + public static func monitorList(_ from: From, _ fetchClauses: FetchClause...) -> ListMonitor { 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public static func monitorList(_ from: From, _ fetchClauses: [FetchClause]) -> ListMonitor { + public static func monitorList(_ from: From, _ fetchClauses: [FetchClause]) -> ListMonitor { return self.defaultStack.monitorList(from, fetchClauses) } @@ -74,7 +74,7 @@ public extension CoreStore { - 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. */ - public static func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: FetchClause...) { + public static func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: FetchClause...) { self.defaultStack.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses) } @@ -86,7 +86,7 @@ public extension CoreStore { - 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. */ - public static func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: [FetchClause]) { + public static func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: [FetchClause]) { 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public static func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor { + public static func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor { 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public static func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor { + public static func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor { 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 fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. */ - public static func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { + public static func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { 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 fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. */ - public static func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { + public static func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { self.defaultStack.monitorSectionedList(createAsynchronously: createAsynchronously, from, sectionBy, fetchClauses) } diff --git a/Sources/Observing/DataStack+Observing.swift b/Sources/Observing/DataStack+Observing.swift index c408637..1070b42 100644 --- a/Sources/Observing/DataStack+Observing.swift +++ b/Sources/Observing/DataStack+Observing.swift @@ -38,7 +38,7 @@ public extension DataStack { - parameter object: the `NSManagedObject` to observe changes from - returns: a `ObjectMonitor` that monitors changes to `object` */ - public func monitorObject(_ object: T) -> ObjectMonitor { + public func monitorObject(_ object: T) -> ObjectMonitor { CoreStore.assert( 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public func monitorList(_ from: From, _ fetchClauses: FetchClause...) -> ListMonitor { + public func monitorList(_ from: From, _ fetchClauses: FetchClause...) -> ListMonitor { 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public func monitorList(_ from: From, _ fetchClauses: [FetchClause]) -> ListMonitor { + public func monitorList(_ from: From, _ fetchClauses: [FetchClause]) -> ListMonitor { CoreStore.assert( Thread.isMainThread, @@ -95,7 +95,7 @@ public extension DataStack { - 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. */ - public func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: FetchClause...) { + public func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: FetchClause...) { self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses) } @@ -107,7 +107,7 @@ public extension DataStack { - 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. */ - public func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: [FetchClause]) { + public func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: [FetchClause]) { CoreStore.assert( 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor { + public func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor { 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor { + public func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor { CoreStore.assert( 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 fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. */ - public func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { + public func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { 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 fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. */ - public func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { + public func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { CoreStore.assert( Thread.isMainThread, diff --git a/Sources/Observing/ListMonitor.swift b/Sources/Observing/ListMonitor.swift index 78cb88b..54e9be1 100644 --- a/Sources/Observing/ListMonitor.swift +++ b/Sources/Observing/ListMonitor.swift @@ -67,14 +67,14 @@ import CoreData In the example above, both `person1` and `person2` will contain the object at section=2, index=3. */ @available(OSX 10.12, *) -public final class ListMonitor: Hashable { +public final class ListMonitor: Hashable { // MARK: Public (Accessors) /** 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(_:)`. @@ -82,7 +82,7 @@ public final class ListMonitor: Hashable { - 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 */ - public subscript(index: Int) -> T { + public subscript(index: Int) -> ObjectType { CoreStore.assert( !self.isPendingRefetch || Thread.isMainThread, @@ -90,7 +90,7 @@ public final class ListMonitor: Hashable { ) 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] } @@ -101,14 +101,14 @@ public final class ListMonitor: Hashable { - 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 */ - public subscript(safeIndex index: Int) -> T? { + public subscript(safeIndex index: Int) -> ObjectType? { if self.isSectioned { let fetchedObjects = self.fetchedResultsController.fetchedObjects! if index < fetchedObjects.count && index >= 0 { - return T.cs_fromRaw(object: fetchedObjects[index]) + return ObjectType.cs_fromRaw(object: fetchedObjects[index]) } return nil } @@ -122,7 +122,7 @@ public final class ListMonitor: Hashable { - 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 */ - public subscript(sectionIndex: Int, itemIndex: Int) -> T { + public subscript(sectionIndex: Int, itemIndex: Int) -> ObjectType { return self[NSIndexPath(indexes: [sectionIndex, itemIndex], length: 2) as IndexPath] } @@ -134,7 +134,7 @@ public final class ListMonitor: Hashable { - 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 */ - 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 { @@ -144,7 +144,7 @@ public final class ListMonitor: Hashable { 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: Hashable { - 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 */ - public subscript(indexPath: IndexPath) -> T { + public subscript(indexPath: IndexPath) -> ObjectType { CoreStore.assert( !self.isPendingRefetch || Thread.isMainThread, "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: Hashable { - 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 */ - public subscript(safeIndexPath indexPath: IndexPath) -> T? { + public subscript(safeIndexPath indexPath: IndexPath) -> ObjectType? { return self[ safeSectionIndex: indexPath[0], @@ -345,7 +345,7 @@ public final class ListMonitor: Hashable { - 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. */ - public func indexOf(_ object: T) -> Int? { + public func indexOf(_ object: ObjectType) -> Int? { CoreStore.assert( !self.isPendingRefetch || Thread.isMainThread, @@ -364,7 +364,7 @@ public final class ListMonitor: Hashable { - 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. */ - public func indexPathOf(_ object: T) -> IndexPath? { + public func indexPathOf(_ object: ObjectType) -> IndexPath? { CoreStore.assert( !self.isPendingRefetch || Thread.isMainThread, @@ -387,7 +387,7 @@ public final class ListMonitor: Hashable { - parameter observer: a `ListObserver` to send change notifications to */ - public func addObserver(_ observer: U) where U.ListEntityType == T { + public func addObserver(_ observer: U) where U.ListEntityType == ObjectType { self.unregisterObserver(observer) self.registerObserver( @@ -422,7 +422,7 @@ public final class ListMonitor: Hashable { - parameter observer: a `ListObjectObserver` to send change notifications to */ - public func addObserver(_ observer: U) where U.ListEntityType == T { + public func addObserver(_ observer: U) where U.ListEntityType == ObjectType { self.unregisterObserver(observer) self.registerObserver( @@ -476,7 +476,7 @@ public final class ListMonitor: Hashable { - parameter observer: a `ListSectionObserver` to send change notifications to */ - public func addObserver(_ observer: U) where U.ListEntityType == T { + public func addObserver(_ observer: U) where U.ListEntityType == ObjectType { self.unregisterObserver(observer) self.registerObserver( @@ -537,7 +537,7 @@ public final class ListMonitor: Hashable { - parameter observer: a `ListObserver` to unregister notifications to */ - public func removeObserver(_ observer: U) where U.ListEntityType == T { + public func removeObserver(_ observer: U) where U.ListEntityType == ObjectType { self.unregisterObserver(observer) } @@ -580,7 +580,7 @@ public final class ListMonitor: Hashable { // MARK: Equatable - public static func == (lhs: ListMonitor, rhs: ListMonitor) -> Bool { + public static func == (lhs: ListMonitor, rhs: ListMonitor) -> Bool { return lhs.fetchedResultsController === rhs.fetchedResultsController } @@ -590,7 +590,7 @@ public final class ListMonitor: Hashable { return lhs.fetchedResultsController === rhs.fetchedResultsController } - public static func ~= (lhs: ListMonitor, rhs: ListMonitor) -> Bool { + public static func ~= (lhs: ListMonitor, rhs: ListMonitor) -> Bool { return lhs.fetchedResultsController === rhs.fetchedResultsController } @@ -611,7 +611,7 @@ public final class ListMonitor: Hashable { // MARK: Internal - internal convenience init(dataStack: DataStack, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void) { + internal convenience init(dataStack: DataStack, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void) { self.init( context: dataStack.mainContext, @@ -623,7 +623,7 @@ public final class ListMonitor: Hashable { ) } - internal convenience init(dataStack: DataStack, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void, createAsynchronously: @escaping (ListMonitor) -> Void) { + internal convenience init(dataStack: DataStack, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void, createAsynchronously: @escaping (ListMonitor) -> Void) { self.init( context: dataStack.mainContext, @@ -635,7 +635,7 @@ public final class ListMonitor: Hashable { ) } - internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void) { + internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void) { self.init( context: unsafeTransaction.context, @@ -647,7 +647,7 @@ public final class ListMonitor: Hashable { ) } - internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void, createAsynchronously: @escaping (ListMonitor) -> Void) { + internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void, createAsynchronously: @escaping (ListMonitor) -> Void) { self.init( context: unsafeTransaction.context, @@ -659,7 +659,7 @@ public final class ListMonitor: Hashable { ) } - internal func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor) -> Void) { + internal func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor) -> Void) { cs_setAssociatedRetainedObject( NotificationObserver( @@ -679,7 +679,7 @@ public final class ListMonitor: Hashable { ) } - internal func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor, _ object: T, _ indexPath: IndexPath?, _ newIndexPath: IndexPath?) -> Void) { + internal func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor, _ object: ObjectType, _ indexPath: IndexPath?, _ newIndexPath: IndexPath?) -> Void) { cs_setAssociatedRetainedObject( NotificationObserver( @@ -689,7 +689,7 @@ public final class ListMonitor: Hashable { guard let `self` = self, 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 } @@ -706,7 +706,7 @@ public final class ListMonitor: Hashable { ) } - internal func registerSectionNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor, _ sectionInfo: NSFetchedResultsSectionInfo, _ sectionIndex: Int) -> Void) { + internal func registerSectionNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ListMonitor, _ sectionInfo: NSFetchedResultsSectionInfo, _ sectionIndex: Int) -> Void) { cs_setAssociatedRetainedObject( NotificationObserver( @@ -729,7 +729,7 @@ public final class ListMonitor: Hashable { ) } - internal func registerObserver(_ observer: U, willChange: @escaping (_ observer: U, _ monitor: ListMonitor) -> Void, didChange: @escaping (_ observer: U, _ monitor: ListMonitor) -> Void, willRefetch: @escaping (_ observer: U, _ monitor: ListMonitor) -> Void, didRefetch: @escaping (_ observer: U, _ monitor: ListMonitor) -> Void) { + internal func registerObserver(_ observer: U, willChange: @escaping (_ observer: U, _ monitor: ListMonitor) -> Void, didChange: @escaping (_ observer: U, _ monitor: ListMonitor) -> Void, willRefetch: @escaping (_ observer: U, _ monitor: ListMonitor) -> Void, didRefetch: @escaping (_ observer: U, _ monitor: ListMonitor) -> Void) { CoreStore.assert( Thread.isMainThread, @@ -789,7 +789,7 @@ public final class ListMonitor: Hashable { ) } - internal func registerObserver(_ observer: U, didInsertObject: @escaping (_ observer: U, _ monitor: ListMonitor, _ object: T, _ toIndexPath: IndexPath) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ListMonitor, _ object: T, _ fromIndexPath: IndexPath) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ListMonitor, _ object: T, _ atIndexPath: IndexPath) -> Void, didMoveObject: @escaping (_ observer: U, _ monitor: ListMonitor, _ object: T, _ fromIndexPath: IndexPath, _ toIndexPath: IndexPath) -> Void) { + internal func registerObserver(_ observer: U, didInsertObject: @escaping (_ observer: U, _ monitor: ListMonitor, _ object: ObjectType, _ toIndexPath: IndexPath) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ListMonitor, _ object: ObjectType, _ fromIndexPath: IndexPath) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ListMonitor, _ object: ObjectType, _ atIndexPath: IndexPath) -> Void, didMoveObject: @escaping (_ observer: U, _ monitor: ListMonitor, _ object: ObjectType, _ fromIndexPath: IndexPath, _ toIndexPath: IndexPath) -> Void) { CoreStore.assert( Thread.isMainThread, @@ -850,7 +850,7 @@ public final class ListMonitor: Hashable { ) } - internal func registerObserver(_ observer: U, didInsertSection: @escaping (_ observer: U, _ monitor: ListMonitor, _ sectionInfo: NSFetchedResultsSectionInfo, _ toIndex: Int) -> Void, didDeleteSection: @escaping (_ observer: U, _ monitor: ListMonitor, _ sectionInfo: NSFetchedResultsSectionInfo, _ fromIndex: Int) -> Void) { + internal func registerObserver(_ observer: U, didInsertSection: @escaping (_ observer: U, _ monitor: ListMonitor, _ sectionInfo: NSFetchedResultsSectionInfo, _ toIndex: Int) -> Void, didDeleteSection: @escaping (_ observer: U, _ monitor: ListMonitor, _ sectionInfo: NSFetchedResultsSectionInfo, _ fromIndex: Int) -> Void) { CoreStore.assert( Thread.isMainThread, @@ -1017,7 +1017,7 @@ public final class ListMonitor: Hashable { } } - private init(context: NSManagedObjectContext, transactionQueue: DispatchQueue, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void, createAsynchronously: ((ListMonitor) -> Void)?) { + private init(context: NSManagedObjectContext, transactionQueue: DispatchQueue, from: From, sectionBy: SectionBy?, applyFetchClauses: @escaping (_ fetchRequest: NSFetchRequest) -> Void, createAsynchronously: ((ListMonitor) -> Void)?) { self.isSectioned = (sectionBy != nil) @@ -1129,23 +1129,23 @@ public final class ListMonitor: Hashable { } -// MARK: - ListMonitor where T: NSManagedObject +// MARK: - ListMonitor where ListMonitor.ObjectType: NSManagedObject @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 */ - public func objectsInAllSections() -> [T] { + public func objectsInAllSections() -> [ObjectType] { CoreStore.assert( !self.isPendingRefetch || Thread.isMainThread, "Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress." ) - return (self.fetchedResultsController.dynamicCast() as NSFetchedResultsController).fetchedObjects ?? [] + return (self.fetchedResultsController.dynamicCast() as NSFetchedResultsController).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. - 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`. - 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, *) -extension ListMonitor where T: CoreStoreObject { +extension ListMonitor where ListMonitor.ObjectType: CoreStoreObject { /** Returns all objects in all sections - returns: all objects in all sections */ - public func objectsInAllSections() -> [T] { + public func objectsInAllSections() -> [ObjectType] { CoreStore.assert( !self.isPendingRefetch || Thread.isMainThread, "Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress." ) 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. - returns: all objects in the specified section */ - public func objectsInSection(_ section: Int) -> [T] { + public func objectsInSection(_ section: Int) -> [ObjectType] { 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`. - 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)? - .map({ T.cs_fromRaw(object: $0 as! NSManagedObject) }) + .map({ ObjectType.cs_fromRaw(object: $0 as! NSManagedObject) }) } } diff --git a/Sources/Observing/ObjectMonitor.swift b/Sources/Observing/ObjectMonitor.swift index 51f6f87..3550a57 100644 --- a/Sources/Observing/ObjectMonitor.swift +++ b/Sources/Observing/ObjectMonitor.swift @@ -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. */ @available(OSX 10.12, *) -public final class ObjectMonitor: Equatable { +public final class ObjectMonitor: Equatable { /** 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. */ - public var object: EntityType? { + public var object: ObjectType? { return self.fetchedResultsController .fetchedObjects? .first - .flatMap({ EntityType.cs_fromRaw(object: $0) }) + .flatMap({ ObjectType.cs_fromRaw(object: $0) }) } /** @@ -77,7 +77,7 @@ public final class ObjectMonitor: Equatable { - parameter observer: an `ObjectObserver` to send change notifications to */ - public func addObserver(_ observer: U) where U.ObjectEntityType == EntityType { + public func addObserver(_ observer: U) where U.ObjectEntityType == ObjectType { self.unregisterObserver(observer) self.registerObserver( @@ -104,7 +104,7 @@ public final class ObjectMonitor: Equatable { - parameter observer: an `ObjectObserver` to unregister notifications to */ - public func removeObserver(_ observer: U) where U.ObjectEntityType == EntityType { + public func removeObserver(_ observer: U) where U.ObjectEntityType == ObjectType { self.unregisterObserver(observer) } @@ -112,7 +112,7 @@ public final class ObjectMonitor: Equatable { // MARK: Equatable - public static func == (lhs: ObjectMonitor, rhs: ObjectMonitor) -> Bool { + public static func == (lhs: ObjectMonitor, rhs: ObjectMonitor) -> Bool { return lhs === rhs } @@ -122,7 +122,7 @@ public final class ObjectMonitor: Equatable { return lhs.fetchedResultsController === rhs.fetchedResultsController } - public static func ~= (lhs: ObjectMonitor, rhs: ObjectMonitor) -> Bool { + public static func ~= (lhs: ObjectMonitor, rhs: ObjectMonitor) -> Bool { return lhs === rhs } @@ -143,17 +143,17 @@ public final class ObjectMonitor: Equatable { // MARK: Internal - internal convenience init(dataStack: DataStack, object: EntityType) { + internal convenience init(dataStack: DataStack, object: ObjectType) { 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) } - internal func registerObserver(_ observer: U, willChangeObject: @escaping (_ observer: U, _ monitor: ObjectMonitor, _ object: EntityType) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ObjectMonitor, _ object: EntityType) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ObjectMonitor, _ object: EntityType, _ changedPersistentKeys: Set) -> Void) { + internal func registerObserver(_ observer: U, willChangeObject: @escaping (_ observer: U, _ monitor: ObjectMonitor, _ object: ObjectType) -> Void, didDeleteObject: @escaping (_ observer: U, _ monitor: ObjectMonitor, _ object: ObjectType) -> Void, didUpdateObject: @escaping (_ observer: U, _ monitor: ObjectMonitor, _ object: ObjectType, _ changedPersistentKeys: Set) -> Void) { CoreStore.assert( Thread.isMainThread, @@ -243,7 +243,7 @@ public final class ObjectMonitor: Equatable { private var didDeleteObjectKey: Void? private var didUpdateObjectKey: Void? - private init(context: NSManagedObjectContext, object: EntityType) { + private init(context: NSManagedObjectContext, object: ObjectType) { let rawObject = object.cs_toRaw() let fetchRequest = CoreStoreFetchRequest() @@ -258,7 +258,7 @@ public final class ObjectMonitor: Equatable { let fetchedResultsController = CoreStoreFetchedResultsController( context: context, fetchRequest: fetchRequest.dynamicCast(), - from: nil as From?, + from: nil as From?, applyFetchClauses: Where("SELF", isEqualTo: objectID).applyToFetchRequest ) @@ -274,7 +274,7 @@ public final class ObjectMonitor: Equatable { 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) -> Void) { + private func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor) -> Void) { cs_setAssociatedRetainedObject( NotificationObserver( @@ -294,7 +294,7 @@ public final class ObjectMonitor: Equatable { ) } - private func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor, _ object: EntityType) -> Void) { + private func registerObjectNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor, _ object: ObjectType) -> Void) { cs_setAssociatedRetainedObject( NotificationObserver( @@ -308,7 +308,7 @@ public final class ObjectMonitor: Equatable { return } - callback(self, EntityType.cs_fromRaw(object: object)) + callback(self, ObjectType.cs_fromRaw(object: object)) } ), forKey: notificationKey, diff --git a/Sources/Observing/UnsafeDataTransaction+Observing.swift b/Sources/Observing/UnsafeDataTransaction+Observing.swift index 7e7ac14..e8ac0b7 100644 --- a/Sources/Observing/UnsafeDataTransaction+Observing.swift +++ b/Sources/Observing/UnsafeDataTransaction+Observing.swift @@ -38,7 +38,7 @@ public extension UnsafeDataTransaction { - parameter object: the `NSManagedObject` to observe changes from - returns: a `ObjectMonitor` that monitors changes to `object` */ - public func monitorObject(_ object: T) -> ObjectMonitor { + public func monitorObject(_ object: T) -> ObjectMonitor { return ObjectMonitor( 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public func monitorList(_ from: From, _ fetchClauses: FetchClause...) -> ListMonitor { + public func monitorList(_ from: From, _ fetchClauses: FetchClause...) -> ListMonitor { 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public func monitorList(_ from: From, _ fetchClauses: [FetchClause]) -> ListMonitor { + public func monitorList(_ from: From, _ fetchClauses: [FetchClause]) -> ListMonitor { CoreStore.assert( fetchClauses.filter { $0 is OrderBy }.count > 0, @@ -90,7 +90,7 @@ public extension UnsafeDataTransaction { - 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. */ - public func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: FetchClause...) { + public func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: FetchClause...) { self.monitorList(createAsynchronously: createAsynchronously, from, fetchClauses) } @@ -102,7 +102,7 @@ public extension UnsafeDataTransaction { - 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. */ - public func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: [FetchClause]) { + public func monitorList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ fetchClauses: [FetchClause]) { CoreStore.assert( 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor { + public func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> ListMonitor { 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. - returns: a `ListMonitor` instance that monitors changes to the list */ - public func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor { + public func monitorSectionedList(_ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> ListMonitor { CoreStore.assert( 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 fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. */ - public func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { + public func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) { 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 fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. */ - public func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { + public func monitorSectionedList(createAsynchronously: @escaping (ListMonitor) -> Void, _ from: From, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) { CoreStore.assert( fetchClauses.filter { $0 is OrderBy }.count > 0,