删除实体关系Coredata
Delete entity relationship Coredata
使用 CoreData,我有一个名为 Wishlist 的实体,它与名为 WishlistProduct 的实体存在多对多关系。 (关系被称为'wishlists')。
product.wishlists = nil 删除 Wishlist 和 WishlistProduct 之间的关系。 WishlistListProduct 在多个 Wishlist 中被引用并且 product.wishlists = nil 删除所有引用,我如何删除对特定 Wishlist
的引用
WishlistProduct *product;
NSManagedObjectContext *context = [self managedObjectContext];
product.wishlists = nil; // Removes all relationships
[context save:nil];
我想像 currentWishlist 这样的东西。product.wishlists = 没有,任何帮助都会很棒
因为您有多对多关系,所以您不能使用 wishList.product
来引用特定产品,因为每个列表中有 多个 产品。
您首先必须 filter/fetch 有问题的产品,然后您可以将其愿望清单设置为 nil
。
将特定 Product
的对多关系(属于 NSSet
类型)设置为 nil
可能会成功,但您应该对其进行测试。您应该检查所有反向关系是否也按预期更新。如果不是这种情况,循环遍历列表并从每个列表的 products
关系中删除产品。
实际上,当您生成 NSManagedObject
子类时,Xcode 会创建适当的访问器来为您添加和删除对多对象。它们看起来像这样
- (void)addListsObject:(List *)value;
- (void)removeListsObject:(List *)value;
- (void)addLists:(NSSet *)values;
- (void)removeLists:(NSSet *)values;
您必须获取产品的所有 whishlist 并删除 whishlist。
NSMutableSet *set = [product mutableSetValueForKey:@"wishlists"];
[set removeObject:wishlist];
或反过来。
如果您生成了 NSManagedObject 的子类,则可以使用核心数据生成的访问器。
[product removeWishlistsObject:wishlist];
或反过来。
我仍然不确定你想要完成什么,但这里是我能想到的四种可能性的例子。
- (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"];
}
其中 mutableToManyRelationshipForKey:
是 NSManagedObject
上的类别方法,returns 是基于关系是否有序的适当可变集合。
- (id)mutableToManyRelationshipForKey:(NSString*)key {
NSRelationshipDescription *relationship =
[[self.entity relationshipsByName] objectForKey:key];
return relationship.isOrdered
? [self mutableOrderedSetValueForKey:key]
: [self mutableSetValueForKey:key];
}
当然,如果您使用 Xcode 或类似 mogenerator 生成的代码,所有这些都会更容易一些。
使用 CoreData,我有一个名为 Wishlist 的实体,它与名为 WishlistProduct 的实体存在多对多关系。 (关系被称为'wishlists')。
product.wishlists = nil 删除 Wishlist 和 WishlistProduct 之间的关系。 WishlistListProduct 在多个 Wishlist 中被引用并且 product.wishlists = nil 删除所有引用,我如何删除对特定 Wishlist
的引用 WishlistProduct *product;
NSManagedObjectContext *context = [self managedObjectContext];
product.wishlists = nil; // Removes all relationships
[context save:nil];
我想像 currentWishlist 这样的东西。product.wishlists = 没有,任何帮助都会很棒
因为您有多对多关系,所以您不能使用 wishList.product
来引用特定产品,因为每个列表中有 多个 产品。
您首先必须 filter/fetch 有问题的产品,然后您可以将其愿望清单设置为 nil
。
将特定 Product
的对多关系(属于 NSSet
类型)设置为 nil
可能会成功,但您应该对其进行测试。您应该检查所有反向关系是否也按预期更新。如果不是这种情况,循环遍历列表并从每个列表的 products
关系中删除产品。
实际上,当您生成 NSManagedObject
子类时,Xcode 会创建适当的访问器来为您添加和删除对多对象。它们看起来像这样
- (void)addListsObject:(List *)value;
- (void)removeListsObject:(List *)value;
- (void)addLists:(NSSet *)values;
- (void)removeLists:(NSSet *)values;
您必须获取产品的所有 whishlist 并删除 whishlist。
NSMutableSet *set = [product mutableSetValueForKey:@"wishlists"];
[set removeObject:wishlist];
或反过来。
如果您生成了 NSManagedObject 的子类,则可以使用核心数据生成的访问器。
[product removeWishlistsObject:wishlist];
或反过来。
我仍然不确定你想要完成什么,但这里是我能想到的四种可能性的例子。
- (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"];
}
其中 mutableToManyRelationshipForKey:
是 NSManagedObject
上的类别方法,returns 是基于关系是否有序的适当可变集合。
- (id)mutableToManyRelationshipForKey:(NSString*)key {
NSRelationshipDescription *relationship =
[[self.entity relationshipsByName] objectForKey:key];
return relationship.isOrdered
? [self mutableOrderedSetValueForKey:key]
: [self mutableSetValueForKey:key];
}
当然,如果您使用 Xcode 或类似 mogenerator 生成的代码,所有这些都会更容易一些。