How to dispatch code blocks to the same thread in iOS?

前端 未结 8 1774
广开言路
广开言路 2021-01-31 11:04

Main aspect of the question: It\'s about iOS. Can I somehow dispatch code blocks in a way, that they will all (a) run in background and (b) on the same thread?

8条回答
  •  不要未来只要你来
    2021-01-31 11:23

    Never tried this but this might do the trick. Use separate properties of atomic dispatch queues for each operation.

        @property (strong, atomic) dispatch_queue_t downloadQueue;
    

    Queue/Thread 1 for first operation

        downloadQueue = dispatch_queue_create("operation1", NULL);
    

    etc.

    Since atomic is thread safe, downloadQueue should not be accessed by other threads. So it makes sure that there will be only single thread per operation and other threads will not access it.

提交回复
热议问题