Iphone 核心数据-保存关系的技术?
我的数据模型具有以下关系:Iphone 核心数据-保存关系的技术?,iphone,objective-c,ios,core-data,relation,Iphone,Objective C,Ios,Core Data,Relation,我的数据模型具有以下关系: [Account] -|------o< [Transaction] 现在,我已经成功创建了一个帐户并将其插入核心数据。我的问题是,如何向帐户中添加起始余额?很明显,这只是帐户上的一个事务,但以下内容是否足以在数据模型中实现双向连接(即连接newAccount.transactions以及newTransaction.Account) // we need to insert a new account Account *newAccount = [NSEnt
[Account] -|------o< [Transaction]
现在,我已经成功创建了一个帐户并将其插入核心数据。我的问题是,如何向帐户中添加起始余额?很明显,这只是帐户上的一个事务,但以下内容是否足以在数据模型中实现双向连接(即连接newAccount.transactions
以及newTransaction.Account
)
// we need to insert a new account
Account *newAccount = [NSEntityDescription insertNewObjectForEntityForName:[Account entityName] inManagedObjectContext:self.managedObjectContext];
// . . . configure newAccount
NSNumber *startingBalance = @([self.startingBalanceTextField.text floatValue]);
NSError *error;
// save the new account
[self.managedObjectContext save:&error];
if( !error )
{
Transaction *newTransaction = [NSEntityDescription insertNewObjectForEntityForName:[Transaction entityName] inManagedObjectContext:self.managedObjectContext];
// . . . configure newTransaction
// is this sufficient & proper? Will this add newTransaction to newAccount.transactions as well?
newTransaction.account = newAccount;
// save the starting balance
[self.managedObjectContext save:&error];
}
是,如果
交易
和账户
定义为反向关系,则
newTransaction.account = newAccount;
自动将newTransaction
添加到newAccount.transactions
您可以使用po newAccount
在调试器中轻松验证这一点
newTransaction.account = newAccount;