From 669b342332a643f54693488e0da91e964111257d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 11 Apr 2014 16:01:58 -0400 Subject: [PATCH] make processing more parallel --- ios/MBXViewController.mm | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index 9e08a6ddc..f2ed117d4 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -64,6 +64,15 @@ class MBXMapView { [super loadView]; + queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT); + + NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; + sessionConfig.timeoutIntervalForResource = 6; + sessionConfig.HTTPMaximumConnectionsPerHost = 8; + sessionConfig.requestCachePolicy = NSURLRequestUseProtocolCachePolicy; + + session = [NSURLSession sessionWithConfiguration:sessionConfig]; + self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; if ( ! self.context) @@ -439,8 +448,11 @@ class MBXMapView return ([validSimultaneousGestures containsObject:[gestureRecognizer class]] && [validSimultaneousGestures containsObject:[otherGestureRecognizer class]]); } -MBXMapView *mapView; -CADisplayLink *displayLink; +MBXMapView *mapView = nullptr; +CADisplayLink *displayLink = nullptr; +dispatch_queue_t queue = nullptr; +NSURLSession *session = nullptr; + namespace llmr { @@ -460,25 +472,30 @@ namespace llmr Request *request_http(std::string url, std::function background_function, std::function foreground_callback) { - NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@(url.c_str())] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) + if (!session) { + session = [[NSURLSession alloc] init]; + } + NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@(url.c_str())] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil]; + dispatch_async(queue, ^(){ + [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil]; + Response res; - Response res; + if ( ! error && [response isKindOfClass:[NSHTTPURLResponse class]]) + { + res.code = [(NSHTTPURLResponse *)response statusCode]; + res.body = { (const char *)[data bytes], [data length] }; + } else { + NSLog(@"http error (%s): %@", url.c_str(), [error localizedDescription]); + } - if ( ! error && [response isKindOfClass:[NSHTTPURLResponse class]]) - { - res.code = [(NSHTTPURLResponse *)response statusCode]; - res.body = { (const char *)[data bytes], [data length] }; - } else { - NSLog(@"http error (%s): %@", url.c_str(), [error localizedDescription]); - } + background_function(res); - background_function(res); + dispatch_async(dispatch_get_main_queue(), ^(void) + { + foreground_callback(); + }); - dispatch_async(dispatch_get_main_queue(), ^(void) - { - foreground_callback(); }); }]; @@ -496,7 +513,7 @@ namespace llmr void cancel_request_http(Request *request) { - [[NSURLSession sharedSession] getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) + [session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) { for (NSURLSessionDownloadTask *task in downloadTasks) {