Inherits from NSObject
Declared in MMRecordCache.h
MMRecordCache.m

Overview

This class encapsulates the functionality for caching managed objects in correspondence to a particular NSURLRequest’s NSCachedURLResponse. It is meant to be a private class used by MMRecord should you wish to enable caching support for a given entity type. However, it could be used as a standalone way to cache NSManagedObject’s for a given NSURLRequest. Your milage may vary.

Caching

In general, the best way to implement caching is through the use of NSURLCache and the server’s implementation of the HTTP Cache Control headers. In the event that this is not enough, MMRecord does provide an additional level of caching support. The caching provided by MMRecord is in accordance with the cache control headers and augments the system provided by NSURLCache. If caching is enabled then MMRecord will store the object IDs for each request/response pair that were parsed as part of the last response in a separate SQLite persistent store. It’s important to note that these are stored UNENCRYPTED. IF DATA SECURITY IS A REQUIREMENT CACHING SHOULD NOT BE USED!

Configuration

Please see the MMRecord header for more information about configuring your app to support caching.

Tasks

  • + hasResultsForKey:

    This method returns YES if there are cached results for a given key. The key is typically the absolute URL for a given NSURLRequest. You can specify the key using the cacheRecords method below.

  • + getCachedResultsForRequest:cacheKey:metaKeyPath:context:cacheResultBlock:

    This method retrieves cached results in a given context. If results are cached, it will fetch those objectIDs from the internal caching persistent store and attempt to obtain records matching those object IDs in the provided managed object context. Those records, along with the cached response object (if any) will be returned in the cache result block. This method will also respect the caching policy of the NSURLCache and of this NSURLRequest. If the response for that request is not cached, no results will be returned – regardless of what is found in the cache persistent store.

  • + cacheRecords:withMetadata:forKey:fromContext:

    This method caches the objectIDs of the given records. Those records should be subclasses of NSManagedObject. The key provided will be used to locate those records later if a subsequent request is made for a certain NSURLRequest which you wish to associate with that key.

Class Methods

cacheRecords:withMetadata:forKey:fromContext:

This method caches the objectIDs of the given records. Those records should be subclasses of NSManagedObject. The key provided will be used to locate those records later if a subsequent request is made for a certain NSURLRequest which you wish to associate with that key.

+ (void)cacheRecords:(NSArray *)records withMetadata:(NSDictionary *)metadata forKey:(NSString *)key fromContext:(NSManagedObjectContext *)context

Parameters

records

An array of records that should be cached.

metadata

Metadata contained in the response that should also be cached.

key

A key that will be associated with and used to identify cached results.

context

A managed object context where these records exist.

Discussion

This method caches the objectIDs of the given records. Those records should be subclasses of NSManagedObject. The key provided will be used to locate those records later if a subsequent request is made for a certain NSURLRequest which you wish to associate with that key.

Declared In

MMRecordCache.h

getCachedResultsForRequest:cacheKey:metaKeyPath:context:cacheResultBlock:

This method retrieves cached results in a given context. If results are cached, it will fetch those objectIDs from the internal caching persistent store and attempt to obtain records matching those object IDs in the provided managed object context. Those records, along with the cached response object (if any) will be returned in the cache result block. This method will also respect the caching policy of the NSURLCache and of this NSURLRequest. If the response for that request is not cached, no results will be returned – regardless of what is found in the cache persistent store.

+ (void)getCachedResultsForRequest:(NSURLRequest *)request cacheKey:(NSString *)cacheKey metaKeyPath:(NSString *)metaKeyPath context:(NSManagedObjectContext *)context cacheResultBlock:(void ( ^ ) ( NSArray *cachedResults , id responseObject ))cacheResultBlock

Parameters

request

Request object to obtain cached results for.

cacheKey

Key used to identify the cached results.

metaKeyPath

Key used to identify metadata that may have been returned in the response.

context

managed object context to return cached results in.

cacheResultBlock

Result block that will be executed when this method finishes checking for cached results.

Discussion

This method retrieves cached results in a given context. If results are cached, it will fetch those objectIDs from the internal caching persistent store and attempt to obtain records matching those object IDs in the provided managed object context. Those records, along with the cached response object (if any) will be returned in the cache result block. This method will also respect the caching policy of the NSURLCache and of this NSURLRequest. If the response for that request is not cached, no results will be returned – regardless of what is found in the cache persistent store.

Declared In

MMRecordCache.h

hasResultsForKey:

This method returns YES if there are cached results for a given key. The key is typically the absolute URL for a given NSURLRequest. You can specify the key using the cacheRecords method below.

+ (BOOL)hasResultsForKey:(NSString *)cacheKey

Parameters

cacheKey

The key used to identify cached results.

Return Value

BOOL YES for has results, NO otherwise.

Discussion

This method returns YES if there are cached results for a given key. The key is typically the absolute URL for a given NSURLRequest. You can specify the key using the cacheRecords method below.

Declared In

MMRecordCache.h