Remove the object from the database relationship – NSSet

综合技术 2017-11-15

I have a one to many relationship between a Product entity and a Cart Entity.

The user needs to have the opportunity to delete a product from the cart.

I fetch the products like so:

// Fetch request for "Product":
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Product"];

// Fetch only products for the cart:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inCart = %@", self.cart];
[fetchRequest setPredicate:predicate]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"navn" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor]];

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_theManagedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;

and populate the tableview:

Product *prod = (Product *)[self.fetchedResultsController objectAtIndexPath:indexPath];

In the tableview I have a button and when you tap it, that current product, should get deleted... I have the following code in my "delete product method"

-(void)RemoveFromCart:(UIButton *)sender {

    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexPath = [_Table indexPathForCell:cell];

    Product *prod = (Product *)[self.fetchedResultsController objectAtIndexPath:indexPath];

    /*_cart is retrieved when the user pushes the add to cart button, in another    viewcontroller, a cart object is then returned and passed to the cart viewcontroller */
    NSMutableSet *mutableSet = [NSMutableSet setWithSet:_cart.products];

        [mutableSet removeObject:prod];
        _cart.products = mutableSet;

    [self saveCurrentContext:_theManagedObjectContext];

    [_Table reloadData];


But nothing happens, when the method is fired.. the product is not removed from the relationship. How do I manage to fix this, my code is obviously wrong somewhere.

To remove a product from the cart, you can either use

product.inCart = nil;


[_cart removeProductsObject:product];

(Both lines are equivalent if the inverse relationships are properly set up.)


Manage a database char value I save a char value in a table, under the type "char(10)". When I try to get the values of a row in the table, I do the following: DataTable table =...
Zero-Downtime Deployments with Data Migrations A few months ago I talked about how to deploy with zero-downtime . I can wait while you read it (TL;DR: Blue-Green deployment is the prevalent ...
steemd 源码分析2 chainbase chain 插件是 steemd 中最重要的插件之一,涉及到的功能也比较复杂,基本上大多数API都依赖于该插件。本篇 就主要来分析该插件。 chain 插件的源码主要位于 libraries/chain 和 libraries/chainbase 中。 chainbase ...
VLCKit 3.0 adds support for H.265, better 4K memor... Today’s VLCKit 3.0 update from the developer of the popular VLC Media Player brings a number of enhancements to the framework for apps on iOS, tvO...
iOS 最新审核被拒及解决方案 (包括2.1大礼包)... 临近年底手里的app都要上线,不知道大家有没有感觉这段时间的审核团队各种严,手里的3个项目全部被卡(3个不同账号),其中两个是迭代,一个是新上线的app。全部都遭到了毒手~~~(>_<)~~~ 下面我就我遇见的问题做一下总结,希望可以帮到正在困惑中的你。 Guideli...