-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CoreData Sample Code #51
Comments
I don't understand how to use it with CoreData either. |
Hi, I had to deal with CoreData a few days ago and found out that you have to use a DCCustomInitialize object. Here is a short example that should give the idea how to do this: If you have a dictionary which looks like this: {
"user_name": "John",
"age_integer": 23
} and the according classes: User.h #import <CoreData/CoreData.h>
NS_ASSUME_NONNULL_BEGIN
@interface User : NSManagedObject
@end
NS_ASSUME_NONNULL_END
#import "User+CoreDataProperties.h" User+CoreDataProperties.h NS_ASSUME_NONNULL_BEGIN
@interface VFBConfigPackage (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *userName;
@property (nullable, nonatomic, retain) NSNumber *userAge;
@end
NS_ASSUME_NONNULL_END ViewController.h - (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *json = //see example
DCCustomInitialize *initializer = [[DCCustomInitialize alloc] initWithBlockInitialize:^id(__weak Class classOfObjectToGenerate, NSDictionary *__weak values, __weak id parentObject) {
User *managedObject = (User *)[NSEntityDescription insertNewObjectForEntityForName:@"User"
inManagedObjectContext:managedObjectContext];
return managedObject;
} forClass:[User class]];
DCParserConfiguration *config = [DCParserConfiguration configuration];
[config addCustomInitializersObject:initializer];
[config addObjectMapping:[DCObjectMapping mapKeyPath:@"user_name" toAttribute:@"userName" onClass:[User class]]];
[config addObjectMapping:[DCObjectMapping mapKeyPath:@"age_integer" toAttribute:@"userAge" onClass:[User class]]];
DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:[User class] andConfiguration:config];
User *user = [parser parseDictionary:jsonDict];
//Continue with the User Object
} Hope this helps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi dchohfi,
I really don't know how to deal with core data object mapping using DCCustomInitialize.
Could you please write a few sample codes for me? THX : )
The text was updated successfully, but these errors were encountered: