In my app there is a photos section, where users can insert lots of photos. The photos are saved in Core Data as image and thumbnail image. Allows external storage option is checked.
I am using NSFetchedResultsController, and NSFetchRequest.fetchBatchSize is set to 27 as can be seen below. I also set returnsObjectsAsFaults to true, so that the full images are not read from disk until needed.
lazy var frc: NSFetchedResultsController<EventImage> = {
let request = NSFetchRequest<EventImage>(entityName: EventImage.entityName)
request.sortDescriptors = [NSSortDescriptor(key: "timeInterval", ascending: false)]
request.fetchBatchSize = 27
request.predicate = NSPredicate(format: "event = %@", self.event.objectID)
request.returnsObjectsAsFaults = true
let newFRC = NSFetchedResultsController(
fetchRequest: request,
managedObjectContext: self.context,
sectionNameKeyPath: nil,
cacheName: nil)
newFRC.delegate = self
return newFRC
}()
Now, if the NSManagedObjectContext has no parent, then fetchBatchSize is honored and can be observed from the sql logs.
However, in my case I have a privateQueueConcurrencyType context for downloading and uploading which has a parent context with mainQueueConcurrencyType for displaying data which in turn has a parent privateQueueConcurrencyType context for saving to disk.
Now in my case (tested and verified), if the NSManagedObjectContext has a parent, then fetchBatchSize is ignored, and all images are fetched from disk which is very slow. Cache helps, but only on the second run.
I am assuming I am not the only one having this problem (fetchBatchSize is ignored if context has a parent problem). How did you solve this?
Any pointers are appreciated.