apollo-ios icon indicating copy to clipboard operation
apollo-ios copied to clipboard

Deprecated initialization of URLSessionTask

Open AnthonyMDev opened this issue 4 years ago • 2 comments

On URLSessionClient():122 we are initializing a dummy URLSessionTask using a deprecated initializer. This is only being used when a request fails pre-flight. Should we make this function throwing or return an optional to solve this?

 @discardableResult
  open func sendRequest(_ request: URLRequest,
                        rawTaskCompletionHandler: RawCompletion? = nil,
                        completion: @escaping Completion) -> URLSessionTask {
    guard self.hasNotBeenInvalidated else {
      completion(.failure(URLSessionClientError.sessionInvalidated))
      return URLSessionTask()
    }
    
    let task = self.session.dataTask(with: request)
    let taskData = TaskData(rawCompletion: rawTaskCompletionHandler,
                            completionBlock: completion)
    
    self.tasks.mutate { $0[task.taskIdentifier] = taskData }
    
    task.resume()
    
    return task
  }

AnthonyMDev avatar Jul 12 '21 16:07 AnthonyMDev

@AnthonyMDev I believe we should be returning an optional with nil for the guard failure. This function is being asked to return a task and IMO an optional would be enough indication that it was successful or not, especially when you consider that the completion block is called on failure.

calvincestari avatar Jul 14 '21 23:07 calvincestari

Yeah, I think the main reason I wanted to return a Task was not to need the optional, but realistically almost anyplace that's holding onto the task is going to be holding on to it optionally for cancellation purposes etc. so returning an optional task should be fine.

designatednerd avatar Jul 14 '21 23:07 designatednerd