Code Monkey home page Code Monkey logo

arcoredata's Introduction

  • ARCoreData is a library to make CoreData easily. we can become a good friend with CoreData now. Don't need any config,just enjoy a high performance object storage。

MOC Model


Features

  • custom primaryKey, currently support NSString , NSInteger(int_64,int_32,int_16..),NSNumber

  • JSON(NSDictionary) -> NSManageObject(In theory,support any KVC object)

  • JSONs(NSArray) -> NSManageObject(s)

  • Safety Mutlie thread

  • Easily fetch

  • ARTableViewFetchResultController(a convenience class to replace NSFetchResultControler for UITableView)

  • ARCollectionViewFetchResultController(a convenience class to replace NSFetchResultControler for UICollectionView)

Install

Manualy

just drag ARCoreData to your project and edit you model , do not need to config any others
Import: #import "ARCoreData.h"

Cocoapods

Getting start


Creat new object

if you have a Person and Dog class like this:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Dog;

@interface Person : NSManagedObject<ARManageObjectMappingProtocol>

@property (nonatomic, retain) NSString * name;
@property (nonatomic) BOOL sex;
@property (nonatomic, retain) NSString * guid;
@property (nonatomic, retain) NSSet *dogs;
@end

@interface Person (CoreDataGeneratedAccessors)

- (void)addDogsObject:(Dog *)value;
- (void)removeDogsObject:(Dog *)value;
- (void)addDogs:(NSSet *)values;
- (void)removeDogs:(NSSet *)values;

@end

_____________________


#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Person;

@interface Dog : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic) int64_t guid;
@property (nonatomic, retain) NSSet *owners;
@end

@interface Dog (CoreDataGeneratedAccessors)

- (void)addOwnersObject:(Person *)value;
- (void)removeOwnersObject:(Person *)value;
- (void)addOwners:(NSSet *)values;
- (void)removeOwners:(NSSet *)values;

@end

you can create a person like this:

Person *person = [Person AR_new];
person.name = @"aaa";
person.guid = @"1";
    
Dog *pet = [Dog AR_new];
pet.name = @"doggie";
pet.guid = 123;

[person addDogsObject:pet];

[Person AR_saveAndWait];//this is save method

if you want to use a JSON(KVC object) create a new person, you should impliment , this is my Person and Dog .m file.

@implementation Person

@dynamic name;
@dynamic sex;
@dynamic guid;
@dynamic dogs;

+(NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{@"guid":@"g",
             @"name":@"n",
             @"sex":@"s",
             @"dogs":@"ds"};
}

+(NSString *)primaryKey
{
    return @"guid";
}

@end


**********************************


@implementation Dog

@dynamic name;
@dynamic owners;
@dynamic guid;

+(NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{@"name":@"n",
             @"owners":@"o",
             @"guid":@"g.uid"};
}

+(NSString *)primaryKey
{
    return @"guid";
}

@end

and then, you can create a Person like this:

        Person *person = [Person AR_newOrUpdateWithJSON:@{@"n":name,
                                                @"g":@"3",
                                                @"s":@YES,
                                                @"ds":@[@{@"n":@"haha",
                                                          @"g":@{@"uid":@"7",
                                                                 @"extra":@34}},
                                                        @{@"n":name,
                                                          @"g":@{@"uid":@"6",
                                                                 @"extra":@34}}]}];

if you implement the class method +(NSString *)primaryKey; you just can create an unique person through a same "guid".

Mapping (ARManageObjectMappingProtocol)

@protocol ARManageObjectMappingProtocol <NSObject>

+(NSDictionary *)JSONKeyPathsByPropertyKey;

@optional
+(NSString *)primaryKey;

@end

I have implemented some methods, you can use server response directly to create an(or a array) manageObject(s), there have two methods :

+(id)AR_newOrUpdateWithJSON:(NSDictionary *)JSON;

+(NSArray *)AR_newOrUpdateWithJSONs:(NSArray *)JSONs;

you have seen ARManageObjectMappingProtocol , yes ,this protocol have two methods,like famous mapping library Mantle, but it must be faster than Mantle. you just to impliment these two methods , and just use two methods the above, it will automaticly transfrom a JSON(s) or KVC object(s) to NSManageObject(s) instance. Overall, it's very easy and safe.

Fetch objects

there have a lot of methods to help you fetch objects convenience and faster, you can see the file Fetch.

Example:

    NSArray *allPersons = [Person AR_all];
    
    NSArray *persons = [Person AR_where:@"name = %@",@"a name"];
    
    NSArray *persons = [Person AR_whereProperty:@"guid" equalTo:@3];

and so on !!!

Saving objects

* sync
    [Person AR_saveAndWait];

    [Person AR_saveAndWaitCompletion:^(BOOL success, NSError *error) {
        // fetch objects or do UI work
    }];

* async

	[Person AR_saveCompletion:^(BOOL success, NSError *error) {
        NSLog(@"all dog is %@ dog count is %ld",[Dog AR_all],[Dog AR_count]);
        
        NSLog(@"all person is %@ dog count is %ld",[Person AR_all],[Person AR_count]);
    }];


TODO:

  • Migration

  • Encryption

  • Fixed bugs

TL;DR:

there have more methods i have created for you, you can see it in my Demo project after. this library also worked in swift fine !

arcoredata's People

Contributors

augustrush avatar gabrielpeart avatar

Watchers

 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.