summaryrefslogtreecommitdiff
path: root/macosx
diff options
context:
space:
mode:
authorJustin R. Miller <incanus@codesorcery.net>2014-02-25 16:33:06 -0800
committerJustin R. Miller <incanus@codesorcery.net>2014-02-25 16:33:06 -0800
commit55b28aaf6507569c9d1bb87279c8382f043e8b11 (patch)
treeac2908a0e2a08cb83c2c52ec5831dceadbc31fc3 /macosx
parent0406a8229903c880dc37fe5a6654fd49f9e3d4f5 (diff)
downloadqtlocation-mapboxgl-55b28aaf6507569c9d1bb87279c8382f043e8b11.tar.gz
allow for cancellable request, track in tiles, and cancel when obsolete
Diffstat (limited to 'macosx')
-rw-r--r--macosx/main.mm45
1 files changed, 11 insertions, 34 deletions
diff --git a/macosx/main.mm b/macosx/main.mm
index 1a37419c5f..dc65b75606 100644
--- a/macosx/main.mm
+++ b/macosx/main.mm
@@ -240,7 +240,7 @@ void restart() {
[[NSApplication sharedApplication] postEvent: [NSEvent eventWithCGEvent:event] atStart:NO];
}
-void request_http(std::string url, std::function<void(Response&)> background_function, std::function<void()> foreground_callback)
+Request request_http(std::string url, std::function<void(Response&)> background_function, std::function<void()> foreground_callback)
{
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@(url.c_str())] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
@@ -261,46 +261,23 @@ void request_http(std::string url, std::function<void(Response&)> background_fun
}];
[task resume];
+
+ Request req;
+
+ req.identifier = task.taskIdentifier;
+ req.original_url = url;
+
+ return req;
}
-void request_http_tile(std::string url, tile_ptr tile_object, std::function<void(Response&)> background_function, std::function<void()> foreground_callback)
+void cancel_request_http(Request request)
{
- NSString *latestZoom = [@(tile_object->id.z) stringValue];
-
[[NSURLSession sharedSession] getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks)
{
for (NSURLSessionDownloadTask *task in downloadTasks)
- if (task.taskDescription && ! [task.taskDescription isEqualToString:latestZoom])
- [task cancel];
- }];
-
- NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@(url.c_str())] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
- {
- if (error && [[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
- return;
-
- Response res;
-
- if ( ! error)
- {
- res.code = [(NSHTTPURLResponse *)response statusCode];
- res.body = { (const char *)[data bytes], [data length] };
- }
-
- background_function(res);
-
- dispatch_async(dispatch_get_main_queue(), ^(void)
- {
- foreground_callback();
- });
-
- if ( ! error)
- [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil];
+ if (task.taskIdentifier == request.identifier)
+ return [task cancel];
}];
-
- task.taskDescription = [@(tile_object->id.z) stringValue];
-
- [task resume];
}
double time() {