1
votes

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]);
    }
}];
1
Is this still actual to you? If yes, can you please update your question with the code you use for to-one relationships (I guess, it is the part of a whole job, you do understand how to deal with from tutorial) so the possible respondents could understand your concrete context. - Stanislav Pankevich
yes still stuck with this issue. I shall post the code used in the tutorial for a one to one. - user2512523
Before I go with detailed answer, could you please also post: what field of Make is responsible for storing value "BMW"? What field of Model is responsible for storing values like "1 Series"? - Stanislav Pankevich
If you would like to get an answer to your question as fast as possible without writing a whole bunch of comments feel free to contact me in skype (see my profile). I am ready to assist you, if you are not afraid of contacting "one of those russian guys". This comment is just for you. I will delete it in an hour or so, not to make an offtopic noise here in this topic. - Stanislav Pankevich
BMW is stored in attribute "carMake" of type "string" in Entity "Make". Similarly 1 Series is stored in attribute "carModel" of type "string" in Entity "Model". - user2512523

1 Answers

0
votes

This how I do this:

 NSError* err = nil;
 NSArray *jsonArray = [[NSArray alloc] init];
 NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"Words" ofType:@"json"];
 jsonArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
                                              options:kNilOptions
                                                error:&err];
NSManagedObjectContext *context = [self managedObjectContext];

[jsonArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        WordEntity *word = [NSEntityDescription
                            insertNewObjectForEntityForName:@"WordEntity"
                            inManagedObjectContext:context];

word.greekText = [obj objectForKey:@"greektext"];
word.englishText = [obj objectForKey:@"englishtext"];
word.uid = [NSString stringWithFormat:@"%@", [obj objectForKey:@"uid"]];
word.category = [obj valueForKey:@"category"];


        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
    }];
}