Question: UIActivityIndicator stopping animating before NSFetchedResultsController finishes fetch


UIActivityIndicator stopping animating before NSFetchedResultsController finishes fetch

Answers 1
Added at 2017-01-04 19:01

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?

nr: #1 dodano: 2017-01-04 19:01

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.

Source Show
◀ Wstecz