Note that there are some explanatory texts on larger screens.

plurals
  1. POFetch relationships and show in a UITableView
    primarykey
    data
    text
    <p>I have the following relationship set up in my Core Data model, in my shopping cart app. </p> <p><code>Menu</code> ->> <code>Product</code> &lt;&lt;- <code>Cart</code> (See picture below).</p> <p><img src="https://i.stack.imgur.com/lqcPS.png" alt="enter image description here"></p> <p>And a Objective-C category with the following code:</p> <pre><code>+ (Cart *)addProductToCartWithProduct:(Product *)product inManagedObjectContext:(NSManagedObjectContext *)context { Cart *cart = [NSEntityDescription insertNewObjectForEntityForName:@"Cart" inManagedObjectContext:context]; NSManagedObjectID *retID = [product objectID]; [(Product *)[context objectWithID:retID] setInCart:cart]; [cart addProductsObject:(Product *)[context objectWithID:retID]]; return cart; } </code></pre> <p>When the user pushes the "add to cart" button in the app, this method is fired (the method above). I now want to fetch these products (which is added to the cart by the user), in my other class, where the "cart" is located. I want these products to be shown in my <code>UITableView</code> in that class, I therefore use an FRC. <em>But I can't figure out how to fetch the relationship and thereby show the products. How would I manage to do this?</em></p> <p>I have the following code right now.</p> <pre><code>- (void)loadCart { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; fetchRequest.entity = [NSEntityDescription entityForName:@"Cart" inManagedObjectContext:_theManagedObjectContext]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES]; fetchRequest.sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [NSFetchedResultsController deleteCacheWithName:@"cartproducts"]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_theManagedObjectContext sectionNameKeyPath:nil cacheName:@"cartproducts"]; _fetchedResultsController.delegate = self; NSError *error = nil; if (![_fetchedResultsController performFetch:&amp;error]) { NSLog(@"Fetch Failed"); } } </code></pre> <p>My <code>UITableview</code> <code>cellForRowAtIndexPath:</code> method.</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *plainCellIdentifier = @"OrderCell"; ProductsCell *cell = (ProductsCell *)[aTableView dequeueReusableCellWithIdentifier:plainCellIdentifier]; if (!cell) { cell = [[ProductsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:plainCellIdentifier]; } Cart *ret = (Cart *)[self.fetchedResultsController objectAtIndexPath:indexPath]; // How to show the product names using the relationship? cell.title = ret.products ??; [cell setNeedsDisplay]; return cell; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload