簡體   English   中英

為什么我的findObjectsInBackgroundWithBlock:^中的所有代碼都沒有執行?

[英]Why isn't all of the code in my in my findObjectsInBackgroundWithBlock:^ being executed?

這是我ViewDidLoad中包含的代碼

我正在嘗試在代碼部分執行某些操作,其中我有未執行的NSLogs。 我找不到任何遇到相同問題的人嗎?

我要去哪里錯了? 提前致謝!

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]; 到您的循環之外。

您還應確保將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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM