There's this code of the test project in Objective-C:
@implementation ViewController {
NSArray *_locations;
}
- (void)viewDidLoad
{
[super viewDidLoad];
JSONLoader *jsonLoader = [[JSONLoader alloc] init];
NSURL *url = [[NSURL alloc]initWithString:@"http://mechnikova.info/api/pic2.php?task=1"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
_locations = [jsonLoader locationsFromJSONFile:url];
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
});
There's this code of the same test project in Swift:
class ViewController: UITableViewController {
var locations:NSArray=[]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var jsonLoader:JSONLoader = JSONLoader()
var url = NSURL(fileURLWithPath: "http://mechnikova.info/api/pic2.php?task=1")
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
self.locations = jsonLoader.locationsFromJSONFile(url)
self.tableView.performSelectorOnMainThread(selector:(reloadData), withObject: nil, waitUntilDone: true)
})
}
I have error - Use of unresolved identifier 'reloadData' in
self.tableView.performSelectorOnMainThread(selector:(reloadData), withObject: nil, waitUntilDone: true)
Help please!
Use:
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
self.locations = jsonLoader.locationsFromJSONFile(url)
dispatch_async(dispatch_get_main_queue(),{
self.tableView.reloadData()
})
})
for convenience, you can use Costant
let diffQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
let diffMain = dispatch_get_main_queue()
and then used in a viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
dispatch_async(diffQueue) {
self.locations = jsonLoader.locationsFromJSONFile(url)
dispatch_async(self.diffMain){
self.tableView.reloadData()
}
})
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With