Note that there are some explanatory texts on larger screens.

plurals
  1. PONSFetchedResultsController not showing all results
    primarykey
    data
    text
    <p>A bit of background info: I'm grabbing data from a Ruby on Rails website, parsing its json, and assigning objects to Core Data objects. This is working well, and the rows are importing fine, with the exception that it doesn't yet check for duplicates (I haven't yet implemented that). So every time I launch the app, I get an extra 3 objects added.</p> <p>I've setup a <code>NSFetchedResultsController</code> to fetch all objects of my model:</p> <pre><code>NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([self class])]; request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"remote_id" ascending:YES]]; return [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; </code></pre> <p>and In my table controller, I've added all the methods to pull data off from the FetchedResultsController into the table delegate methods.</p> <p>The problem itself is, when doing a <code>NSFetchRequest</code> I get back all the objects in the array (there are around 60 at the moment), but the table is only showing 3 results. The (coincidently..?) same 3 results that are on my rails server.</p> <p>I opened the database manually and edited a few of the 'duplicate' records to check whether it was doing some magical duplication checking and my manually edited records still aren't showing. At this point I'm lost. What could possibly be causing the results to filter down? The code shouldn't be aware of these models being duplicates in the first place. I just don't understand.</p> <p>Edit----</p> <p>More info:</p> <p>My TableViewController is a subclass of the CoreDataTableViewController provided here from Standford (accompanying lecture videos are available on iTunes U, and are fantastic, by the way): <a href="http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2011-fall" rel="nofollow">http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2011-fall</a> (ctrl+f for CoreDataTableViewController.zip)</p> <p>This deals with <code>performFetch</code> after assigning on the fetchedResultsController property setter and simply calls</p> <pre><code>NSError *error; [self.fetchedResultsController performFetch:&amp;error]; if (error) NSLog(@"[%@ %@] %@ (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]); //... [self.tableView reloadData]; </code></pre> <p>and as for section and row count (returns 1 and 3):</p> <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.fetchedResultsController sections] count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; } </code></pre> <p>and in <code>cellForRowAtIndexPath</code> I'm simply calling the following to retrieve each object</p> <pre><code>id myObject = [self.fetchedResultsController objectAtIndexPath:indexPath] </code></pre> <p>edit 2 ----- Okay, here's actual code rather than me picking parts out:</p> <p>In my <code>Registration+sync.m</code>:</p> <pre><code>+ (NSFetchRequest*)requestForAll { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([self class])]; request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"remote_id" ascending:YES]]; return request; } + (NSFetchedResultsController*)requestForAllResultsController { NSFetchRequest *request = [self requestForAll]; NSManagedObjectContext *context = [DataController instance].database.managedObjectContext; return [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; } + (NSArray*)allObjects { NSFetchRequest *request = [self requestForAll]; return [[DataController instance] fetch:request]; } </code></pre> <p>In DataController:</p> <pre><code>- (NSArray*)fetch:(NSFetchRequest*)request { NSManagedObjectContext *context = self.database.managedObjectContext; NSError *error = nil; return [context executeFetchRequest:request error:&amp;error]; } </code></pre> <p>In the TableViewController:</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSLog(@"rows: %d", [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]); NSLog(@"all objects: %d", [[Registration allObjects] count]); return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; } </code></pre> <p>prints out the following:</p> <pre><code>2012-10-09 15:22:04.211 MyApp[8279:c07] rows: 4 2012-10-09 15:22:04.214 MyApp[8279:c07] all objects: 68 </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    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