问题
I'm trying to use Magical Record
to import data into my CoreData
db.
I'm working on the entity Person
at the moment.
A Person
has firstName
, lastName
, personID
attributes. (I know I don't need the idString, I'm not using it internally, it is used to talk back to the server).
Anyway, if I already have the person...
firstName - John
lastName - Smith
personID - 1
And I download the JSON...
[
{
firstName: John,
lastName: Doe,
id: 1
},
{
firstName: Michael,
lastName: Caine,
id: 2
},
{
firstName: Angelina,
lastName: Jolie,
id: 3
}
]
I'd want the import to add Michael
and Angelina
and to update John Smith
to John Doe
because it already exists with the same id vs personID.
I've set the mappedKeyName between id and personID.
Is it possible to do this? I've been reading this blog... http://www.cimgf.com/2012/05/29/importing-data-made-easy/ about automatic mapping of key names etc... and wondered if I could exploit this to do what I want?
I have read the part about relatedByAttribute from the blog but I'm not sure where I should be setting it and what to?
Should I set it on the entity Person? Or the attribute personID? And what should I set it to? "id"? Or something else?
回答1:
I'm not sure if you're using the relatedByAttribute setting in the userInfo area of your Core Data model. You don't mention that in your issue here. I suggest you double check that. And to clarify, it's called relatedByAttribute because the string you put into the value section is the CoreData attribute which relates the entities, no the json (or other object) data. So, check those two things and if there still a problem, please open a ticket on github, preferably with a unit test that helps describe the problem.
来源:https://stackoverflow.com/questions/15544129/magical-record-import-with-already-existing-data