NSFetchedResultsController Does anyone know of a cache implementation?

This is a bit of a strange question, so I will start from the very beginning ...

My need for an NSFetchedResultsController (NSFRC) is the ability to filter and sort after objects have been retrieved, mainly because filtering and sorting requires retrieving the retrieved objects themselves and is therefore not possible with NSFRC. So, I wrote my own class BSFetchedResultsController, the purpose of which is to replicate NSFRC functions (delegate notifications, automatic split and caching), but with added hooks for the user to set their own blocks for filtering and sorting. The code for this class is on github here if someone wants it: https://github.com/blindingskies/BSFetchedResultsController , although I wouldn’t assume the class is still ready for a drop instead of NSFRC.

So, I haven't implemented caching yet, mainly because I'm not quite sure how Apple implemented it. Caches are stored in binaries here:

{app dir} /Library/Caches/.CoreDataCaches/SectionInfoCaches/ {cache name} / sectionInfo

So, presumably, my class will need to store its caches in a similar place? How is this structure organized / working? The cache must store the NSFetchPredicate (or the properties necessary to recreate it), and it needs to somehow archive the extracted objects. But NSManagedObject does not match NSCoding, so how does it archive objects? Finally, during the NSNotificationCenterDidChangeNotification handler, the cache needs to be updated.

So, the real aspect of this is how to archive extracted objects, am I inclined to simply store object identifiers in an array? And then just get these objects out of context. It's enough?

If someone thought about how to implement

+1
source share
1 answer

Well, therefore, to answer my own question, I implemented the cache as follows:

Another class is created that saves the entity (NSEntityDescription), the selection predicate (NSPredicate), and the NSFetchPredicate sorting descriptors (NSArray), as well as sectionNameKeyPath objects and additional BSFetchedResultsController objects (predicate, filter, comparator). Make this class compatible with NSCoding.

Then, at the beginning of the performFetch method: if there is a cache name, unzip the object and see if the BSFRC properties match, and if so, use the data in the cache section.

Then add another notification handler to NSManagedObjectContextDidSaveNotification to clear the objects before the cache.

A few points ... I found that archiving NSFetchRequest directly (which corresponds to NSCoding) does not work, and at the moment I am only checking the name NSEntityDescription.

In addition, I do not cache the entire object graph, but only the URIR representation of the NSManagedObject NSManangedObjectID data. Then I repeat these URIs based on the context of the managed entity after checking the cache.

It seems to work, although I'm not sure how often I have to clear objects to cache ...

+1
source

All Articles