如何插入与另一个相关的核心数据记录而不重复

发布于 2024-11-05 06:44:26 字数 1512 浏览 0 评论 0原文

我有两个核心数据实体:ArticlesFavoriteArticlesFavorite 具有一对多关系。首先,我成功插入了所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的夜不怕黑 2024-11-12 06:44:26

首先,确保 ArticleFavorite 之间存在互惠关系,即双向关系。像这样:

Article{
  favorites<-->>Favorite.article
}

Favorite{
  article<<-->Article.favorites
}

在核心数据中定义相互关系意味着从一侧设置关系会自动为另一侧设置该关系。

因此,要为新创建的 Article 对象设置新的 Favorite 对象,您只需:

Favorite *fav =[NSEntityDescription insertNewObjectForEntityForName:@"favorite" inManagedObjectContext:context];
Articles *article = [NSEntityDescription insertNewObjectForEntityForName:@"Articles" inManagedObjectContext:context];
[article.addFavoriteObject:fav];
//... or if you don't use custom NSManagedObject subclasses
[[article mutableSetValueForKey:@"favorites"] addObject:fav];

如果 Article 对象或 Favorite 对象已经存在,您将首先获取该对象,但设置关系的工作方式完全相同。

关键是要确保具有相互关系,以便托管对象上下文知道在两个对象中设置关系。

First, make sure you have a reciprocal i.e. two way relationship between Article and Favorite. Something like this:

Article{
  favorites<-->>Favorite.article
}

Favorite{
  article<<-->Article.favorites
}

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 created Article object you would just:

Favorite *fav =[NSEntityDescription insertNewObjectForEntityForName:@"favorite" inManagedObjectContext:context];
Articles *article = [NSEntityDescription insertNewObjectForEntityForName:@"Articles" inManagedObjectContext:context];
[article.addFavoriteObject:fav];
//... or if you don't use custom NSManagedObject subclasses
[[article mutableSetValueForKey:@"favorites"] addObject:fav];

If either the Article object or the Favorite 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.

昔日梦未散 2024-11-12 06:44:26

我通过创建新的 Article 对象解决了这个问题:

Articles *NewObj = [fetchedObjects objectAtIndex:0];

并用它来插入关系:

    [Fav setFavArticles:NewObj];
     [NewObj setArticlesFav:Fav];

非常感谢 TechZen..

I solved it by creating new Article object:

Articles *NewObj = [fetchedObjects objectAtIndex:0];

and use it to insert relationship :

    [Fav setFavArticles:NewObj];
     [NewObj setArticlesFav:Fav];

thanks very much TechZen..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文