Latest 1.0.4
Homepage https://github.com/iruleonu/IRCoreDataStack
License MIT
Platforms ios 8.0, tvos 9.0, watchos 2.0, osx 10.10, requires ARC
Frameworks Foundation, CoreData
Authors

Version
License
Platform

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.

Under the hood:

  • CRUD operations goes in the IRCoreDataStack.backgroundManagedObjectContext, calling contextSave automatically sends a NSManagedObjectContextDidSaveNotification.
  • Changes are merged into the IRCoreDataStack.managedObjectContext, by listening for the NSManagedObjectContextDidSaveNotification followed by a mergeChangesFromContextDidSaveNotification.
  • Fetches should be done on the main IRCoreDataStack.managedObjectContext

Installation

Podfile

platform :ios, '8.0'
pod 'IRCoreDataStack', '~> 1.0'

Then, run the following command:

$ pod install

Getting started

You’ve got the main IRCoreDataStack to setup the stack and an included category to help in operations.

Init stack

IRCoreDataStack *coreDataStack = [[IRCoreDataStack alloc] initWithType:NSSQLiteStoreType
                                                         modelFilename:@"nameOfTheModelFile"
                                                              inBundle:[NSBundle mainBundle]];

Insert

You can make use of the method included category to create an entity on the correct context.

NSManagedObject *managedObject = [coreDataStack createEntityWithClassName:classNameString
                                                     attributesDictionary:attributesDictionary];

Fetch

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
                             }];

Save

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
}];

Delete

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];
}];

License

MIT

Latest podspec

{
    "name": "IRCoreDataStack",
    "version": "1.0.4",
    "summary": "CoreDataStack with two independent NSManagedObjectContext instances",
    "homepage": "https://github.com/iruleonu/IRCoreDataStack",
    "license": {
        "type": "MIT",
        "file": "LICENSE.md"
    },
    "authors": {
        "Nuno Salvador": "[email protected]"
    },
    "source": {
        "git": "https://github.com/iruleonu/IRCoreDataStack.git",
        "tag": "1.0.4"
    },
    "requires_arc": true,
    "frameworks": [
        "Foundation",
        "CoreData"
    ],
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0",
        "watchos": "2.0",
        "osx": "10.10"
    },
    "subspecs": [
        {
            "name": "IOS_TVOS_WATCHOS",
            "source_files": "IRCoreDataStack",
            "platforms": {
                "ios": "8.0",
                "tvos": "9.0",
                "watchos": "2.0"
            }
        },
        {
            "name": "OSX",
            "source_files": [
                "IRCoreDataStack"
            ],
            "exclude_files": [
                "IRCoreDataStack/IRCoreDataStack+NSFecthedResultsController.{h,m}"
            ],
            "platforms": {
                "osx": "10.10"
            }
        }
    ]
}

Pin It on Pinterest

Share This