UIActivityIndicator stopping animating before NSFetchedResultsController finishes fetch

60 views Asked by At

I have a core data request being made through a NSFetchedResultsController. The expected outcome of this code is that a UIActivityIndicator will startAnimating the call will be made, complete, and then return the table, before the indicator will `stopAnimating.

What actually happens is that the function returns before the NSFetchedResultsController gets any information and/or repopulates the TableView.stopAnimating is called almost instantly, and then only later does the table load.

My code is as below:

[_loadingActivityIndicator startAnimating];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"user"];
[fetchRequest setReturnsObjectsAsFaults:NO];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"connection.userId matches %@", [NSString stringWithFormat:@"%@",currentUser.userId ]];
fetchRequest.predicate = predicate;

fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES]];

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

self.fetchedResultsController.delegate = self;

// Perform Fetch
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];

if (error) {
    NSLog(@"Unable to perform fetch.");
    NSLog(@"%@, %@", error, error.localizedDescription);
}

[_loadingActivityIndicator stopAnimating];

What am I doing wrong?

1

There are 1 answers

0
Piepants On BEST ANSWER

You are setting a delegate which is what is called when the fetch results have finished. You need to move the stopAnimating call to that delegate.