0

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?

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
  • why you're using force casting lastURLSessionDataTask? use a guard statement instead of do it by if let lastSession = self.lastURLSessionDataTask, lastSession.state == running{} – tryKuldeepTanwar Apr 15 '19 at 06:13

1 Answers1

0

Because lastURLSessionDataTask is an optional type of URLSessionTask, Instead of Force Casting you should either use guard statement

override func viewWillDisappear(_ animated: Bool) {
    guard let task = lastURLSessionDataTask, task.state == .running else { super.viewWillDisappear(animated); return }
    task.cancel()
    task = nil
}

Or Check the value by

override func viewWillDisappear(_ animated: Bool) {
    if let task = lastURLSessionDataTask, task.state == .running {
        task.cancel()
        task = nil
    }
    super.viewWillDisappear(animated)
}
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
  • There is nothing wrong with the force cast as the first check is for `nil` and due to short-circuit evaluation the rest of the expression will be skipped if `lastURLSessionDataTask` is `nil`. – vadian Apr 15 '19 at 06:51
  • Correct, It was a suggestion not to use force casting that was the only thing I find wrong in this code. Did you find anything that makes the crash happen @vadian – tryKuldeepTanwar Apr 15 '19 at 07:13
  • No, I didn't. `NSURLSessionDataTask` does have a `state` property. Basically the code is supposed to work. – vadian Apr 15 '19 at 07:17
  • maybe `if let task = lastURLSessionDataTask as? URLSessionTask` will help? i don't know. – tryKuldeepTanwar Apr 15 '19 at 07:28
  • Exactly, i feel the same, it must work, but why it is making crash. Also, it is not reproducible, it randomly appears. – Mehul Thakkar Apr 15 '19 at 07:28
  • @dreamBegin, As `lastURLSessionDataTask` is already of type `URLSessionDataTask?`(which is subclass of `URLSessionTask`), so i think this is not making any sense. – Mehul Thakkar Apr 15 '19 at 07:32
  • if you have declared lastURLSessionDataTask as URLSessionDataTask then it should work. – tryKuldeepTanwar Apr 15 '19 at 07:33
  • Yes, thats why i posted this question, i already searched enough via google for such weird behaviour, but not getting any proper solution. – Mehul Thakkar Apr 15 '19 at 07:35