如何插入与另一个相关的核心数据记录而不重复
我有两个核心数据实体:Articles
和 Favorite
。 Articles
与 Favorite
具有一对多关系。首先,我成功插入了所有 Articles
对象。
现在,我尝试将 ArticleID 插入“Favorite”实体中,但我不能。记录要么插入空关系,要么插入“文章”实体中的新记录。
我认为我应该首先获取 Articles
实体中的相关记录,然后使用它插入 Favorite
但我不知道如何执行此操作。我当前的代码:
NSManagedObjectContext *context =[appDelegate managedObjectContext] ;
favorite *Fav =[NSEntityDescription insertNewObjectForEntityForName:@"favorite" inManagedObjectContext:context];
Articles * Article = [NSEntityDescription insertNewObjectForEntityForName:@"Articles" inManagedObjectContext:context];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Articles" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *secondpredicate = [NSPredicate predicateWithFormat:@"qid = %@",appDelegate.GlobalQID ];
NSPredicate *thirdpredicate = [NSPredicate predicateWithFormat:@"LangID=%@",appDelegate.LangID];
NSPredicate *comboPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: secondpredicate,thirdpredicate, nil]];
[fetchRequest setPredicate:comboPredicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
// ?????????????????????????
}
}
任何建议将不胜感激。
I have two core data entities, Articles
and Favorite
. Articles
has To-Many relationship to Favorite
. First, I inserted all Articles
object successfully.
Now, I'm trying to insert ArticleID in “Favorite” entity but I cant. Either the record is inserted with an empty relationship or it is inserted with new record in “Articles” entity.
I think that I should be getting the related record in Articles
entity first and then using it to insert in Favorite
but I'm not sure how to do this. My current code:
NSManagedObjectContext *context =[appDelegate managedObjectContext] ;
favorite *Fav =[NSEntityDescription insertNewObjectForEntityForName:@"favorite" inManagedObjectContext:context];
Articles * Article = [NSEntityDescription insertNewObjectForEntityForName:@"Articles" inManagedObjectContext:context];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Articles" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *secondpredicate = [NSPredicate predicateWithFormat:@"qid = %@",appDelegate.GlobalQID ];
NSPredicate *thirdpredicate = [NSPredicate predicateWithFormat:@"LangID=%@",appDelegate.LangID];
NSPredicate *comboPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: secondpredicate,thirdpredicate, nil]];
[fetchRequest setPredicate:comboPredicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
// ?????????????????????????
}
}
Any suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,确保
Article
和Favorite
之间存在互惠关系,即双向关系。像这样:在核心数据中定义相互关系意味着从一侧设置关系会自动为另一侧设置该关系。
因此,要为新创建的
Article
对象设置新的Favorite
对象,您只需:如果
Article
对象或Favorite
对象已经存在,您将首先获取该对象,但设置关系的工作方式完全相同。关键是要确保具有相互关系,以便托管对象上下文知道在两个对象中设置关系。
First, make sure you have a reciprocal i.e. two way relationship between
Article
andFavorite
. Something like this:Defining reciprocal relationships in Core Data means that setting the relationship from one side automatically sets it for the other.
So, to set a new
Favorite
object for a newly createdArticle
object you would just:If either the
Article
object or theFavorite
object already exist, you would fetch the object first but setting the relationship would work exactly the same way.The key is to make sure you have the reciprocal relationship so that the managed object context knows to set the relationship in both objects.
我通过创建新的 Article 对象解决了这个问题:
并用它来插入关系:
非常感谢 TechZen..
I solved it by creating new Article object:
and use it to insert relationship :
thanks very much TechZen..