diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index e4d3231f6..2f0de333f 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -21,6 +21,7 @@ struct Response { }; void request_http(std::string url, std::function func); +void request_http(std::string url, std::function func, std::function cb); void async(std::function fn, std::function cb); diff --git a/macosx/main.mm b/macosx/main.mm index 68ebeedc5..f1b02dabf 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -248,6 +248,29 @@ void request_http(std::string url, std::function func) { }]; } +void request_http(std::string url, std::function func, std::function 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() { diff --git a/src/map/tile.cpp b/src/map/tile.cpp index 475d83593..dc5c4122f 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -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); }); }