URLSession.datatask with request block not called in background

后端 未结 4 1081
轻奢々
轻奢々 2020-11-29 06:08

URLSession data task block is not calling when the app is in background and it stuck at dataTask with request.
When I open the app the block ge

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 06:57

    Or simply start a BackgroundTask

    func send(...) {
      let backgroundTaskID = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
      let session = URLSession(configuration: .default)
    
      // ... after all the main logic, when you finish:
      DispatchQueue.main.async {
        completion(result)
        UIApplication.shared.endBackgroundTask(backgroundTaskID)
      }
    }
    

提交回复
热议问题