WIP: make fetching methods throwable

This commit is contained in:
John Estropia
2019-01-11 19:52:12 +09:00
parent 42d1f41939
commit 5777831565
16 changed files with 326 additions and 226 deletions

View File

@@ -136,7 +136,7 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
*/
public subscript(safeSectionIndex sectionIndex: Int, safeItemIndex itemIndex: Int) -> ObjectType? {
guard let section = self.sectionInfoAtIndex(safeSectionIndex: sectionIndex) else {
guard let section = self.sectionInfo(safelyAt: sectionIndex) else {
return nil
}
@@ -202,9 +202,9 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
- parameter section: the section index. Using an index outside the valid range will return `false`.
- returns: `true` if at least one object in the specified section exists, `false` otherwise
*/
public func hasObjectsInSection(_ section: Int) -> Bool {
public func hasObjects(in section: Int) -> Bool {
return self.numberOfObjectsInSection(safeSectionIndex: section)! > 0
return self.numberOfObjects(safelyIn: section)! > 0
}
/**
@@ -241,9 +241,9 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
- parameter section: the section index. Using an index outside the valid range will raise an exception.
- returns: the number of objects in the specified section
*/
public func numberOfObjectsInSection(_ section: Int) -> Int {
public func numberOfObjects(in section: Int) -> Int {
return self.sectionInfoAtIndex(section).numberOfObjects
return self.sectionInfo(at: section).numberOfObjects
}
/**
@@ -252,9 +252,9 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
- parameter section: the section index. Using an index outside the valid range will return `nil`.
- returns: the number of objects in the specified section
*/
public func numberOfObjectsInSection(safeSectionIndex section: Int) -> Int? {
public func numberOfObjects(safelyIn section: Int) -> Int? {
return self.sectionInfoAtIndex(safeSectionIndex: section)?.numberOfObjects
return self.sectionInfo(safelyAt: section)?.numberOfObjects
}
/**
@@ -263,7 +263,7 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
- parameter section: the section index. Using an index outside the valid range will raise an exception.
- returns: the `NSFetchedResultsSectionInfo` for the specified section
*/
public func sectionInfoAtIndex(_ section: Int) -> NSFetchedResultsSectionInfo {
public func sectionInfo(at section: Int) -> NSFetchedResultsSectionInfo {
CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread,
@@ -278,7 +278,7 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
- parameter section: the section index. Using an index outside the valid range will return `nil`.
- returns: the `NSFetchedResultsSectionInfo` for the specified section, or `nil` if the section index is out of bounds.
*/
public func sectionInfoAtIndex(safeSectionIndex section: Int) -> NSFetchedResultsSectionInfo? {
public func sectionInfo(safelyAt section: Int) -> NSFetchedResultsSectionInfo? {
CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread,
@@ -312,17 +312,17 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
/**
Returns the target section for a specified "Section Index" title and index.
- parameter title: the title of the Section Index
- parameter index: the index of the Section Index
- parameter sectionIndexTitle: the title of the Section Index
- parameter sectionIndex: the index of the Section Index
- returns: the target section for the specified "Section Index" title and index.
*/
public func targetSectionForSectionIndex(title: String, index: Int) -> Int {
public func targetSection(forSectionIndexTitle sectionIndexTitle: String, at sectionIndex: Int) -> Int {
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.section(forSectionIndexTitle: title, at: index)
return self.fetchedResultsController.section(forSectionIndexTitle: sectionIndexTitle, at: sectionIndex)
}
/**
@@ -345,7 +345,7 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
- parameter object: the `DynamicObject` to search the index of
- returns: the index of the `DynamicObject` if it exists in the `ListMonitor`'s fetched objects, or `nil` if not found.
*/
public func indexOf(_ object: ObjectType) -> Int? {
public func index(of object: ObjectType) -> Int? {
CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread,
@@ -364,7 +364,7 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
- parameter object: the `DynamicObject` to search the index of
- returns: the `IndexPath` of the `DynamicObject` if it exists in the `ListMonitor`'s fetched objects, or `nil` if not found.
*/
public func indexPathOf(_ object: ObjectType) -> IndexPath? {
public func indexPath(of object: ObjectType) -> IndexPath? {
CoreStore.assert(
!self.isPendingRefetch || Thread.isMainThread,
@@ -1164,6 +1164,57 @@ public final class ListMonitor<D: DynamicObject>: Hashable {
try! self.fetchedResultsController.performFetchFromSpecifiedStores()
}
}
// MARK: Deprecated
@available(*, deprecated, renamed: "hasObjects(in:)")
public func hasObjectsInSection(_ section: Int) -> Bool {
return self.hasObjects(in: section)
}
@available(*, deprecated, renamed: "numberOfObjects(in:)")
public func numberOfObjectsInSection(_ section: Int) -> Int {
return self.numberOfObjects(in: section)
}
@available(*, deprecated, renamed: "numberOfObjects(safelyIn:)")
public func numberOfObjectsInSection(safeSectionIndex section: Int) -> Int? {
return self.numberOfObjects(safelyIn: section)
}
@available(*, deprecated, renamed: "sectionInfo(at:)")
public func sectionInfoAtIndex(_ section: Int) -> NSFetchedResultsSectionInfo {
return self.sectionInfo(at: section)
}
@available(*, deprecated, renamed: "sectionInfo(safelyAt:)")
public func sectionInfoAtIndex(safeSectionIndex section: Int) -> NSFetchedResultsSectionInfo? {
return self.sectionInfo(safelyAt: section)
}
@available(*, deprecated, renamed: "targetSection(forSectionIndexTitle:at:)")
public func targetSectionForSectionIndex(title: String, index: Int) -> Int {
return self.targetSection(forSectionIndexTitle: title, at: index)
}
@available(*, deprecated, renamed: "index(of:)")
public func indexOf(_ object: ObjectType) -> Int? {
return self.index(of: object)
}
@available(*, deprecated, renamed: "indexPath(of:)")
public func indexPathOf(_ object: ObjectType) -> IndexPath? {
return self.indexPath(of: object)
}
}
@@ -1192,9 +1243,9 @@ extension ListMonitor where ListMonitor.ObjectType: 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) -> [ObjectType] {
public func objects(in section: Int) -> [ObjectType] {
return (self.sectionInfoAtIndex(section).objects as! [ObjectType]?) ?? []
return (self.sectionInfo(at: section).objects as! [ObjectType]?) ?? []
}
/**
@@ -1203,9 +1254,24 @@ extension ListMonitor where ListMonitor.ObjectType: 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) -> [ObjectType]? {
public func objects(safelyIn section: Int) -> [ObjectType]? {
return self.sectionInfoAtIndex(safeSectionIndex: section)?.objects as! [ObjectType]?
return self.sectionInfo(safelyAt: section)?.objects as! [ObjectType]?
}
// MARK: Deprecated
@available(*, deprecated, renamed: "objects(in:)")
public func objectsInSection(_ section: Int) -> [ObjectType] {
return self.objects(in: section)
}
@available(*, deprecated, renamed: "objects(safelyIn:)")
public func objectsInSection(safeSectionIndex section: Int) -> [ObjectType]? {
return self.objects(safelyIn: section)
}
}
@@ -1236,9 +1302,9 @@ extension ListMonitor where ListMonitor.ObjectType: 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) -> [ObjectType] {
public func objects(in section: Int) -> [ObjectType] {
return (self.sectionInfoAtIndex(section).objects ?? [])
return (self.sectionInfo(at: section).objects ?? [])
.map({ ObjectType.cs_fromRaw(object: $0 as! NSManagedObject) })
}
@@ -1248,11 +1314,26 @@ extension ListMonitor where ListMonitor.ObjectType: 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) -> [ObjectType]? {
public func objects(safelyIn section: Int) -> [ObjectType]? {
return (self.sectionInfoAtIndex(safeSectionIndex: section)?.objects)?
return (self.sectionInfo(safelyAt: section)?.objects)?
.map({ ObjectType.cs_fromRaw(object: $0 as! NSManagedObject) })
}
// MARK: Deprecated
@available(*, deprecated, renamed: "objects(in:)")
public func objectsInSection(_ section: Int) -> [ObjectType] {
return self.objects(in: section)
}
@available(*, deprecated, renamed: "objects(safelyIn:)")
public func objectsInSection(safeSectionIndex section: Int) -> [ObjectType]? {
return self.objects(safelyIn: section)
}
}