Preface:

Today, in the group, a friend asked a question. When they interact with JS, they need to wait for a callback from JS.

The main thread calls a network request, then waits for the result of the network request to come back, and then returns.

He said that they had previously solved the problem by adding listeners to the runloop callback. Saying raises some other questions, asking if anyone knows how to do it.

To solve

I thought most people knew this until he solved it with Runloop and a lot of people in the group said it couldn’t be done, so I realized it might not be so simple.

Methods a

If you really don’t know how to do this, then remove the return and replace it with a block callback and make the call. Of course this may require you to rewrite some of the logic, but it’s easy to work out.

Method 2

It’s plugging the main thread. So here’s the problem

  • Can the main thread be blocked?
  • Main thread callasyncWill new threads be created?

Main thread callasyncWill new threads be created?

Let’s start with basic async whether it’s a synchronous queue or an asynchronous queue, calling async will create threads, but what’s the difference? Is the main thread async going to work?

It is recommended to enlarge the image first and think about what the output will be.

阿鲁纳恰尔邦1--<NSThread: 0x600001e08880>{number = 1, name = main}**

**3--<NSThread: 0x600001e5cc40>{number = 5, name = (null)}**

**2--<NSThread: 0x600001e08880>{number = 1, name = main}**
Copy the code

The asynchronous invocation of the main thread does not open up new threads, but the main queue is a serial queue. After the asynchronous invocation of tasks, the current task must be executed before the subsequent task will be executed.

How to print a custom synchronization queue?

First analysis, custom synchronization queue:

  • On the main thread, so1, four, fiveIt’s on the main thread.The 1-4-5There should be no doubt about the order
  • Problem 2 and 3, task 2 is first added to the synchronization queuequeue, so2-3The order.
  • 2Task not affected4The execution, however3The task is blocked5Task execution.

So 1-4-2-3-5 where the 2 and 4 are not fixed,1 is first,3,5 is last

So,syncThe call will not create a thread, it will be in the current task thread, and the task will block, waitblock_tYou can continue the current task only after the task in.

How to print a custom asynchronous queue?

Analyze:

According to the above question, we know the logic of the task running in the thread

  • Well, first of all,The 1-3-4-5Is on the main thread, or the thread where demo4 is called.
  • 2The task is asynchronously queued, executed asynchronously, and unfettered in another thread.

So the final print is: one-three-four-five, with two anywhere after one.

Main thread blocking solution

Problem code:

The problem is that the main thread calls a method and needs to waitwebHowever, the main thread can not use the semaphore card, a card will be stuck, so the people in the group saidrunloopTo change the solution, there is no good way.

You can think about it for three seconds, and it could be 10 or 15, it doesn’t really matter, but it’s probably 10:

I’ll give you a straight answer, because I think it’s common sense to write more threads, queues, locks, and so on, and it’s not that hard to understand:

There are other implementations, but this is just one way to think about it, and if you understand it, you can actually think about others. This also makes it clear that one thread has multiple queues and one queue has multiple tasks.

Note, do not add getMainQueue sync task in this, otherwise the task will block each other, I tried, a third party in our project, his request internal call HUD, so this will directly task jam, interface will be stuck.

The following figure shows the relationship between threads and queues:

I will list the important points of GCD source code in detail later.

conclusion

The meaning of the existence of the world is not to find and solve the problem, so the problem is the meaning of typing code.