alternative http_request function
This commit is contained in:
@@ -21,6 +21,7 @@ struct Response {
|
||||
};
|
||||
|
||||
void request_http(std::string url, std::function<void(Response&)> func);
|
||||
void request_http(std::string url, std::function<void(Response&)> func, std::function<void()> cb);
|
||||
|
||||
void async(std::function<void()> fn, std::function<void()> cb);
|
||||
|
||||
|
||||
@@ -248,6 +248,29 @@ void request_http(std::string url, std::function<void(Response&)> func) {
|
||||
}];
|
||||
}
|
||||
|
||||
void request_http(std::string url, std::function<void(Response&)> func, std::function<void()> cb) {
|
||||
NSMutableURLRequest *urlRequest = [NSMutableURLRequest
|
||||
requestWithURL:[NSURL
|
||||
URLWithString:[NSString
|
||||
stringWithUTF8String:url.c_str()]]];
|
||||
|
||||
[NSURLConnection
|
||||
sendAsynchronousRequest:urlRequest
|
||||
queue:[NSOperationQueue mainQueue]
|
||||
completionHandler: ^ (NSURLResponse* response, NSData* data, NSError* error) {
|
||||
Response res;
|
||||
if (error == nil) {
|
||||
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
|
||||
res.code = [httpResponse statusCode];
|
||||
res.body = { (const char *)[data bytes], [data length] };
|
||||
}
|
||||
|
||||
func(res);
|
||||
dispatch_async(dispatch_get_main_queue(), ^ {
|
||||
cb();
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
double time() {
|
||||
|
||||
@@ -77,16 +77,13 @@ void Tile::request() {
|
||||
platform::request_http(url, [=](platform::Response& res) {
|
||||
if (res.code == 200) {
|
||||
tile->data.swap(res.body);
|
||||
|
||||
platform::async([tile]() {
|
||||
tile->parse();
|
||||
}, []() {
|
||||
// TODO: Make sure this gets passed the correct map ID/pointer.
|
||||
platform::restart(NULL);
|
||||
});
|
||||
tile->parse();
|
||||
} else {
|
||||
fprintf(stderr, "tile loading failed\n");
|
||||
}
|
||||
}, []() {
|
||||
// TODO: Make sure this gets passed the correct map ID/pointer.
|
||||
platform::restart(NULL);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user