简体   繁体   中英

Delete entity relationship Coredata

Using CoreData, I have a entity called Wishlist with a many to many relationship to entity call WishlistProduct. (the relationship is called 'wishlists').

product.wishlists = nil removes the relationship between Wishlist and WishlistProduct. WishlistListProduct is referenced in multiple Wishlist and product.wishlists = nil removes all references, how can i remove the reference to a specific 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

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.

You first have to filter/fetch the product in question and then you can set it's wish lists to 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. 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.

Actually, Xcode creates the proper accessors to add and remove to-many objects for you when you generate the NSManagedObject subclass. 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.

[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.

- (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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