#import "JSONModel.h"
@interface User : JSONModel
@property(nonatomic, copy) NSString <Optional> *name;
@property(nonatomic, copy) NSString <Optional> *email;
@property(nonatomic, copy) NSString <Optional> *phone;
@property(nonatomic, copy) NSString <Optional> *password;
- (NSManagedObject *)createCoreDataModel:(NSManagedObjectContext *)context;
@end
#import "CDUser.h"
#import "NSManagedObject+MagicalDataImport.h"
@implementation User
- (Class)relatedCoreDataModelClass
{
return [CDUser class];
}
- (NSManagedObject *)createCoreDataModel:(NSManagedObjectContext *)context
{
return [[self relatedCoreDataModelClass] MR_importFromObject:[self toDictionary] inContext:context];
}
#import "User.h"
#import "NSManagedObject+JsonSerialization.h" // see https://github.com/Mozilla9/coredata-to-json
#import "NSManagedObject+MagicalFinders.h"
@implementation UserManager
- (User *)loadUserWithPredicate:(NSPredicate *)predicate inContext:(NSManagedContext *)context
{
CDUser *cdUser = [CDUser MR_findFirstWithPredicate:predicate inContext:context];
NSDictionary *dict = [cdUser toDictionary];
NSError *error = nil;
User *user = [User initWithDictionary:dict error:&error];
return user;
}
@end
-
Link to the lib https://github.com/Mozilla9/coredata-to-json
-
notSerializeToJSON
key for relationship.
It allows to break a relationship cycle. E.g. CDUser
has an inverse relantionship to entity CDJob
. So when we convert CDUser
to NSDictionary
, it performs converting of CDJob
too. But CDJob
has a relationship to CDUser
and it spawns an infinite cycle.
kMagicalRecordImportAttributeKeyMapKey
key for CD attributes.
Similar to the JSONKeyMapper
.