From 565a23785c385021a4f41a12e43152c3f0adb5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 13 Feb 2014 12:15:01 +0100 Subject: [PATCH] remove unused functions and run http_request in other operation queue --- include/llmr/platform/platform.hpp | 3 -- ios/MBXViewController.mm | 58 ++++++------------------------ macosx/main.mm | 57 ++++++++--------------------- 3 files changed, 25 insertions(+), 93 deletions(-) diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index 6b3912d09..f56e738ff 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -23,11 +23,8 @@ struct Response { std::string body; }; -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); - // Returns a relative timestamp in seconds. This value must be monotonic. double time(); diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index d27a1aad3..ca66a4268 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -368,7 +368,7 @@ class MBXMapView CADisplayLink *displayLink; MBXMapView *mapView; -NSOperationQueue *queue; +NSOperationQueue *queue = NULL; namespace llmr { @@ -378,58 +378,18 @@ namespace llmr { } - void async(std::function fn, std::function cb) - { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) - { - fn(); - - dispatch_async(dispatch_get_main_queue(), ^(void) - { - cb(); - }); - }); - } - - void request_http(std::string url, std::function func) - { - [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@1 forKey:@"count"]]; - - NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]]; - - [NSURLConnection sendAsynchronousRequest:urlRequest - queue:[NSOperationQueue mainQueue] - completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) - { - if ( ! error) - { - Response res; - - res.code = [(NSHTTPURLResponse *)response statusCode]; - res.body = { (const char *)[data bytes], [data length] }; - - func(res); - } - else - { - Response res; - - func(res); - } - - [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@(-1) forKey:@"count"]]; - [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil]; - }]; - } - void request_http(std::string url, std::function func, std::function cb) { [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@1 forKey:@"count"]]; + if (!queue) { + queue = [[NSOperationQueue alloc] init]; + } + NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]]; [NSURLConnection sendAsynchronousRequest:urlRequest - queue:[NSOperationQueue mainQueue] + queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { Response res; @@ -439,10 +399,12 @@ namespace llmr res.code = [(NSHTTPURLResponse *)response statusCode]; res.body = { (const char *)[data bytes], [data length] }; } - + func(res); - cb(); + dispatch_async(dispatch_get_main_queue(), ^(void) { + cb(); + }); [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil userInfo:[NSDictionary dictionaryWithObject:@(-1) forKey:@"count"]]; [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil]; diff --git a/macosx/main.mm b/macosx/main.mm index db244da04..8eb22e782 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -57,12 +57,11 @@ public: glfwSetKeyCallback(window, key); [[NSNotificationCenter defaultCenter] addObserverForName:MBXNeedsRenderNotification - object:nil - queue:[NSOperationQueue mainQueue] - usingBlock:^(NSNotification *notification) - { - dirty = true; - }]; + object:nil + queue:[NSOperationQueue mainQueue] + usingBlock: ^ (NSNotification * notification) { + dirty = true; + }]; } static void character(GLFWwindow *window, unsigned int codepoint) { @@ -163,7 +162,7 @@ public: if (dirty) { try { dirty = render(); - } catch(std::exception& ex) { + } catch (std::exception& ex) { fprintf(stderr, "exception: %s\n", ex.what()); } glfwSwapBuffers(window); @@ -218,6 +217,7 @@ public: }; MapView *view; +NSOperationQueue *queue = NULL; namespace llmr { namespace platform { @@ -226,16 +226,11 @@ void restart(void *) { view->dirty = true; } -void async(std::function fn, std::function cb) { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ - fn(); - dispatch_async(dispatch_get_main_queue(), ^(void){ - cb(); - }); - }); -} +void request_http(std::string url, std::function func, std::function cb) { + if (!queue) { + queue = [[NSOperationQueue alloc] init]; + } -void request_http(std::string url, std::function func) { NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString @@ -244,31 +239,7 @@ void request_http(std::string url, std::function func) { [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] - completionHandler: ^ (NSURLResponse* response, NSData* data, NSError* error) { - if (error == nil) { - NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; - Response res; - res.code = [httpResponse statusCode]; - res.body = { (const char *)[data bytes], [data length] }; - func(res); - } else { - Response res; - func(res); - } - [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil]; - }]; -} - -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) { + completionHandler: ^(NSURLResponse * response, NSData * data, NSError * error) { Response res; if (error == nil) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; @@ -277,7 +248,9 @@ void request_http(std::string url, std::function func, std::fun } func(res); - cb(); + dispatch_async(dispatch_get_main_queue(), ^(void) { + cb(); + }); [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil]; }]; }