NSManagedObject不会添加到Core Data的一对多关系

时间:2013-11-08 20:29:30

标签: ios objective-c core-data one-to-many

我有一个非常奇怪的问题,即将NSMangedObject添加到一对多关系,其中只添加了一个对象,即使我尝试向关系添加多个对象。 我必须遵循以下设置:

enter image description here

当我将产品对象添加到ProductCart实体时,我使用以下代码:

+ (ProductCart *)addProductToCartWithProduct:(Product *)product inManagedObjectContext:(NSManagedObjectContext *)context {

      ProductCart *cartProduct = nil;

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ProductCart"];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", cartProduct.name];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    NSError *error = nil;
    NSArray *matches = [context executeFetchRequest:request error:&error];

    if (!matches || ([matches count] > 1)) {
        // handle error
    } else if ([matches count] == 0) {


        cartProduct = [NSEntityDescription insertNewObjectForEntityForName:@"ProductCart" inManagedObjectContext:context];

        [product addChosedProductsObject:cartProduct];


    } else {

        cartProduct = [matches lastObject];
    }




    return cartProduct; 



}

然后返回一个我可以添加到我的(一个)购物车的productCart对象。我是这样做的:

+ (Cart *)addProductToCartWithProduct:(ProductCart *)cartProduct inManagedObjectContext:(NSManagedObjectContext *)context;
{

    Cart *cart = nil;

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Cart"];

    NSError *error = nil;
    NSArray *carts = [context executeFetchRequest:request error:&error];

    if (!carts || ([carts count] > 1)) {


    } else if (![carts count]) {
        cart = [NSEntityDescription insertNewObjectForEntityForName:@"Cart" inManagedObjectContext:context];

    } else {
        cart = [carts lastObject];

    }

    if (cartProduct && cart) {

        [cart addProductsObject:cartProduct];

    }



    return cart;
}

在我的viewcontroller中显示所有产品然后将购物车对象传递给我的购物车Viewcontroller,如下所示:

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

    NSIndexPath *ip = [NSIndexPath indexPathForRow:sender.tag inSection:0];

    Product *product = (Product *)[self.fetchedResultsController objectAtIndexPath:ip];


    NSInteger amount = product.quantity.intValue;
    amount++;
    product.quantity = [NSNumber numberWithInt:amount];

    UITabBarController *tabBarController = self.tabBarController;

    for (UINavigationController *navController in  tabBarController.viewControllers) {


        for (UIViewController *vc in navController.viewControllers) {
            if ([vc isMemberOfClass:NSClassFromString(@"CartViewController")]){
                CartViewController *cVC = (CartViewController *) vc;

                ProductCart *prodCart = [ProductCart addProductToCartWithProduct:product inManagedObjectContext:self.theManagedObjectContext];


               Cart *cart =  [Cart addProductToCartWithProduct:prodCart inManagedObjectContext:self.theManagedObjectContext];

               cVC.cart = cart;


            }
        }

    }


    BOOL saved = [self saveCurrentContext:self.theManagedObjectContext];
    if (saved) {

        [self addToCartTapped:ip];

        [self.menuDetailTableView reloadData];
    }




}



-(BOOL)saveCurrentContext:(NSManagedObjectContext *)context  {


    BOOL contextSaved = NO;
    NSError *error;
    BOOL saved = [context save:&error];
    // [[DataManager sharedInstance] saveBackgroundContext];
    // [[DataManager sharedInstance] saveMasterContext];
    if (saved) {
        contextSaved = YES;


    }else {

        [self displayErrorAlert:error];


    }

    return contextSaved;



}

传递的购物车然后在另一个视图控制器(cartviewcontroller)中被提取,如下所示:

- (void)loadCart {

    [self.theManagedObjectContext reset];

    if (self.cart) {


        NSFetchRequest *fetchRequest;
        if (!fetchRequest) {
            fetchRequest = [[NSFetchRequest alloc] init];
            fetchRequest.entity = [NSEntityDescription entityForName:@"ProductCart" inManagedObjectContext:self.theManagedObjectContext];

            fetchRequest.predicate = [NSPredicate predicateWithFormat:@"inCart = %@", self.cart];


        }


        NSSortDescriptor *sortDescriptor;
        if (!sortDescriptor) {
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"products.name" ascending:YES];
            fetchRequest.sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        }

        if (!self.fetchedResultsController) {


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

        }



        NSError *error = nil;


        if (![self.fetchedResultsController performFetch:&error]) {
            [self displayErrorAlert:error];

        }

    }


        self.cartProducts = self.fetchedResultsController.fetchedObjects;
}

但由于某种原因,只有一个对象(产品)被添加到购物车中,即使很难我尝试添加多个对象,我认为添加addProductToCartWithProduct:方法时出现问题,当我尝试将对象添加到关系。 我做错了什么以及如何解决这个问题,所以我可以在关系中添加多个对象?

0 个答案:

没有答案