- To use CoreData easily .
-
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 convinence class to replace NSFetchResultControler for UITableView)
-
ARCollectionViewFetchResultController(a convinence class to replace NSFetchResultControler for UICollectionView)
just drag ARCoreData to your project and edit you model , do not need to config any others
Import: #import "ARCoreData.h"
- pod 'ARCoreData', :git => 'https://github.com/AugustRush/ARCoreData.git'
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".
@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.
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 !!!
* 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]);
}];
##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 !