From 6f655951aa43c3f79fea1c39fd34cc688751fb77 Mon Sep 17 00:00:00 2001 From: Ruslan Skorb Date: Sat, 24 Nov 2018 16:24:14 +0200 Subject: [PATCH 1/2] [ListMonitor] [numberOfObjects()] Calculate the number of objects in all sections by summing the number of objects stored in `NSFetchedResultsSectionInfo`. There is a performance problem in Swift when calling `count` method on an array with a large number of fetched objects. It requires casting the array between Objective-C and Swift, that is pretty slow. --- Sources/ListMonitor.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/ListMonitor.swift b/Sources/ListMonitor.swift index 86ce96e..1db479d 100644 --- a/Sources/ListMonitor.swift +++ b/Sources/ListMonitor.swift @@ -232,7 +232,10 @@ public final class ListMonitor: Hashable { !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?.count ?? 0 + return self.sections().reduce(0, { (numberOfObjects, sectionInfo) -> Int in + + return numberOfObjects + sectionInfo.numberOfObjects + }) } /** From a9a73fa5c44ccc7f77eacd1ca4da7597e9cd9a89 Mon Sep 17 00:00:00 2001 From: Ruslan Skorb Date: Thu, 6 Dec 2018 10:56:30 +0200 Subject: [PATCH 2/2] [ListMonitor] [numberOfObjects()] Return `count` of `fetchedObjects`. Casting `fetchedObjects` to `NSArray?` has better performance. https://github.com/JohnEstropia/CoreStore/pull/288 --- Sources/ListMonitor.swift | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Sources/ListMonitor.swift b/Sources/ListMonitor.swift index 1db479d..39bc603 100644 --- a/Sources/ListMonitor.swift +++ b/Sources/ListMonitor.swift @@ -232,10 +232,7 @@ public final class ListMonitor: Hashable { !self.isPendingRefetch || Thread.isMainThread, "Attempted to access a \(cs_typeName(self)) outside the main thread while a refetch is in progress." ) - return self.sections().reduce(0, { (numberOfObjects, sectionInfo) -> Int in - - return numberOfObjects + sectionInfo.numberOfObjects - }) + return (self.fetchedResultsController.fetchedObjects as NSArray?)?.count ?? 0 } /**