CRLoom

Core Data Threading Made Easy

View the Project on GitHub cruffenach/CRLoom

Introduction

CRLoom is an unobtrusive framework for easing the pains of moving your Core Data interactions off of the main thread.

Motivations

When creating iOS applications, I regularly would make calls to web services, be delivered a collection of objects and need to create or update objects in my NSManagedObjectContext with the data.

Commonly I would do this on the main thread and would notice my tableviews stutter, or for very large collections even freeze for a moment or two. To solve this I began doing my collection import work off of the main thread. I have generalized some of the patterns I used to move off the main thread into CRLoom, an unobtrusive framework for easing the pains of Core Data threading.

Quick Start

If you have an existing application using Core Data you're ready to use CRLoom.

+ (NSString*)uniqueDataIdentifierKey {
    //The key for the unique identifier in the dictionary representing the object coming from your API
}
+ (NSString*)uniqueModelIdentifierKey {
    //The key for the unique identifier in the NSManagedObject your are working in
}
- (BOOL)updateWithData:(NSDictionary*)data
           intoContext:(NSManagedObjectContext*)moc
             withCache:(NSCache*)cache
                 error:(NSError**)error {                                
    //Hydrate your core data object with the data from your API
}
- (BOOL)isIdenticalToData:(NSDictionary*)data {
    //Check whether the NSManagedObject is already identical to the data handed down.
}
+ (id)operationWithData:(id)data
     managedObjectClass:(Class)class
       guaranteedInsert:(BOOL)guaranteedInsert
        saveOnBatchSize:(NSUInteger)batchSize
               useCache:(BOOL)useCache
                  error:(NSError**)error;

Provide the data to by synthesize, the NSManagedObject subclass in question. There are a few other options you can set for your specific needs.

The import of that data will now be done on a background thread, saved and merged into the main thread context.

Continued Development

I have found this to be useful for many of the threading use cases I have run into, but there is a lot of room to grow. Issues and Pulls welcome. Enjoy.