gpt4 book ai didi

ios - 在后台线程中下载和解析数据

转载 作者:行者123 更新时间:2023-11-29 13:02:56 24 4
gpt4 key购买 nike

我有一个从数组中获取数据的 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

谢谢!

最佳答案

// you can use any string instead "mythread"
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

dispatch_async(backgroundQueue, ^{
// Send Request to server for Data
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:SOME_URL]];

dispatch_async(dispatch_get_main_queue(), ^{
// Receive Result here for your request and perform UI Updation Task Here
if ([data length] > 0) {
// if you receive any data in Response, Parse it either (XML or JSON) and reload tableview new data
}
});
});

关于ios - 在后台线程中下载和解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350917/

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