UIActivityIndi​​cator 在 NSFetchedResultsController 完成获取之前停止动画

UIActivityIndicator stopping animating before NSFetchedResultsController finishes fetch

我有一个通过 NSFetchedResultsController 发出的核心数据请求。此代码的预期结果是 UIActivityIndicatorstartAnimating 调用完成,然后 return table,然后指示器将`stopAnimating。

实际发生的是 NSFetchedResultsController 获取任何信息 and/or 之前的函数 return 重新填充 TableView。stopAnimating 几乎立即被调用,然后才稍后 table加载.

我的代码如下:

[_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];

我做错了什么?

您正在设置一个委托,它是在获取结果完成时调用的委托。 您需要将 stopAnimating 调用移至该委托。