繁体   English   中英

核心数据一对一关系,保存无效

[英]Core-Data One-to-One Relationship, Save Not Working

我很想念我想抓的东西,并希望这里的人能帮上忙。

我正在使用Xcode 4.4版,而我的项目正在使用Core Data,Storyboards和ARC。

我的项目有以下3个实体。

在此处输入图片说明

一切都与Livestock和Notes实体配合良好。 但是,当我尝试将数据保存到分类实体时,什么也没有发生。 我没有收到任何错误,但是数据没有保存。

我可以不对获取的结果使用数组吗? 根据我的谓词,我期望只返回一个对象,所以我认为我不需要数组。 下面是我的代码,可以节省费用。 我已验证数据是从视图的文本变量传递过来的。

AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

fetchRequest.entity = [NSEntityDescription entityForName:@"Livestock" inManagedObjectContext:managedObjectContext];

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"tank == %@ AND type == %@ AND name == %@", self.detailTank, self.detailType, self.detailName];

NSError *error = nil;
Livestock *livestock = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] lastObject];
if (error) {
    NSLog(@"TaxonViewController: saveTaxonomy: Retrieving Livestock Record for saving: error = %@", error);
}

Taxonomy *taxonomy = livestock.taxonChildren;

taxonomy.kingdom = self.kingdom.text;
taxonomy.phylum  = self.phylum.text;
taxonomy.classs  = self.classs.text;
taxonomy.order   = self.order.text;
taxonomy.family  = self.family.text;
taxonomy.genus   = self.genus.text;
taxonomy.species = self.species.text;
taxonomy.common  = self.common.text;
taxonomy.livestockParent = livestock;

error = nil;
if (![managedObjectContext save:&error]) {
    NSLog(@"Taxonomy save error. error = %@, userInfo = %@", error, [error userInfo]);
}    

非常感谢任何见识! 谢谢!

更新1:

修改后的代码以测试NULL taxonChildren值。 这为我解决了。 谢谢杰西!

if (livestock.taxonChildren == NULL) {

    Taxonomy *taxonomy = [NSEntityDescription insertNewObjectForEntityForName:@"Taxonomy" inManagedObjectContext:managedObjectContext];

    taxonomy.kingdom = self.kingdom.text;
    taxonomy.phylum  = self.phylum.text;
    taxonomy.classs  = self.classs.text;
    taxonomy.order   = self.order.text;
    taxonomy.family  = self.family.text;
    taxonomy.genus   = self.genus.text;
    taxonomy.species = self.species.text;
    taxonomy.common  = self.common.text;
    taxonomy.livestockParent = livestock;
}
else {

    Taxonomy *taxonomy = livestock.taxonChildren;

    taxonomy.kingdom = self.kingdom.text;
    taxonomy.phylum  = self.phylum.text;
    taxonomy.classs  = self.classs.text;
    taxonomy.order   = self.order.text;
    taxonomy.family  = self.family.text;
    taxonomy.genus   = self.genus.text;
    taxonomy.species = self.species.text;
    taxonomy.common  = self.common.text;
    taxonomy.livestockParent = livestock;
}

您的livestock.taxonChildren是否为零? 您必须首先将该对象的实例插入您的上下文中。 即使是一对一的关系,它也不会自动创建。

请注意,您不应测试此类错误:

if (error) { ... }

因为当获取请求成功时, error可能是垃圾。 您应该改为测试返回值:

NSArray *results = [managedObjectContext executeFetchRequest...]
if (!results) { ... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM