0

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.

oyalhi
  • 3,834
  • 4
  • 40
  • 45
  • 1
    [This answer](http://stackoverflow.com/a/11470560/3985749) might help. – pbasdf Nov 16 '16 at 17:53
  • @pbasdf thank you for the link. It does help and looks similar to what I am doing already. I create the context just before showing the pictures. However, more pictures can be updated in the background and more pictures can be added in the main thread, and this way I loose the automatic update of the parent context and have to manage manually which is more error prone. But I guess I have no other choice. – oyalhi Nov 19 '16 at 12:34

0 Answers0