从一对多关系中删除对象 [英] Delete an object from one-to-many relationship

查看:143
本文介绍了从一对多关系中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个实体:




  • 通知

  • 用户



c $ c>有一个属性 username



有一对多的关系 用户< < - > 通知 / p>

有一个通知对象(ObjectA)保存有(2) code>保存在用户关系中的对象。我想通过删除用户关系中的 User 对象来更新ObjectA。



User 实体具有称为username的属性。



有(2) User' s,用户名为UserA UserB作为用户关系中的对象,我将如何删除UserA?



这是我想出的,它不工作:

  NSFetchRequest * notificationRequest = [[NSFetchRequest alloc] initWithEntityName:@Notification]; 
NSPredicate * predicate = [NSPredicate predicateWithFormat:@notification_id ==%@,[selectedManagedObject valueForKey:@notification_id]];
[self.managedObjectContext executeFetchRequest:notificationRequest onSuccess:^(NSArray * results)
{
//因为我基于objectID获取,应该总是有一个对象。
Notification * notificationObject = [results objectAtIndex:0];
NSArray * usersArray = [NSArray alloc] init];

//我不认为下面的代码是正确的?

usersArray = [notificationObject valueForKey:@users];

(userArray中的user *用户)
{
if(user.username == @UserA)
{
[self.managedObjectContext deleteObject:user];
[self.managedObjectContext saveOnSuccess:^ {
} onFailure:^(NSError * error){
}];

} onFailure:^(NSError * error){

}];

EDIT





最好的方法从关系中删除UserA对象?

解决方案

如果删除User对象也会删除相关的Notification对象
,那么您可能已设置从用户到通知到级联的关系的删除规则。



请注意,字符串比较

 

code> if(user.username == @UserA)

应为

  if([user.username isEqualToString:@UserA)

但这并不能解释为什么通知对象被删除。


Two Entities:

  • Notification
  • User

User has an attribute called username

There is one-to-many relationship between User <<---> Notification called "users"

There is a Notification object (ObjectA) saved that has (2) UserObjects saved in the "users" relationship. I would like to update ObjectA by deleting one of the User object in "users" relationship.

User entity has an attribute called "username".

There are (2) User's with username "UserA" & "UserB" as objects in the "users" relationship, how would I delete "UserA"?

Here is what I came up with and it's not working:

NSFetchRequest *notificationRequest = [[NSFetchRequest alloc] initWithEntityName:@"Notification"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notification_id == %@", [selectedManagedObject valueForKey:@"notification_id"]];
[self.managedObjectContext executeFetchRequest:notificationRequest onSuccess:^(NSArray *results) 
{
   //Since I'm fetching based on objectID, there should always be one Object.
    Notification *notificationObject = [results objectAtIndex:0];
    NSArray *usersArray = [NSArray alloc]init];

             //I don't think the code below is correct?  

    usersArray =  [notificationObject valueForKey:@"users"];

    for (User *user in userArray)
    {
        if (user.username == @"UserA")
        {
             [self.managedObjectContext deleteObject:user];
             [self.managedObjectContext saveOnSuccess:^{
         } onFailure:^(NSError *error) {
          }];

     } onFailure:^(NSError *error) {

     }];

EDIT

What's the best way to delete "UserA" object from the relationship?

解决方案

If deleting a "User" object also deletes the related "Notification" object, then you probably have set the "Delete Rule" for the relationship from "User" to "Notification" to "Cascade". You should set it to "Nullify" instead.

Note also that the string comparison

if (user.username == @"UserA")

is wrong, it should be

if ([user.username isEqualToString:@"UserA")

That does however not explain why the "Notification" object is deleted.

这篇关于从一对多关系中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆