Latest 0.1.2
Homepage https://github.com/FortyTwoEng/KFManagedObjectContextManager
License MIT
Frameworks CoreData
Authors ,

Version
License
Platform

Usage

To run the example project; clone the repo, and run pod install from the Example directory first.

Requirements

Initialize CoreData in your project

Installation

KFManagedObjectContextManager is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "KFManagedObjectContextManager"

Initialization

Add the following line your AppDelegate

[[KFManagedObjectContextManager sharedInstance] setMainThreadContext:self.managedObjectContext];

*managedObjectContext should be declared in your AppDelegate.

How to use:

When executing any core data code, wrap your code in the following method’s block:

  • (void)handleCoreDataOperation:(void (^)(NSManagedObjectContext* context))contextBlock;

Examples:

- (User*)getCurrentUser
{
    __block User* user = nil;

    [KFManagedObjectContextManager handleCoreDataOperation:^(NSManagedObjectContext* context) {

        user = [NSEntityDescription insertNewObjectForEntityForName:@"User"
                                         inManagedObjectContext:context];
        user.first_name = @"David";
        user.last_name = @"Elsonbaty";
        user.userSince = [NSDate date];

        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Whoops, saving failed: %@", [error localizedDescription]);
        } else {
            NSLog(@"Yay! Save Successful");
        }

    }];

    return user;
}

- (User*)getCurrentUser
{
    __block User* user = nil;

    [KFManagedObjectContextManager handleCoreDataOperation:^(NSManagedObjectContext* context) {

        NSFetchRequest* request = [[NSFetchRequest alloc] init];
        request.entity = [NSEntityDescription entityForName:@"User"
                                     inManagedObjectContext:context];
        NSSortDescriptor* sort = [[NSSortDescriptor alloc] initWithKey:@"first_name" ascending:YES];

        [request setSortDescriptors:@[ sort ]];

        NSError* error = nil;
        NSArray* objects = [context executeFetchRequest:request error:&error];
        user = [objects lastObject];

        if (!error && !user) {
            user = [NSEntityDescription
                    insertNewObjectForEntityForName:@"User"
                    inManagedObjectContext:context];
            user.userSince = [NSDate date];
        }

    }];

    return user;
}

Author

Eduardo Fonseca, @ebf
David Elsonbaty, @NSDavidObject

License

KFManagedObjectContextManager is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "KFManagedObjectContextManager",
    "version": "0.1.2",
    "summary": "A simple, safe and block-based wrapper around NSManagedObjectContexts",
    "description": "                      CoreData & Multithreading can get weird sometimes. At Kifi, we always try to avoid dependencies as much as we can. This simple wrapper tries to make things easier and simpler just by wrapping the creation and locks of your available NSManagedObjectContexts in a simple and easy to use blocks-based API.nn",
    "homepage": "https://github.com/FortyTwoEng/KFManagedObjectContextManager",
    "license": "MIT",
    "authors": {
        "Eduardo Fonseca": "[email protected]",
        "David Elsonbaty": "[email protected]"
    },
    "source": {
        "git": "https://github.com/FortyTwoEng/KFManagedObjectContextManager.git",
        "tag": "0.1.2"
    },
    "social_media_url": "https://twitter.com/kifi",
    "requires_arc": true,
    "source_files": "Classes",
    "frameworks": "CoreData"
}

Pin It on Pinterest

Share This