I’ve got a method that get c++ based callback as input param(ReplyCallback).
It passes this callback to background task (NSURLSessionDataTask) that send http request and uses this callback in the completionHandler.
Since this callback is used in another thread (on the completionHandler of NSURLSessionDataTask) I used semaphore so it won’t be deleted.
However, I sometimes get The a crash on this code due to the
Thread 14 name: NSOperationQueue 0x134609be0 (QOS: UNSPECIFIED)
Here’s my code (the relevant parts)
bool Send(Request &req, const std::function<void(bool, Reply)> &ReplyCallback) {
....
NSMutableURLRequest *urlReq = ...;
dispatch_semaphore_t sem;
sem = dispatch_semaphore_create(0);
NSURLSessionDataTask *_dTask = [session
dataTaskWithRequest:urlReq completionHandler:^(NSData * _Nullable data,NSURLResponse * _Nullable response, NSError * _Nullable error)
{
Reply r = ...;
...
ReplyCallback(true, r);
}
[_dTask resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
}
My question is whether using the semaphore is ok ?