dchohfi / KeyValueObjectMapping

Automatic KeyValue Object Mapping for Objective-C, parse JSON/plist/Dictionary automatically
http://dchohfi.com/
MIT License
601 stars 90 forks source link

CoreData Support #31

Open kexoth opened 11 years ago

kexoth commented 11 years ago

Hey,

In your podspec is written that you support work with CoreData, but in practice I cannot find it in the documentation, nor the tests.

Could you provide a simple example how's that supposed to work?

I thought it would work out of the box, but this is what happens:

MyNSManagedObjectSubclass *contact = [mapping parseDictionary: myDictionary];

in this line I get CoreData: error: Failed to call designated initializer on NSManagedObject class 'MyNSManagedObjectSubclass'.

I know that this in CoreData should look something like:

MyNSManagedObjectSubclass *contact = [NSEntityDescription
                             insertNewObjectForEntityForName:@"MyNSManagedObjectSubclass"
                             inManagedObjectContext:backgroundContext];

.. but I don't know how to populate all the properties in my NSManagedObject with your library.

Thanks,

Kex

dchohfi commented 10 years ago

Hey man, how are you? First of all, your question is really good.

Do to things without coupling the framework with the coredata objects I let you customize how objects are created using blocks.

Try to look at this class: https://github.com/dchohfi/KeyValueObjectMapping/blob/master/KeyValueObjectMapping/DCCustomInitialize.h

and see if you understand how to use the blocks to create your objects, then inside that block you create using the normal NSEntityDescription from coredata.

If your question persist, please let me know and I try to write some code to you.

Cya man

xrnd commented 9 years ago

Hi dchohfi

I am having same issue as kex's.. Could please give small snippet of example for using block and initialising object as NSManagedObject.?

Thanks, Xrnd

plopesrocket commented 8 years ago

For me this worked: DCCustomInitialize _selfInitializer = [[DCCustomInitialize alloc] initWithBlockInitialize:^id(weak Class classOfObjectToGenerate, NSDictionary *weak values, __weak id parentObject) { // init your core data object and return it NSManagedObject temporaryObject = [[NSManagedObject alloc] initWithEntity:description insertIntoManagedObjectContext:nil]; return temporaryObject; } forClass:_class of your core data object];