使用RxSwift限制对服务类的并发访问

时间:2017-05-04 23:47:14

标签: swift reactive-programming rx-swift

给定这样的服务类:

class Service {
    let networkService = NetworkService()

    func handleJobA(input: String) -> Observable<ResultA> {
        return networkService
            .computeA(input)
            .map { $0.a }
    }
}

当我从呼叫者那边使用它时:

let service = Service()

Observable
    .from(["Hello", "World"])
    .flatMap {
        service.handleJobA($0)
    }
    .subscribe()

然后,这会同时向service发送多个请求。我希望流等待每个请求完成。这可以使用merge运算符实现。

Observable
    .from(["Hello", "World"])
    .flatMap {
        Observable.just(
            service.handleJobA($0)
        )
    }
    .merge(maxConcurrent: 1)
    .subscribe()

到目前为止,这么好 - 该服务不会同时执行多个handleJobA任务。

但是,并发性是服务细节,调用者不应该关心它。实际上,该服务在稍后阶段可能决定允许差异并发值。

其次,当我添加新方法handleJobB时,它不能与作业A同时处于活动状态,反之亦然。

所以我的问题是:

  1. 如何将maxConcurrency作为实现细节限制为handleJobA observable?
  2. 哪种RxSwift模式允许对任何服务方法进行限制?

2 个答案:

答案 0 :(得分:4)

您需要一个专用于该服务的串行调度程序。这是一个可以粘贴到游乐场的例子:

/// playground

import RxSwift

class Service {

    func handleJobA(input: String) -> Observable<String> {

        return Observable.create { observer in
            print("start job a")
            sleep(3)
            observer.onNext(input)
            print("complete job a")
            observer.onCompleted()
            return Disposables.create()
        }.subscribeOn(scheduler)
    }

    func handleJobB(input: String) -> Observable<String> {
        return Observable.create { observer in
            print("start job b")
            sleep(3)
            observer.onNext(input)
            print("complete job b")
            observer.onCompleted()
            return Disposables.create()
            return Disposables.create()
        }.subscribeOn(scheduler)
    }

    let scheduler = SerialDispatchQueueScheduler(internalSerialQueueName: "Service")
}


let service = Service()

_ = Observable.from(["hello","world","swift"])
    .flatMap { service.handleJobA(input: $0) }
    .subscribe(onNext:{
        print("result " + $0)
    })

_ = Observable.from(["hello","world","swift"])
    .flatMap { service.handleJobB(input: $0) }
    .subscribe(onNext:{
        print("result " + $0)
    })

import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

答案 1 :(得分:0)

也许你想要concat运算符,我在下面写了一些测试代码,看看你是否想要:

 func sleepAndPrint(label:String) -> Observable<String> {


        return Observable.create { obser -> Disposable in
            DispatchQueue.global().async {
                sleep(3)
                print("\(label) come")
                obser.onNext(label)
                obser.onCompleted()
            }

            return Disposables.create()
        }
    }


Observable.from(["hello","world","swift"])
    // we need observable of observable sequences so just use map
    // Observable<Observable<String>> in this case
    .map{
        sleepAndPrint(label: $0)
    }
    // Concatenates all inner observable sequences, as long as the previous observable sequence terminated successfully.
    .concat()

    .subscribe(onNext:{
        print("subscribe: " + $0)
    })
    .addDisposableTo(disposeBag)

    prints : 
      hello come
      subscribe: hello
      world come
      subscribe: world
      swift come
      subscribe: swift