TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Apr 2016 |
Maintained by Nuno Salvador.
Stack with two independent NSManagedObjectContext instances, based on the performance analisys of this blog article. Automatically sync of changes by automatically merging changes between the background context and the main context. Supports automatic database migration.
IRCoreDataStack.backgroundManagedObjectContext
, calling contextSave automatically sends a NSManagedObjectContextDidSaveNotification.IRCoreDataStack.managedObjectContext
, by listening for the NSManagedObjectContextDidSaveNotification followed by a mergeChangesFromContextDidSaveNotification.IRCoreDataStack.managedObjectContext
...platform :ios, '8.0'
pod 'IRCoreDataStack', '~> 1.0'
Then, run the following command:
$ pod install
You've got the main IRCoreDataStack to setup the stack and an included category to help in operations.
IRCoreDataStack *coreDataStack = [[IRCoreDataStack alloc] initWithType:NSSQLiteStoreType
modelFilename:@"nameOfTheModelFile"
inBundle:[NSBundle mainBundle]];
You can make use of the method included category to create an entity on the correct context.
NSManagedObject *managedObject = [coreDataStack createEntityWithClassName:classNameString
attributesDictionary:attributesDictionary];
You can make use of the methods included category and you'll get the results in the completion block. This is the simple one:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"uuid == %@", [obj uuid]];
[self.coreDataStack fetchEntriesForClassName:className
withPredicate:predicate
sortDescriptors:nil
completionBlock:^(NSArray *results) {
// Your completion block
}];
After operations, you should save your changes, on the backgroundManagedObjectContext, using the helper method saveIntoBackgroundContextUsingBlock:
[self.coreDataStack saveIntoBackgroundContextUsingBlock:^(BOOL saved, NSError *error) {
// Your completion block
}];
Trivial as the previous ones. We've got other methods available too...
NSManagedObjectContext *bmoc = self.coreDataStack.backgroundManagedObjectContext;
[self.coreDataStack deleteAllFromEntity:nameEntity inManagedObjectContext:bmoc];
// Despite this method is called save, actually, from the previous operations, is going to delete the objects
[self.coreDataStack saveIntoContext:bmoc usingBlock:^(BOOL saved, NSError *error) {
// You should call processPendingChanges before inspecting deletedObjects of NSManagedObjectContext.
// At least if some relationships have deleteRule set to NSCascadeDeleteRule.
// http://stackoverflow.com/questions/5709302/when-and-how-often-to-call-processpendingchanges-to-ensure-graph-integrity
// if(saved) [bmoc processPendingChanges];
}];
MIT