0

现在我遇到了一种情况,我从分离的线程开始了几个请求,比如线程池。

我想让这些请求是同步的,所以它们将在完全相同的线程上启动和结束,而不是在主线程上。

我尝试设置.observeOn(CurrentThreadScheduler.instance), 但由于响应处理程序线程始终是主线程而不是当前线程。

是否可以将当前线程指定为响应的处理程序线程?

4

1 回答 1

0

是否可以将当前线程指定为响应的处理程序线程?

有一种方法!看看MoyaProvider课堂。

/// Request provider class. Requests should be made through this class only.
open class MoyaProvider<Target: TargetType>: MoyaProviderType {

    /// Propagated to Alamofire as callback queue. If nil - the Alamofire default (as of their API in 2017 - the main queue) will be used.
    let callbackQueue: DispatchQueue?

    /// Initializes a provider.
    public init(..., callbackQueue: DispatchQueue? = nil, ...) {
        ...
        self.callbackQueue = callbackQueue
    }
}

您可以提供回调队列。只需提供所需的队列。

于 2018-03-21T03:35:51.393 回答