CRLoom 0.0.8

CRLoom 0.0.8

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.



CRLoom 0.0.8

  • By
  • Collin Ruffenach

CRLoom is a framework for helping with the import, update and querying of NSManagedObjects

Overview

NSManagedObjectImportOperation

This is an NSOperation subclass that created via

+ (instanceType)operationWithData:(id)data
               managedObjectClass:(Class)class
                 guaranteedInsert:(BOOL)guaranteedInsert
                  saveOnBatchSize:(NSUInteger)batchSize
                            pruneMissingObjects:(BOOL)pruneMissingObjects
                         useCache:(BOOL)useCache
                            error:(NSError* __autoreleasing *)error;

This operation can be added to an NSOperationQueue and will thread the work of importing the data. Setting useCache to YES will have the operation provide an NSCache to the thread doing the import work that will be used as a first layer to check to find existing objects before entire fetch requests are used.

NSManagedObject+CRLoom

This is a category that provides a generic implementation for importing and finding NSManagedObjects. The import and search methods take an NSManagedObjectContext as a parameter so they can work on different threads.

Importing Data

+ (NSArray*)importData:(id)data
           intoContext:(NSManagedObjectContext*)moc
             withCache:(NSCache*)cache
      guaranteedInsert:(BOOL)guaranteedInsert
       saveOnBatchSize:(NSUInteger)batchSize
                 error:(NSError* __autoreleasing *)error;

A cache can be provided to be used as a first check place for existing objects when processing this data into NSManagedObjects. Providing a cache here will ensure that those shared objects when created or retrieved for the first time will be held in a cache to reduce the total amount of fetch requests made.

Finding Objects

+ (instancetype)existingObjectWithIdentifierValue:(id)value
                                        inContext:(NSManagedObjectContext*)moc
                                        withCache:(NSCache*)cache
                                            error:(NSError* __autoreleasing *)error;

<CRLoomImport>

For these methods to work for a given NSManagedObject subclass must implement a few methods. The methods that need to be implemented provided by the <CRLoomImport> protocol.

Identifiers

A method that returns the model (Core Data) key that represents the object's unique identifier

+ (NSString*)uniqueModelIdentifierKey;

A method that returns the key that represents the object's unique identifier in the data being imported (e.g. key in JSON from your API)

+ (NSString*)uniqueDataIdentifierKey;

Object Updating

A method to update the object with data from the api. This allows work to be done on any given context should ensure error delivery, the cache will be used as a first layer for getting existing relationship objects before a full fetch request to core data is done.

- (BOOL)updateWithData:(NSDictionary*)data
           intoContext:(NSManagedObjectContext*)moc
             withCache:(NSCache*)cache
                 error:(NSError**)error;

A method to indicate whether a model's data is identical to an NSDictionary representation of that object.

- (BOOL)isIdenticalToData:(NSDictionary*)data;

Optional individual and collection predicates

An NSManagedObject subclass can also optionally implement methods to return the NSPredicate that should be used to "match" objects.

+ (NSPredicate*)predicateWithIdentiferValue:(id)identifierValue;
+ (NSPredicate*)predicateWithIdentiferCollection:(NSArray*)identifierCollection;