将一些 Alamofire 组合到队列中的最佳方法是什么?
what is the best way to combine some Alamofire to queue?
我是 Swift 的新手,对线程、操作、队列等几乎一无所知。例如,我的应用程序中有初始页面,我在其中使用了 3 个 alamofire 请求,如下所示:
request1 => request2
request3
=> segue to the next view
这意味着,请求 1
和 2
是串行的,但是 3
可以是并行的,只有当所有请求都完成并处理后才会继续。
我已经尝试过 OperationsQueue
并且至少速度更快 =) 但是在 Xcode 的控制台我看到 Alamofire 的成功案例中的一些代码是在执行 segue 之后完成的。
那么合并最多 5 个 Alamofire 而不嵌套 的最佳做法是什么?
或者如何使用 OperationQueue
正确处理异步函数?
DispatchGroup 有时会导致 死锁 等问题。您可以使用完成块。完成块函数是异步函数。
func 1(completion:@escaping () -> ()) {
func 2(completion:@escaping () -> ()) {
func 3(completion:@escaping () -> ()) {
//You dont never know when func1 is finished. So you have to write nested functions.
//In this situation func2 waits func1. And func3 wait for func1 and func2.
//You can write the func3 at the top. This is you choice.
}
}
}
我是 Swift 的新手,对线程、操作、队列等几乎一无所知。例如,我的应用程序中有初始页面,我在其中使用了 3 个 alamofire 请求,如下所示:
request1 => request2
request3
=> segue to the next view
这意味着,请求 1
和 2
是串行的,但是 3
可以是并行的,只有当所有请求都完成并处理后才会继续。
我已经尝试过 OperationsQueue
并且至少速度更快 =) 但是在 Xcode 的控制台我看到 Alamofire 的成功案例中的一些代码是在执行 segue 之后完成的。
那么合并最多 5 个 Alamofire 而不嵌套 的最佳做法是什么?
或者如何使用 OperationQueue
正确处理异步函数?
DispatchGroup 有时会导致 死锁 等问题。您可以使用完成块。完成块函数是异步函数。
func 1(completion:@escaping () -> ()) {
func 2(completion:@escaping () -> ()) {
func 3(completion:@escaping () -> ()) {
//You dont never know when func1 is finished. So you have to write nested functions.
//In this situation func2 waits func1. And func3 wait for func1 and func2.
//You can write the func3 at the top. This is you choice.
}
}
}