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

时间:2017-01-04 18:04:34

标签: ios objective-c core-data nsfetchedresultscontroller uiactivityindicatorview

我通过NSFetchedResultsController发出核心数据请求。此代码的预期结果是UIActivityIndicatorstartAnimating调用将完成,然后返回表,然后指示符将“停止动画”。

实际发生的是该函数在NSFetchedResultsController获取任何信息和/或重新填充TableView之前返回。stopAnimating几乎立即被调用,然后才加载表。

我的代码如下:

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

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