使用 DEFER 离开 dispatchGroup 会导致崩溃
Leaving dispatchGroup with DEFER cause a crash
我有一个休息Api异步方法,所以使用调度组:
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
Task.do { result in
defer { dispatchGroup.leave() }
//...
}
这会导致崩溃 Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
但是
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
Task.do { result in
//...
dispatchGroup.leave()
}
不要崩溃。为什么?
要点 1 是:当函数范围结束时调用 defer ...
要点 2 是:dispatchGroup enter 应该等于 leaves 否则你的应用程序会崩溃
我有一个休息Api异步方法,所以使用调度组:
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
Task.do { result in
defer { dispatchGroup.leave() }
//...
}
这会导致崩溃 Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
但是
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
Task.do { result in
//...
dispatchGroup.leave()
}
不要崩溃。为什么?
要点 1 是:当函数范围结束时调用 defer ...
要点 2 是:dispatchGroup enter 应该等于 leaves 否则你的应用程序会崩溃