Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData - how to add multiple objects in a to-many relationship

Ok so this question is two fold.

I am creating a bill reminder app and want to use CoreData to store all of the data (which I am new to). I have setup all of my entities and relationships (BillAccount one-to-many relationship with Bills). So an account can have many bills.

Question 1: So someone enters the account details and sets how many times the bill will repeat and taps save. How do I create the BillAccount object, then loop through and add all of the Bills for that BillAccount just added? I can easily add one bill and bill account but not sure how to add multiple bills to the one BillAccount.

Question 2: How can I add an additional Bill to an existing BillAccount after I have already created the BillAccount... so editing the bill not adding for the first time? Do have to firstly set the BillAccount object and get its uniqueID. I am a bit confused by this.

Some basic code examples would be great. Thanks for your help.

like image 348
camslaz Avatar asked Dec 21 '22 05:12

camslaz


1 Answers

I am guessing (sorry if i am wrong) that you didn't generate classes to your core data entities. If not -

  1. Get inside you model in Xcode
  2. In the core data editor, select all your entities.
  3. In Xcode menu go to Editor --> create managedObject SubClass
  4. Set the location and save.

Now go to your project file system and locate the BillAccount entity class. you will find in the .h file that Xcode generated "CoreDataGeneratedAccessors" methods for you:

- (void)addBillsObject:(Bills *)value;
- (void)removeBillsObject:(Bills *)value;
- (void)addBills:(NSSet *)values;
- (void)removeBills:(NSSet *)values;

Now to your first question

  1. Get all the bills objects you wish to add to the account
  2. Create a NSSet and pass all the bills to it
  3. Add the set to the bill account

      NSSet * billsForAccount = [NSSet setWithArray:allTheBills];
      [billAccount addBills:billsForAccount];
    

And that's it for adding many bills to one account.

As for your second question:

  1. The usual scenario I found is that you present to the user all the accounts, each account is stored in a tableView row (You can use NSFetchResultController for that but It will be impossible to get in to it now).
  2. As for now you can store all of the accounts in an Array when you set the accounts tableView.
  3. When the user selects a row, store the selected account by getting the indexPath.row from the tableView and getting the appropriate account from the Array.
  4. Now use this object to add the bills until the user selects different account.

    Array *allAccounts = [BillAccount allObjects];//will get all of the accounts
    //in the table view methods - use this array to set the tableView rows
    
      //in the userDidSelectRowForIndexPath use
    BillAccount *selectedAccount = [allAccounts objectAtIndex:indexPath.row];
    
    //now use this for adding the bills. (you might want to pass the selected account to other viewController or any other way appropriate to your App structure.
    //when you want to add new bill use
    [selectedAccount addBillsObject:billObject];
    
like image 50
shannoga Avatar answered Mar 02 '23 00:03

shannoga