Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect way to add relational data to Core Data
    primarykey
    data
    text
    <p>I've created two objects in my model, Account and Friend - where 1 Account will have many Friends. I have created these two objects in code also.</p> <p>I am using a UITableView to show my Accounts (all fine), and using a new UIViewController to add a new record. In the new record I am adding the Account details, and getting friends from an API. When going from the UITableView to the new UIViewController I am creating an empty Account object: UIViewController.account = account;</p> <p>Now the tricky part. When saving this data I am doing the following:</p> <pre><code>// Configure the new account with information from the form. [account setUsername:[profileDict objectForKey:@"the_name"]]; [account setPassword:password.text]; // from formfield [account setCreationDate:[NSDate date]]; // Commit the change. NSError *error; if (![account.managedObjectContext save:&amp;error]) { // Handle the error. NSLog(@"save error"); } NSManagedObjectContext *context = [account managedObjectContext]; for(NSArray *names in usernameArray) //usernameArray holds my Friends { friend = [NSEntityDescription insertNewObjectForEntityForName:@"Friend" inManagedObjectContext:context]; [account addReplyAccountsObject:friend]; [friend setName:[names objectAtIndex:0]]; [friend setPicUrl:[names objectAtIndex:1]]; // Commit the change. NSError *error; if (![context save:&amp;error]) { // Handle the error. NSLog(@"save error"); } } </code></pre> <p>Now this seems to work - however sometimes my app crashes with a bus error - usually on the device rather than the simulator. Is this the right way to save an Account and many Friends at once? Also - any reason why I would get a bus error? It seems to happen when there are many Friends....</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload