The easist way to use core data!
just drag ARCoreData to your project and edit you model , do not need to config any others
Import: #import "ARCoreData.h"
+ (id)AR_new;
+ (id)AR_newInContext:(NSManagedObjectContext *)context;
+ (id)AR_newOrUpdateWithJSON:(id)JSON inContext:(NSManagedObjectContext *)context;
+ (NSArray *)AR_newOrUpdateWithJSONs:(NSArray *)JSONs inContext:(NSManagedObjectContext *)context;
+ (id)AR_newOrUpdateWithJSON:(id)JSON relationshipMergePolicy:(ARRelationshipMergePolicy)policy inContext:(NSManagedObjectContext *)context;
+ (NSArray *)AR_newOrUpdateWithJSONs:(NSArray *)JSONs relationshipsMergePolicy:(ARRelationshipMergePolicy)policy inContext:(NSManagedObjectContext *)context;
@protocol ARManageObjectMappingProtocol <NSObject>
+(NSDictionary *)JSONKeyPathsByPropertyKey;
@optional
+(NSSet *)uniquedPropertyKeys;
you have seen ARManageObjectMappingProtocol, yes, this protocol has two methods, like famous mapping library Mantle, but it must be faster than Mantle. You just need to implement and use these two methods, it will automatically transfrom a JSON(s) or KVC object(s) to NSManageObject(s) instance. Overall, it’s very easy and safe.
I have implemented some methods, you can use server response directly to create an(or a array) manageObject(s),
there are some methods you can use to sync JSONs to persistance store (.sqlite):
+ (id)AR_newOrUpdateWithJSON:(id)JSON inContext:(NSManagedObjectContext *)context;
+ (NSArray *)AR_newOrUpdateWithJSONs:(NSArray *)JSONs relationshipsMergePolicy:(ARRelationshipMergePolicy)policy inContext:(NSManagedObjectContext *)context;
+(id)AR_anyone;
+(NSArray *)AR_all;
+(NSArray *)AR_whereProperty:(NSString *)property
equalTo:(id)value;
+(NSArray *)AR_where:(NSString *)condition,...;
+(NSUInteger)AR_count;
.......
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 !!!
* sync
//NSManageObjectID is thread safety
[Person AR_saveAndWait:^(NSManagedObjectContext *currentContext) {
Dog *deleteDog = (Dog *)[currentContext existingObjectWithID:objectID error:nil];
[currentContext deleteObject:deleteDog];
}];
* async
[Person AR_save:^(NSManagedObjectContext *currentContext) {
[Person AR_newOrUpdateWithJSON:@{@"n":name,
@"g":@"3",
@"s":@YES,
@"ds":@[@{@"n":@"123",
@"g":@{@"uid":@"123",
@"extra":@34}},
@{@"n":name,
@"g":@{@"uid":@"123",
@"extra":@34}}]} inContext:currentContext];
} completion:^(NSError *error) {
ARLog(@"all person count is %lu",(unsigned long)[Person AR_count]);
ARLog(@"all dogs count is %lu",(unsigned long)[Dog AR_count]);
}];
Migration
Encryption
there have more methods I have created, you can see it in the Demo project after. This library also works in
Swift!