gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:15:55 24 4
gpt4 key购买 nike

这是我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com