Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MagicalRecord how to add objects in relationships

in iOS5.0, using arc, i'm using Magical record to create objects in core data. I have a product object that has a one to many relationship called 'currentPromotions' with Promotion objects. Promotion object has an inverse relationship 'parentProduct', too. I am able to create these objects, but am stymied at how to create the relationship and attach the Promotion objects to the product using MagicalRecord in github.

Also the documentation on magical record is too sparse. any pointers on this will also be helpful.

Thanks in advance for your help on this

like image 683
Inforeqd51 A Avatar asked Feb 19 '23 07:02

Inforeqd51 A


1 Answers

Most information on Core Data applies when using MagicalRecord. I suggest looking there first when looking for documentation. MagicalRecord basically tries to codify what the docs tell you is a good idea.

For your problem though, it seems that you have a relationship like this:

Product <---> Promotion

You connect your two instances like this:

Product *a = //...;
a.promotion = [Promotion createEntity];

Everything else is done for you, not by MagicalRecord, but Core Data. If you want to add a product to your collection of products for a promotion, you can do:

Promotion *p = //...;
[p addProductOjbect:[Product createEntity]];

The addProductObject: method is created for you at runtime by Core Data.

I highly suggest that you learn more about Core Data when looking into MagicalRecord, since MagicalRecord does not really hide anything. It just makes the "easy" stuff simple, and the hard stuff possible.

like image 185
casademora Avatar answered Mar 05 '23 23:03

casademora