summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-04-15 11:06:29 -0400
committerKonstantin Käfer <mail@kkaefer.com>2014-04-15 11:06:29 -0400
commit79d34115d05a6bffadd3568044187f8bc8ed7519 (patch)
tree3aa5a7ac85d98bab38344876f2253a283e82f4b7 /common
parentab66ab39e6c46406f45e4898cc72359deb74583b (diff)
downloadqtlocation-mapboxgl-79d34115d05a6bffadd3568044187f8bc8ed7519.tar.gz
make processing more parallel
restores 669b342332a643f54693488e0da91e964111257d
Diffstat (limited to 'common')
-rw-r--r--common/foundation_request.mm48
1 files changed, 35 insertions, 13 deletions
diff --git a/common/foundation_request.mm b/common/foundation_request.mm
index eb662a8de7..297b4f380e 100644
--- a/common/foundation_request.mm
+++ b/common/foundation_request.mm
@@ -4,8 +4,25 @@
#include <string>
#include <functional>
#include <llmr/platform/platform.hpp>
+#include <uv.h>
+uv_once_t request_initialize = UV_ONCE_INIT;
+dispatch_queue_t queue = nullptr;
+NSURLSession *session = nullptr;
+
+
+void request_initialize_cb() {
+ queue = dispatch_queue_create("Parsing", DISPATCH_QUEUE_CONCURRENT);
+
+ NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
+ sessionConfig.timeoutIntervalForResource = 6;
+ sessionConfig.HTTPMaximumConnectionsPerHost = 8;
+ sessionConfig.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
+
+ session = [NSURLSession sessionWithConfiguration:sessionConfig];
+}
+
namespace llmr {
class platform::Request {
@@ -19,8 +36,11 @@ public:
std::shared_ptr<platform::Request>
platform::request_http(const std::string &url, std::function<void(Response *)> background_function,
std::function<void()> foreground_callback) {
+
+ uv_once(&request_initialize, request_initialize_cb);
+
__block std::shared_ptr<Request> req;
- NSURLSessionDataTask *task = [[NSURLSession sharedSession]
+ NSURLSessionDataTask *task = [session
dataTaskWithURL:[NSURL URLWithString:@(url.c_str())]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Make sure we clear the shared_ptr to resolve the circular reference. We're referencing
@@ -34,22 +54,24 @@ platform::request_http(const std::string &url, std::function<void(Response *)> b
return;
}
- Response res;
+ dispatch_async(queue, ^(){
+ Response res;
- if (!error && [response isKindOfClass:[NSHTTPURLResponse class]]) {
- res.code = [(NSHTTPURLResponse *)response statusCode];
- res.body = {(const char *)[data bytes], [data length]};
- } else {
- res.code = -1;
- res.error_message = [[error localizedDescription] UTF8String];
- }
+ if (!error && [response isKindOfClass:[NSHTTPURLResponse class]]) {
+ res.code = [(NSHTTPURLResponse *)response statusCode];
+ res.body = {(const char *)[data bytes], [data length]};
+ } else {
+ res.code = -1;
+ res.error_message = [[error localizedDescription] UTF8String];
+ }
- background_function(&res);
+ background_function(&res);
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- foreground_callback();
+ dispatch_async(dispatch_get_main_queue(), ^(void) {
+ foreground_callback();
- [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil];
+ [[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil];
+ });
});
}];