|
29 | 29 | return std::string("RESP-") + std::to_string(seq.fetch_add(1)); |
30 | 30 | } |
31 | 31 |
|
| 32 | +static dispatch_once_t once; |
| 33 | +static NSURLSession* session; |
| 34 | + |
32 | 35 | class HttpRequestApple : public SimpleHttpRequest |
33 | 36 | { |
34 | 37 | public: |
|
37 | 40 | m_parent(parent) |
38 | 41 | { |
39 | 42 | m_parent->Add(static_cast<IHttpRequest*>(this)); |
| 43 | + dispatch_once(&once, ^{ |
| 44 | + NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; |
| 45 | + session = [NSURLSession sessionWithConfiguration:sessionConfig]; |
| 46 | + }); |
40 | 47 | } |
41 | 48 |
|
42 | 49 | ~HttpRequestApple() noexcept |
@@ -65,18 +72,16 @@ void SendAsync(IHttpResponseCallback* callback) |
65 | 72 | HandleResponse(data, response, error); |
66 | 73 | }; |
67 | 74 |
|
68 | | - NSURLSession* sess = (__bridge NSURLSession*)m_parent->GetSession(); |
69 | | - |
70 | 75 | if(equalsIgnoreCase(m_method, "get")) |
71 | 76 | { |
72 | 77 | [m_urlRequest setHTTPMethod:@"GET"]; |
73 | | - m_dataTask = [sess dataTaskWithRequest:m_urlRequest completionHandler:m_completionMethod]; |
| 78 | + m_dataTask = [session dataTaskWithRequest:m_urlRequest completionHandler:m_completionMethod]; |
74 | 79 | } |
75 | 80 | else |
76 | 81 | { |
77 | 82 | [m_urlRequest setHTTPMethod:@"POST"]; |
78 | 83 | NSData* postData = [NSData dataWithBytes:m_body.data() length:m_body.size()]; |
79 | | - m_dataTask = [sess uploadTaskWithRequest:m_urlRequest fromData:postData completionHandler:m_completionMethod]; |
| 84 | + m_dataTask = [session uploadTaskWithRequest:m_urlRequest fromData:postData completionHandler:m_completionMethod]; |
80 | 85 | } |
81 | 86 |
|
82 | 87 | [m_dataTask resume]; |
@@ -139,31 +144,14 @@ void Cancel() |
139 | 144 |
|
140 | 145 | HttpClient_Apple::HttpClient_Apple() |
141 | 146 | { |
142 | | - @autoreleasepool |
143 | | - { |
144 | | - NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; |
145 | | - m_session = (__bridge_retained void*)[NSURLSession sessionWithConfiguration:sessionConfig]; |
146 | | - } |
147 | 147 | LOG_TRACE("Initializing HttpClient_Apple..."); |
148 | 148 | } |
149 | 149 |
|
150 | 150 | HttpClient_Apple::~HttpClient_Apple() noexcept |
151 | 151 | { |
152 | | - // Invalidate and release the session. By this point, FlushAndTeardown |
153 | | - // should have called CancelAllRequests which drains all in-flight tasks. |
154 | | - if (m_session) { |
155 | | - NSURLSession* sess = (__bridge_transfer NSURLSession*)m_session; |
156 | | - [sess invalidateAndCancel]; |
157 | | - m_session = nullptr; |
158 | | - } |
159 | 152 | LOG_TRACE("Shutting down HttpClient_Apple..."); |
160 | 153 | } |
161 | 154 |
|
162 | | -void* HttpClient_Apple::GetSession() const |
163 | | -{ |
164 | | - return m_session; |
165 | | -} |
166 | | - |
167 | 155 | IHttpRequest* HttpClient_Apple::CreateRequest() |
168 | 156 | { |
169 | 157 | auto request = new HttpRequestApple(this); |
|
0 commit comments