问题描述
我编写了一些代码,通过 HTTP 请求从远程服务器实时下载和处理数据。它是一个连续流(基于 HTTP-Multipart),所以我处理实时数据直到我停止它。这是骨架:
private var currentThread: Thread? = nil
private var isRunning = true
func startAccess() {
let configuration = ...
let session = URLSession(configuration: configuration,delegate: self,delegateQueue: nil)
let request = URLRequest(...)
let task = session.dataTask(with: request)
currentThread = Thread.current
task.resume()
let timer = Timer(timeInterval: .greatestFiniteMagnitude,repeats: true) { timer in
return
}
RunLoop.current.add(timer,forMode: .default)
while isRunning && RunLoop.current.run(mode: .default,before: Date.distantFuture) {}
task.cancel()
}
我用
来阻止它func stopAccess() {
if isRunning {
isRunning = false
perform(#selector(doExitEventLoop),on: currentThread!,with: nil,waitUntilDone: (Thread.current == currentThread))
}
}
所有数据处理都是在委托方法中完成的,这不是这里的重点。
我认为在这里使用 RunLoop 既丑陋又沉重,并且很难使线程安全,因为 stopAccess()
方法可以由除 startAccess()
之外的其他线程调用。此外,URLSession 有自己的操作队列,因此我必须将数据处理传输到调用 startAccess()
方法(带有 perform(#selector...)
)的线程,例如:
func urlSession(_ session: URLSession,dataTask: URLSessionDataTask,didReceive data: Data) {
...
if isRunning {
perform(#selector(handleData),with: ...,waitUntilDone: true)
}
}
我通过对 Atomic<Bool>
变量使用 isRunning
来增强此代码(此处的示例只是一个骨架)。但它仍然是非常罕见的错误,例如 target thread exited while waiting for the perform
,或者如果 currentThread
已经完成则崩溃。
所以这是我的问题:我怎样才能做得更好?还有其他方法可以等待 URLSession 的停止调用吗?我在 API 中没有看到的东西?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)