我有一个从数组中获取数据的 UITableView。然而,填充该数组需要从 Web 下载和解析大量数据。既然如此,我想在后台线程中执行这些操作。这是我到目前为止所得到的:
@interface MyClass()
@property (nonatomic, strong) NSArray *model;
@end
@implementation MyClass
- (void) getData {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:SOME_URL]];
if (data) {
NSMutableArray *arr = [NSMutableArray array];
//Populate arr with data just fetched, which can take a while
dispatch_async(dispatch_get_main_queue(), ^{
//THIS IS THE STEP I AM UNSURE ABOUT. SHOULD I DO:
self.model = arr;
//OR
self.model = [NSArray arrayWithArray:arr];
//OR
self.model = [arr copy];
//OR
//something else?
});
}
});
}
@end
谢谢!