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.
I am guessing (sorry if i am wrong) that you didn't generate classes to your core data entities. If not -
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
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:
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With