我有一个非常奇怪的问题,涉及将 NSMangedObject 添加到一对多关系中,即使我尝试将多个对象添加到关系中,也只会添加一个对象。我必须进行以下设置:
当我将产品对象添加到 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;
}
在显示所有产品的视图控制器中,然后将购物车对象传递给我的购物车视图控制器,如下所示:
-(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;
}
但是由于某种原因,只有一个对象(产品)被添加到购物车中,即使我尝试添加多个对象,我认为我的 add addProductToCartWithProduct: 方法有问题,当我尝试将对象添加到关系中时。我做错了什么,我该如何解决这个问题,所以我可以将多个对象添加到关系中?