How do i avoid running out of memory when performing a task on every item of core data?

后端 未结 2 1266
不知归路
不知归路 2021-01-29 03:32

I\'m trying to perform a task similar to this:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@\"Entry\"];

        NSPredicate *predicate          


        
相关标签:
2条回答
  • 2021-01-29 04:14

    You guessed right. You have to use setFetchBatchSize:. This will only return the first 20 objects. If you want to get the next 20 objects, you have to set the offset using setFetchOffset:.

    0 讨论(0)
  • 2021-01-29 04:16

    This is a perfect case for using an @autoreleasepool

    Place an @autoreleasepool around all the code which gets executed in your loop. This will automatically release the objects after they get processed so your memory footprint stays low. If using this technique you shouldn't have to specify a batch size.

    See the apple docs here for more information;

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html

    0 讨论(0)
提交回复
热议问题