Make writing a habit together! This is the 13th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

  • This paper mainly introduces the scheduling of queues, includingConcurrent and serial queues, send and subscribe environments.

1. ConcurrentDispatchQueueScheduler

Let’s look at an example of a concurrent queue

DispatchQueue.global().async {

            print("current:\(Thread.current)")

            let ob = Observable<Int>.of(1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100)

            

            ob

                .observe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))

// .subscribe(on: ConcurrentDispatchQueueScheduler(qos: .background) )

                .subscribe(onNext: {print($0,Thread.current)})

                .disposed(by: self.disposeBag)

            

        }
Copy the code

Print result:

If we print the result, we can see that our queue is a concurrent asynchronous queue. Here’s why we want to send 100 elements, because our sequence is a pipe in the first place and our queue follows the FIFO principle. So if it’s small, it’s basically thread 7 up to 91.

ObserveOn here runs its observer callback on the specified scheduler, and when we use subscribeon we run its subscribe and unsubscribe on the specified scheduler

Similar to the effect of the concurrent queue synchronization and asynchronous functions in our GCD.

2. SerialDispatchQueueScheduler

  • observeOn

Specifies that the serial queue environment runs its observer callback

  • subscribeon

Specifies the serial queue environment to run its subscribe and unsubscribe

3. OperationQueueScheduler

  • observeOn

Encapsulating the OperationQueue is concurrency by default

  • subscribeOn

4. MainScheduler

  • observeOn

  • subscribeOn

5. To summarize

The above is about the use of queues in Rx Swift. There are altogether 4 queues, all of which are asynchronous by default. Our sequence itself is equivalent to a form of queue. The default is the main thread asynchronous function execution when we do not specify an observer or subscriber scheduling environment.