I have followed this tutorial http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated
Ive now used to tutorial and created my own application for which I have two entities and got stuck as my entities have a one to many relationship.
Make (attributes: carMake) Model (attributes: carModel)
Every Make has many Models. (One to Many Relationship defined as (models)).
I have set the inverse aswell to "make".
I have two questions, firstly how would you go about filling the data in the JSON file, Something like the below? with cars being the data model name.
[{ "cars":{
"carMake": "BMW",
"models": [
{"carModel": "1 Series"}
{"carModel": "3 Series"}
{ "carModel": "4 Series"}
]
"carMake": "Audi",
"models": [
{"carModel": "A4"}
{"carModel": "A3"}
]
}
}]
secondly how would i convert this data through xcode into a sqlite database as in Rays tutorial he does not state how to do this for a one to many relationship.
Thank you in advance for anyone posting a reply.
Below is the code used to pre populate data from JSON file to SQLITE using to one relationship:
[Banks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
FailedBankInfo *failedBankInfo = [NSEntityDescription
insertNewObjectForEntityForName:@"FailedBankInfo"
inManagedObjectContext:context];
failedBankInfo.name = [obj objectForKey:@"name"];
failedBankInfo.city = [obj objectForKey:@"city"];
failedBankInfo.state = [obj objectForKey:@"state"];
FailedBankDetails *failedBankDetails = [NSEntityDescription
insertNewObjectForEntityForName:@"FailedBankDetails"
inManagedObjectContext:context];
failedBankDetails.closeDate = [NSDate dateWithString:[obj objectForKey:@"closeDate"]];
failedBankDetails.updateDate = [NSDate date];
failedBankDetails.zip = [obj objectForKey:@"zip"];
failedBankDetails.info = failedBankInfo;
failedBankInfo.details = failedBankDetails;
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}];