多线程控制器可能变得复杂。这里有一些建议来帮助你。
self.api = [API new]; // 1a: Created in the main thread. [self.api requestWithCompletionHandler:^(NSArray<NSDictionary *> *result, NSError *error) { for (NSDictionary* x in result) { NSLog(@"The whole dict object: %@", [x objectForKey:@"datum"]); [self.arr addObject:x]; // 1b: Updated in the background. } [self reloadTable]; // 2: Calling a view controller method in the background. }];
我在这里看到两个警示标志。
简单的方法是立即调度到主线程。
self.api = [API new]; [self.api requestWithCompletionHandler:^(NSArray<NSDictionary *> *result, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ for (NSDictionary* x in result) { NSLog(@"The whole dict object: %@", [x objectForKey:@"datum"]); [self.arr addObject:x]; } [self reloadTable]; }); }];
处理主线程/后台线程交互还有很多其他方法,但我会从这开始。
你试过在主线程上调用它吗?
- (void) reloadTable { dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); }