I have screen with searchbar, when i leave this screen, i am just cancelling the ongoing running urlSessionDataTask. Here, is my code, by which i am using:
override func viewWillDisappear(_ animated: Bool) {
if self.lastURLSessionDataTask != nil, self.lastURLSessionDataTask!.state == .running{
self.lastURLSessionDataTask!.cancel();
self.lastURLSessionDataTask = nil;
}
super.viewWillDisappear(animated);
}
As you can see in code, i am first checking if my lastURLSessionDataTask
variable is not nil, then checking its state, if running then cancelling that task.
Now the issue is, it gives crash with detail as : [NSURLSessionDataTask state]: unrecognised selector sent to instance
.
Also to note: It is not giving crash each and every time, this get appeared randomly at any time. I am not sure, what i am doing wrong. Can anyone have any idea? Does anyone faced this earlier?