Code Monkey home page Code Monkey logo

asynccoredatawrapper's Introduction

asyncCoreDataWrapper

从此又能和CoreData愉快地玩耍啦(Easy way to play with CoreData)

First, prepare CoreData environment:

1. import CoreData Framework

img1

2. add xdatamodeld file and design your model and generate sub class file

img2

generate sub class in Editor > Create NSManagedContext SubClass

3. copy files to your project

4. init instance in appDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[mmDAO instance] setupEnvModel:@"asyncCoreDataWrapper" DbFile:@"asyncCoreDataWrapper.sqlite"];
    return YES;
}


- (void)applicationWillTerminate:(UIApplication *)application
{
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}

- (void)saveContext
{
    NSError *error = nil;
    if ([[mmDAO instance].bgObjectContext hasChanges]) {
        [[mmDAO instance].bgObjectContext save:&error];
    }
}

5. import catalog class in Prefix.pch file than you can use it anywhere

#import <Availability.h>

#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
    #import "NSManagedObject+helper.h"
#endif

How to use

Create new object

Entity *task = [Entity createNew];
task.task_id = @([self genId]);
task.title = _txInputBox.text;
task.detail = @"[not sure]";
task.done = NO;

Delete object

Entity *task = _dataArray[indexPath.row];
[Entity delobject:task];

Save Changes

[Entity save:^(NSError *error) {
    _txInputBox.text = @"";
    [self fetchEntitys];
}];

Fetch Data Array

sync way:

NSArray *results = [Entity filter:@"task_id>10" orderby:@[@"task_id"] offset:0 limit:0];

async way:

[Entity filter:nil orderby:@[@"task_id"] offset:0 limit:0 on:^(NSArray *result, NSError *error) {
    _dataArray = result;
    [_mainTable reloadData]; //reload table view
}];

Do complex operation asynchronously

[Entity async:^id(NSManagedObjectContext *ctx, NSString *className) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:className];
        [request setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"task_id" ascending:YES]]];
        NSError *error;
        NSArray *dataArray = [ctx executeFetchRequest:request error:&error];
        if (error) {
            return error;
        }else{
            return dataArray;
        }

    } result:^(NSArray *result, NSError *error) {
        _dataArray = result;
        [_mainTable reloadData];
    }];

asynccoredatawrapper's People

Contributors

ipconfiger avatar

Watchers

robbie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.