ios - 为什么不是我的所有代码都在我的 findObjectsInBackgroundWithBlock :^ being executed?

标签 ios objective-c parse-platform

这是我的 ViewDidLoad 中的代码

我正在尝试在我拥有未执行的 NSLog 的代码部分执行某些操作。我没能找到有同样问题的人?

我哪里错了?提前致谢!

PFRelation *relation = [staffMember relationForKey:@"completedTraining"];
PFQuery *query = [relation query];
[query includeKey:@"trainingRecordPointer"];
[query findObjectsInBackgroundWithBlock:^(NSArray *completedTrainingRecords, NSError *error){
    if(!error){
        for (PFObject* completedTrainingRecord in completedTrainingRecords) {
            PFObject * recordWtihTypeAndName = [completedTrainingRecord objectForKey:@"trainingRecordPointer"];
            PFObject *outputObject = [[PFObject alloc]initWithClassName:@"NewTrainingRecord"];
            NSString *recordName = [recordWtihTypeAndName valueForKey:@"RecordName"];
            [completeRecordsWithType addObject:recordName];
            [outputObject addObject:recordName forKey:@"recordName"];
            [outputObject addObject:completedTrainingRecord.createdAt forKey:@"date"];
            [[trainingRecordsDictionary objectForKey:[recordWtihTypeAndName objectForKey:@"Type"]] addObject:outputObject];
            [self.tableView reloadData]; //it works up to this point but if I move this line outside
            //the for-loop nothing happens
            NSLog(@"this will execute"); // does execute

        }
        NSLog(@"this wont execute"); // doesn't execute

    } else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    NSLog(@"this wont execute"); // doesn't execute

}];

最佳答案

您应该将 [self.tableView reloadData]; 移动到您的 for 循环之外。

您还应该确保在主线程中重新加载 TableView ,而不是在后台线程中。

可能是这样的:

[query findObjectsInBackgroundWithBlock:^(NSArray *completedTrainingRecords, NSError *error){
    if(!error){
        for (PFObject* completedTrainingRecord in completedTrainingRecords) {

                 ... do your stuff ...

        }
         __weak typeof(self) weakSelf = self;
            dispatch_async(dispatch_get_main_queue(), ^ {
                  [weakSelf.tableView reloadData];
        });
    }
}];

您可能会遇到麻烦,因为您试图在后台线程上修改您的 UI。

关于ios - 为什么不是我的所有代码都在我的 findObjectsInBackgroundWithBlock :^ being executed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997222/

相关文章:

ios - 运行时属性和实际属性之间的性能差异是什么?

objective-c - 更改文件夹的 Finder 侧边栏图标

iphone - 如何使用 UIKit/Quartz 在 iPhone 上的纹理上绘制灯光效果

swift - 无法从 Parse Server 加载数据,错误 =( 错误?) nil none

android - MultiAutoCompleteTextView 不显示结果

ios - 没有 "KeyboardName - HostAppName"方案的自定义键盘名称

ios - 缩放 CAShapeLayer 时的意外行为

android - PlayMarket 和 App Store 会记住应用程序购买吗?

javascript - 如何返回Parse Query结果进行查看?

ios - RestKit 0.2 错误 'Expected status code in (400-499), got 200'