简体   繁体   English

删除实体关系Coredata

[英]Delete entity relationship Coredata

Using CoreData, I have a entity called Wishlist with a many to many relationship to entity call WishlistProduct. 使用CoreData,我有一个名为Wishlist的实体,该实体与WishlistProduct实体具有多对多关系。 (the relationship is called 'wishlists'). (这种关系称为“愿望清单”)。

product.wishlists = nil removes the relationship between Wishlist and WishlistProduct. product.wishlists = nil删除Wishlist和WishlistProduct之间的关系。 WishlistListProduct is referenced in multiple Wishlist and product.wishlists = nil removes all references, how can i remove the reference to a specific Wishlist WishlistListProduct在多个Wishlist和product.wishlists = nil中被引用,删除所有引用,我如何删除对特定Wishlist的引用

        WishlistProduct *product;

        NSManagedObjectContext *context = [self managedObjectContext];
        product.wishlists = nil;   //  Removes all relationships

         [context save:nil];

i thought something like currentWishlist.product.wishlists = nil, any help would be great 我以为像currentWishlist.product.wishlists = nil之类的东西,任何帮助都会很棒

Because you have a many-to-many relationship, you cannot refer to a specific product with wishList.product because you have more than one product per list. 由于您具有多对多关系,因此无法通过wishList.product引用特定产品,因为每个列表中有多个产品。

You first have to filter/fetch the product in question and then you can set it's wish lists to nil . 您首先必须过滤/获取有问题的产品,然后可以将其愿望清单设置为nil

Setting the to-many relationship (which is of type NSSet ) of a particular Product to nil might do the trick, but you should test it. 将特定Product的多对多关系(类型为NSSet )设置为nil可以解决问题,但是您应该对其进行测试。 You should check if all the reverse relationships are also updated as expected. 您应该检查所有反向关系是否也按预期更新。 If that is not the case, loop through the lists and remove the product from the products relationship of each. 如果不是这种情况,请遍历列表并将产品从每个产品的products关系中删除。

Actually, Xcode creates the proper accessors to add and remove to-many objects for you when you generate the NSManagedObject subclass. 实际上,当您生成NSManagedObject子类时,Xcode会创建适当的访问器来为您添加和删除多个对象。 They look something like this 他们看起来像这样

- (void)addListsObject:(List *)value;
- (void)removeListsObject:(List *)value;
- (void)addLists:(NSSet *)values;
- (void)removeLists:(NSSet *)values;

You have to get all whishlists of the product and remove the whishlist. 您必须获取产品的所有心愿单,并删除心愿单。

NSMutableSet *set = [product mutableSetValueForKey:@"wishlists"];
[set removeObject:wishlist];

or the other way around. 或相反。

If you generated a subclass of NSManagedObject, you can use Core Data Generated Accessors. 如果生成了NSManagedObject的子类,则可以使用Core Data Generated Accessors。

[product removeWishlistsObject:wishlist];

or the other way around. 或相反。

I'm still not sure what you are trying to accomplish, but here are examples for the four possibilities I can think of. 我仍然不确定您要达到的目标,但以下是我能想到的四种可能性的示例。

- (void)removeWishlist:(NSManagedObject*)wishlist
           fromProduct:(NSManagedObject*)product {
    [[product mutableToManyRelationshipForKey:@"wishlists"] removeObject:wishlist];
}

- (void)removeProduct:(NSManagedObject*)product
         fromWishlist:(NSManagedObject*)wishlist {
    [[wishlist mutableToManyRelationshipForKey:@"products"] removeObject:product];
}

- (void)removeAllProductsFromWishlist:(NSManagedObject*)wishlist {
    [wishlist setValue:nil forKey:@"products"];
}

- (void)removeAllWishlistsFromProduct:(NSManagedObject*)product {
    [product setValue:nil forKey:@"wishlists"];
}

Where mutableToManyRelationshipForKey: is a category method on NSManagedObject which returns the appropriate mutable collection based on whether the relationship is ordered or not. 其中mutableToManyRelationshipForKey:NSManagedObject上的类别方法,它根据关系是否有序返回适当的可变集合。

- (id)mutableToManyRelationshipForKey:(NSString*)key {
    NSRelationshipDescription *relationship =
        [[self.entity relationshipsByName] objectForKey:key];
    return relationship.isOrdered
        ? [self mutableOrderedSetValueForKey:key]
        : [self mutableSetValueForKey:key];
}

Of course, all of this is a bit easier if you use the code generated by Xcode or something like mogenerator. 当然,如果您使用Xcode生成的代码或诸如mogenerator之类的代码,那么所有这些操作都将变得容易一些。

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

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