我在数据的 getter 方法中将数据异步加载到数组中。
首先它会产生一个空数组,所以我的表自然会加载 0 行。
数据加载完成后,我调用reloadData
更新我的表,但是,正在下载的数据和显示数据之间似乎存在约 6 秒的差距UITableView
。
有谁知道这可能发生的任何原因?
我使用dispatch_async
优先级高的方法。
我什至记录了加载数据和插入数据过程中的每个点,它显示了这一点。
此外,如果我在加载数据时不断上下滚动表格,表格会尽快显示其数据,而不是在插入之间存在这种间隙。
代码:
- (NSMutableDictionary *)tableDictionary {
if (!_tableDictionary) {
_tableDictionary = [NSMutableDictionary dictionary];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSString *URLString = @"http://www.urlToData.com/path/to/file.php?foo=bar";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLString]];
NSError *error = nil;
NSDictionary *objectIDs = [NSJSONSerialization JSONObjectWithData:data options:NSJSONWritingPrettyPrinted error:&error];
NSMutableDictionary *objects = [NSMutableDictionary dictionary];
for (NSInteger i = 0; i < objectIDs.allValues.count; i++) {
NSArray *eventIDs = (objectIDs.allValues)[i];
NSString *eventType = (objectIDs.allKeys)[i];
[objects setValue:[myObject initWithIDs:objectIDs] forKey:@"key"];
}
self.tableDictionary = objects;
self.titlesForSectionHeader = objects.allKeys;
NSLog(@"Done");
[self.tableView reloadData];
});
}
return _tableDictionary;
}