【问题标题】:1 to 1 relationship core data iOS1对1关系核心数据iOS
【发布时间】:2012-03-08 06:41:01
【问题描述】:

我有两个实体:登录(用户 ID、密码)和信息(标题、信息)。 现在它们之间是一对一的关系。 我需要在数据库中添加一些用户独有的信息。

我的代码如下:

    Login *information = [NSEntityDescription insertNewObjectForEntityForName:@"Login"
                                                      inManagedObjectContext:self.managedObjectContext];

    information.information.title = informationTitleTextView.text;
    information.information.info_1 = information1textview.text;
    information.information.info_2 = information2textview.text;

    [self.managedObjectContext save:nil];  // write to database

    [self.delegate savebuttontapped:self];

但这不起作用。我不知道,我做错了什么?任何帮助将不胜感激。

【问题讨论】:

  • 将 Login 实例命名为 information 确实令人困惑。此外,如果这段代码正在执行,那么很明显 something 正在发生,而不是 nothing,所以如果您解释了您期望发生的事情,它会有所帮助。如果您不忽略 -save: 可能返回的错误,这也会有所帮助——传递 NSError 指针的地址有多难?
  • 我希望将值分配给属性...但是当我在执行 NSLog 并检查它时显示为 NULL 并且没有任何内容写入数据库...
  • 请考虑使用钥匙串来存储身份验证详细信息。这就是它的设计目的。

标签: iphone ios core-data entity-relationship iphonecoredatarecipes


【解决方案1】:

您尚未将Information 的实例添加到上下文中。试试这个:

Login *login = [NSEntityDescription insertNewObjectForEntityForName:@"Login" inManagedObjectContext:self.managedObjectContext];
Information *information = [NSEntityDescription insertNewObjectForEntityForName:@"Information" inManagedObjectContext:self.managedObjectContext];

login.information = information;
login.information.title = informationTitleTextView.text;
//...and so on...

当然,如果您要根据属性获取登录对象,您需要在这些属性中实际存储一些内容:

login.userId = theUserId;
login.password = thePassword;

在将来的某个时候,您可能只想获取与您的条件匹配的 Login 对象。一旦你有了它,你就可以毫无困难地获得相关的信息对象:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Login"
    inManagedObjectContext:managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userId like %@ AND password like %@", theUserId, thePassword];
[request setPredicate:predicate];
NSError *err = nil;
NSArray *matchingLogins = [self.managedObjectContext executeFetchRequest:request error:&err];
int count = [matchingLogins count];
if (count != 1) {
    NSLog(@"Houston, we have a problem.");
}
Login *login = [matchingLogins objectAtIndex:0];
Information *info = login.information; // Notice: no separate fetch needed

【讨论】:

  • 谢谢...解决了我的问题。现在在同一行上,我想获取 information.title 并将其显示为该登录名所独有的。
  • // 配置单元格... 登录 *login;登录 = [self.fetchedResultsController objectAtIndexPath:indexPath];信息 *信息;信息 = [self.fetchedResultsController objectAtIndexPath:indexPath];登录信息=信息; cell.textLabel.text = login.information.title;返回单元格; }
  • @user1253637 您不需要单独获取 Information 对象——它已经与 Login 对象相关联,所以当您获得 Login 时,您也已经获得了 Information。请参阅我更新的答案以获取仅获取与您的用户 ID 和密码匹配的登录名的另一种方法。如果您尝试显示所有用户,则不是很有用,但它可能具有指导意义。
  • 属性 login.userId ,login.password 在用户添加帐户并存储在数据库中时具有值。我正在接受用户输入并比较用户是否输入正确,如果是,那么它应该能够显示仅与该用户相关的 information.title。最后一部分没有发生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多