I am having big issues displaying records from my entity Product and OrderItems. This is one to Many relationships.
I added records to Product entity without any problem and I perform OrderItem entry in the following function:
- (IBAction)cmdSave:(id)sender { self.product.productName = self.txtProductName.text; NSManagedObjectContext *context =[self managedObjectContext]; OrderItems *oi= [NSEntityDescription insertNewObjectForEntityForName:@"OrderItems" inManagedObjectContext:context]; [oi setValue:[NSNumber numberWithInteger:[self.txtQty.text integerValue]] forKey:@"ordQty"]; [oi setValue:[NSNumber numberWithInteger:[self.txtPrice.text integerValue]] forKey:@"price"]; [oi setValue:[NSNumber numberWithInteger:[self.txtTotal.text integerValue]] forKey:@"total"]; [oi setOrders:self.product]; NSError *error = nil; if (![context save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [self.navigationController popViewControllerAnimated:YES]; }
This seems ok and my biggest issue is display all order items for each Product. The relationship Product ====> OrderItems is:product and the inverse is orders. OrderItems ===>Product is: orders and inverse is product.
I try to display for fetchedResult
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... Product *prod = [self.fetchedResultsController objectAtIndexPath:indexPath]; ********************* //I wanted to connect to relationship object and get all orders but I get Incompatible pointer types initializing 'OrderItems *_Strong ' with an expression of type NSSet. ******any ideas? OrderItems *order=prod.product; ************************************** cell.textLabel.text =prod.productName; cell.detailTextLabel.text =[NSString stringWithFormat:@"%@",order.price]; return cell; }
I know the relationship object in product in Product is NSSet as below:
@interface Product : NSManagedObject @property (nonatomic, retain) NSString * productName; @property (nonatomic, retain) NSNumber * qty; @property (nonatomic, retain) NSSet *product; @end @interface Product (CoreDataGeneratedAccessors) - (void)addProductObject:(NSManagedObject *)value; - (void)removeProductObject:(NSManagedObject *)value; - (void)addProduct:(NSSet *)values; - (void)removeProduct:(NSSet *)values; @end
I have done the same way in different program but I don't know really why.
Any help or advice would be much appreciated!