Avoid duplicated entries when importing data with Magical Record #357

Closed
chrischenyc opened this Issue Dec 29, 2012 · 2 comments

Comments

Projects
None yet
2 participants
@chrischenyc

I'm using Magical Record to facilitate Core Data operations. Imagine we have a set of json like this, and a Core Data model defined exactly the same:

{
"cars": [
{
"name": "Corolla",
"brand": {
"name": "Toyota"
},
"price": 20000
},
{
"name": "Pirus",
"brand": {
"name": "Toyota"
},
"price": 50000
},
{
"name": "RAV-4",
"brand": {
"name": "Toyota"
},
"price": 30000
},
{
"name": "Golf",
"brand": {
"name": "VW"
},
"price": 40000
},
{
"name": "Polo",
"brand": {
"name": "VW"
},
"price": 20000
}
]
}
Now, if we use Magical Record helper method:

  • (BOOL) MR_importValuesForKeysWithObject:(id)objectData;
    or
  • (id) MR_importFromObject:(id)data;
    it will be imported as 5 entries of Car and 5 entries of Brand.

However, in our Core Data model, Car-Brand relationship is a many-to-many one, and Brand's name attribute is supposed to be unique, so I'm expecting 5 entries of Car and 2 entries of Brand (Toyota and VW).

My question is how to maintain the data uniqueness while importing with Core Data. Is this something I can define in Core Data model, like unique attribute? or is it I need to override Magical Record's import method?

@casademora

This comment has been minimized.

Show comment Hide comment
@casademora

casademora Dec 29, 2012

Owner

Have to set the primaryRelatedAttribute key as specified in this blog post: http://www.cimgf.com/2012/05/29/importing-data-made-easy/

Saul Mora
@casademora
saul@casademora.com

On Saturday, December 29, 2012 at 2:10 PM, Chris Chen wrote:

I'm using Magical Record to facilitate Core Data operations. Imagine we have a set of json like this, and a Core Data model defined exactly the same:
{
"cars": [
{
"name": "Corolla",
"brand": {
"name": "Toyota"
},
"price": 20000
},
{
"name": "Pirus",
"brand": {
"name": "Toyota"
},
"price": 50000
},
{
"name": "RAV-4",
"brand": {
"name": "Toyota"
},
"price": 30000
},
{
"name": "Golf",
"brand": {
"name": "VW"
},
"price": 40000
},
{
"name": "Polo",
"brand": {
"name": "VW"
},
"price": 20000
}
]
}
Now, if we use Magical Record helper method:
(BOOL) MR_importValuesForKeysWithObject:(id)objectData;
or
(id) MR_importFromObject:(id)data;
it will be imported as 5 entries of Car and 5 entries of Brand.
However, in our Core Data model, Car-Brand relationship is a many-to-many one, and Brand's name attribute is supposed to be unique, so I'm expecting 5 entries of Car and 2 entries of Brand (Toyota and VW).
My question is how to maintain the data uniqueness while importing with Core Data. Is this something I can define in Core Data model, like unique attribute? or is it I need to override Magical Record's import method?


Reply to this email directly or view it on GitHub (#357).

Owner

casademora commented Dec 29, 2012

Have to set the primaryRelatedAttribute key as specified in this blog post: http://www.cimgf.com/2012/05/29/importing-data-made-easy/

Saul Mora
@casademora
saul@casademora.com

On Saturday, December 29, 2012 at 2:10 PM, Chris Chen wrote:

I'm using Magical Record to facilitate Core Data operations. Imagine we have a set of json like this, and a Core Data model defined exactly the same:
{
"cars": [
{
"name": "Corolla",
"brand": {
"name": "Toyota"
},
"price": 20000
},
{
"name": "Pirus",
"brand": {
"name": "Toyota"
},
"price": 50000
},
{
"name": "RAV-4",
"brand": {
"name": "Toyota"
},
"price": 30000
},
{
"name": "Golf",
"brand": {
"name": "VW"
},
"price": 40000
},
{
"name": "Polo",
"brand": {
"name": "VW"
},
"price": 20000
}
]
}
Now, if we use Magical Record helper method:
(BOOL) MR_importValuesForKeysWithObject:(id)objectData;
or
(id) MR_importFromObject:(id)data;
it will be imported as 5 entries of Car and 5 entries of Brand.
However, in our Core Data model, Car-Brand relationship is a many-to-many one, and Brand's name attribute is supposed to be unique, so I'm expecting 5 entries of Car and 2 entries of Brand (Toyota and VW).
My question is how to maintain the data uniqueness while importing with Core Data. Is this something I can define in Core Data model, like unique attribute? or is it I need to override Magical Record's import method?


Reply to this email directly or view it on GitHub (#357).

@chrischenyc

This comment has been minimized.

Show comment Hide comment
@chrischenyc

chrischenyc Dec 30, 2012

thank you

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment