Skip to content
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

Open
expkzb opened this issue Mar 30, 2014 · 2 comments
Open

CoreData Sample Code #51

expkzb opened this issue Mar 30, 2014 · 2 comments

Comments

@expkzb
Copy link

expkzb commented Mar 30, 2014

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 : )

@pronebird
Copy link

I don't understand how to use it with CoreData either.

@R3inhardH
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants