From dcf562fb26264b0bc10796a281faeb41352eb9b7 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Thu, 9 Jun 2016 17:57:15 +0900 Subject: [PATCH 1/4] ignore negative indexes when using ListMonitor's safeIndex API --- Sources/Observing/ListMonitor.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/Observing/ListMonitor.swift b/Sources/Observing/ListMonitor.swift index a3d38da..e76a18e 100644 --- a/Sources/Observing/ListMonitor.swift +++ b/Sources/Observing/ListMonitor.swift @@ -121,13 +121,15 @@ public final class ListMonitor: Hashable { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress." ) - + guard sectionIndex >= 0 && itemIndex >= 0 else { + + return nil + } guard let sections = self.fetchedResultsController.sections where sectionIndex < sections.count else { return nil } - let section = sections[sectionIndex] guard itemIndex < section.numberOfObjects else { From c0ed2f23ce3cf36980ab8aed8f14806608a8b674 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Thu, 9 Jun 2016 18:06:08 +0900 Subject: [PATCH 2/4] ignore negative indexes when using ListMonitor's safeIndex APIs --- CoreStore/Observing/ListMonitor.swift | 50 ++++++--------------------- 1 file changed, 10 insertions(+), 40 deletions(-) diff --git a/CoreStore/Observing/ListMonitor.swift b/CoreStore/Observing/ListMonitor.swift index 2c020fc..1f9b584 100644 --- a/CoreStore/Observing/ListMonitor.swift +++ b/CoreStore/Observing/ListMonitor.swift @@ -117,19 +117,11 @@ public final class ListMonitor { */ public subscript(safeSectionIndex sectionIndex: Int, safeItemIndex itemIndex: Int) -> T? { - CoreStore.assert( - !self.isPendingRefetch || NSThread.isMainThread(), - "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." - ) - - guard let sections = self.fetchedResultsController.sections - where sectionIndex < sections.count else { - - return nil + guard let section = self.sectionInfoAtIndex(safeSectionIndex: sectionIndex) else { + + return nil } - - let section = sections[sectionIndex] - guard itemIndex < section.numberOfObjects else { + guard itemIndex >= 0 && itemIndex < section.numberOfObjects else { return nil } @@ -148,7 +140,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return self.fetchedResultsController.objectAtIndexPath(indexPath) as! T } @@ -201,7 +192,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return (self.fetchedResultsController.fetchedObjects as? [T]) ?? [] } @@ -214,12 +204,7 @@ public final class ListMonitor { @warn_unused_result public func objectsInSection(section: Int) -> [T] { - CoreStore.assert( - !self.isPendingRefetch || NSThread.isMainThread(), - "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." - ) - - return (self.fetchedResultsController.sections?[section].objects as? [T]) ?? [] + return (self.sectionInfoAtIndex(section).objects as? [T]) ?? [] } /** @@ -231,12 +216,7 @@ public final class ListMonitor { @warn_unused_result public func objectsInSection(safeSectionIndex section: Int) -> [T]? { - CoreStore.assert( - !self.isPendingRefetch || NSThread.isMainThread(), - "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." - ) - - return (self.fetchedResultsController.sections?[section].objects as? [T]) ?? [] + return (self.sectionInfoAtIndex(safeSectionIndex: section)?.objects as? [T]) ?? [] } /** @@ -251,7 +231,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return self.fetchedResultsController.sections?.count ?? 0 } @@ -267,7 +246,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return self.fetchedResultsController.fetchedObjects?.count ?? 0 } @@ -308,7 +286,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return self.fetchedResultsController.sections![section] } @@ -325,13 +302,15 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - + guard section >= 0 else { + + return nil + } guard let sections = self.fetchedResultsController.sections where section < sections.count else { return nil } - return sections[section] } @@ -347,7 +326,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return self.fetchedResultsController.sections ?? [] } @@ -365,7 +343,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return self.fetchedResultsController.sectionForSectionIndexTitle(title, atIndex: index) } @@ -381,7 +358,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return self.fetchedResultsController.sectionIndexTitles } @@ -398,7 +374,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return (self.fetchedResultsController.fetchedObjects as? [T] ?? []).indexOf(object) } @@ -415,7 +390,6 @@ public final class ListMonitor { !self.isPendingRefetch || NSThread.isMainThread(), "Attempted to access a \(typeName(self)) outside the main thread while a refetch is in progress." ) - return self.fetchedResultsController.indexPathForObject(object) } @@ -439,7 +413,6 @@ public final class ListMonitor { NSThread.isMainThread(), "Attempted to add an observer of type \(typeName(observer)) outside the main thread." ) - self.removeObserver(observer) self.registerChangeNotification( @@ -513,7 +486,6 @@ public final class ListMonitor { NSThread.isMainThread(), "Attempted to add an observer of type \(typeName(observer)) outside the main thread." ) - self.removeObserver(observer) self.registerChangeNotification( @@ -657,7 +629,6 @@ public final class ListMonitor { NSThread.isMainThread(), "Attempted to add an observer of type \(typeName(observer)) outside the main thread." ) - self.removeObserver(observer) self.registerChangeNotification( @@ -832,7 +803,6 @@ public final class ListMonitor { NSThread.isMainThread(), "Attempted to remove an observer of type \(typeName(observer)) outside the main thread." ) - let nilValue: AnyObject? = nil setAssociatedRetainedObject(nilValue, forKey: &self.willChangeListKey, inObject: observer) setAssociatedRetainedObject(nilValue, forKey: &self.didChangeListKey, inObject: observer) From d25f7510893e8343f7bc1e340a43496fc45c7e81 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Thu, 9 Jun 2016 18:20:49 +0900 Subject: [PATCH 3/4] oops --- CoreStore/Observing/ListMonitor.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CoreStore/Observing/ListMonitor.swift b/CoreStore/Observing/ListMonitor.swift index 1f9b584..229e39a 100644 --- a/CoreStore/Observing/ListMonitor.swift +++ b/CoreStore/Observing/ListMonitor.swift @@ -125,7 +125,7 @@ public final class ListMonitor { return nil } - return sections[sectionIndex].objects?[itemIndex] as? T + return section.objects?[itemIndex] as? T } /** From 141e977f9c7160b814b0e4b4630d58b320be19b7 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Thu, 9 Jun 2016 18:56:32 +0900 Subject: [PATCH 4/4] remove sort assertion when using ObjectMonitor --- ...FetchedResultsController+Convenience.swift | 5 +++++ .../CoreStoreFetchedResultsController.swift | 4 ---- ...SFetchedResultsController+ObjectiveC.swift | 5 +++++ Sources/Observing/DataStack+Observing.swift | 20 +++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Sources/Convenience/NSFetchedResultsController+Convenience.swift b/Sources/Convenience/NSFetchedResultsController+Convenience.swift index 41882df..a5da48d 100644 --- a/Sources/Convenience/NSFetchedResultsController+Convenience.swift +++ b/Sources/Convenience/NSFetchedResultsController+Convenience.swift @@ -208,6 +208,11 @@ public extension NSFetchedResultsController { applyFetchClauses: { fetchRequest in fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) } + + CoreStore.assert( + fetchRequest.sortDescriptors?.isEmpty == false, + "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor." + ) } ) } diff --git a/Sources/Internal/CoreStoreFetchedResultsController.swift b/Sources/Internal/CoreStoreFetchedResultsController.swift index d8c8b40..5bf7776 100644 --- a/Sources/Internal/CoreStoreFetchedResultsController.swift +++ b/Sources/Internal/CoreStoreFetchedResultsController.swift @@ -56,10 +56,6 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll ) applyFetchClauses(fetchRequest: fetchRequest) - CoreStore.assert( - fetchRequest.sortDescriptors?.isEmpty == false, - "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor." - ) if let from = from { self.reapplyAffectedStores = { fetchRequest, context in diff --git a/Sources/ObjectiveC/NSFetchedResultsController+ObjectiveC.swift b/Sources/ObjectiveC/NSFetchedResultsController+ObjectiveC.swift index d02fab5..bed5fbd 100644 --- a/Sources/ObjectiveC/NSFetchedResultsController+ObjectiveC.swift +++ b/Sources/ObjectiveC/NSFetchedResultsController+ObjectiveC.swift @@ -52,6 +52,11 @@ public extension NSFetchedResultsController { applyFetchClauses: { fetchRequest in fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) } + + CoreStore.assert( + fetchRequest.sortDescriptors?.isEmpty == false, + "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(CSOrderBy)) clause or any custom \(cs_typeName(CSFetchClause)) that provides a sort descriptor." + ) } ) } diff --git a/Sources/Observing/DataStack+Observing.swift b/Sources/Observing/DataStack+Observing.swift index 9795dbe..0c2c54d 100644 --- a/Sources/Observing/DataStack+Observing.swift +++ b/Sources/Observing/DataStack+Observing.swift @@ -85,6 +85,11 @@ public extension DataStack { applyFetchClauses: { fetchRequest in fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) } + + CoreStore.assert( + fetchRequest.sortDescriptors?.isEmpty == false, + "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor." + ) } ) } @@ -121,6 +126,11 @@ public extension DataStack { applyFetchClauses: { fetchRequest in fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) } + + CoreStore.assert( + fetchRequest.sortDescriptors?.isEmpty == false, + "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor." + ) }, createAsynchronously: createAsynchronously ) @@ -163,6 +173,11 @@ public extension DataStack { applyFetchClauses: { fetchRequest in fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) } + + CoreStore.assert( + fetchRequest.sortDescriptors?.isEmpty == false, + "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor." + ) } ) } @@ -202,6 +217,11 @@ public extension DataStack { applyFetchClauses: { fetchRequest in fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) } + + CoreStore.assert( + fetchRequest.sortDescriptors?.isEmpty == false, + "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor." + ) }, createAsynchronously: createAsynchronously )