CoreData - how to add multiple objects to the "to-many" relationship

So this question is doubled.

I am creating a banknote reminder application and want to use CoreData to store all the data (for which I am new). I have set up all my entities and relationships (BillAccount one-to-many relationships with bills). Thus, an account can have many accounts.

Question 1: Thus, someone enters the account information and sets how many times the bill and short-term storage will be repeated. How do I create a BillAccount object, then loop through and add all the accounts that BillAccount just added? I can easily add one account and an account, but I'm not sure how to add multiple accounts to one BillAccount.

Question 2: How to add an additional Bill to an existing BillAccount after I have already created BillAccount ... so that account editing will not be added for the first time? You must first set up a BillAccount object and get its unique identifier. I am a little confused by this.

Some basic code examples would be great. Thank you for your help.

+4
source share
1 answer

I assume (sorry if I'm wrong) that you did not generate classes for your main data objects. If not -

  • Get inside your model in Xcode
  • In the main data editor, select all of your objects.
  • In the Xcode menu go to Editor -> create a SubClass managed object
  • Set the location and save.

Now go to your project file system and find the BillAccount entity class. you will find in the .h file that Xcode generated the "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

  • Get all account objects that you want to add to your account
  • Create an NSSet and transfer all accounts to it
  • Add Set to Account Account

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

And this is for adding multiple accounts to one account.

Regarding your second question:

  • The usual scenario that I found is that you present all the accounts to the user, each account is stored in the tableView row (you can use NSFetchResultController for this, but now it will not be possible).
  • Now you can store all accounts in an array when you set up a tableView account.
  • When the user selects a row, save the selected account by getting indexPath.row from tableView and getting the corresponding account from the array.
  • Now use this object to add accounts until the user selects another 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]; 
+10
source

Source: https://habr.com/ru/post/1385037/


All Articles