This repository was archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #129
- Loading branch information
Showing
12 changed files
with
112 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#import <Foundation/Foundation.h> | ||
#include <memory> | ||
#include <string> | ||
#include <functional> | ||
#include <llmr/platform/platform.hpp> | ||
|
||
|
||
namespace llmr { | ||
|
||
class platform::Request { | ||
public: | ||
Request(NSURLSessionDataTask *task, const std::string &original_url) | ||
: task(task), original_url(original_url) {} | ||
NSURLSessionDataTask *task; | ||
std::string original_url; | ||
}; | ||
|
||
std::shared_ptr<platform::Request> | ||
platform::request_http(const std::string &url, std::function<void(Response *)> background_function, | ||
std::function<void()> foreground_callback) { | ||
__block std::shared_ptr<Request> req; | ||
NSURLSessionDataTask *task = [[NSURLSession sharedSession] | ||
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 | ||
// this shared_ptr by value so that the object stays around until this completion handler is | ||
// invoked. | ||
req.reset(); | ||
|
||
if ([error code] == NSURLErrorCancelled) { | ||
// We intentionally cancelled this request. Do nothing. | ||
return; | ||
} | ||
|
||
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]; | ||
} | ||
|
||
background_function(&res); | ||
|
||
dispatch_async(dispatch_get_main_queue(), ^(void) { | ||
foreground_callback(); | ||
}); | ||
}]; | ||
|
||
req = std::make_shared<Request>(task, url); | ||
[task resume]; | ||
return req; | ||
} | ||
|
||
void platform::cancel_request_http(const std::shared_ptr<Request> &req) { | ||
if (req) { | ||
[req->task cancel]; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
305e688
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loses the
MBXUpdateActivityNotification
notification posting, which manages the network activity spinner on iOS (#56).305e688
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, never mind, seeing 70bb318 now.