Reputation:
Does the Main thread run asynchronously or synchronously in iOS?
Please explain with examples.
Upvotes: 2
Views: 1203
Reputation: 114855
Your question doesn't really make sense.
The thread doesn't run synchronously or asynchronously. Tasks are dispatched onto threads.
The main queue is a serial dispatch queue, so it only executes a single task at a time, and that task is always executed on the main thread. Tasks can be added to the main queue (and indeed any queue) synchronously or asynchronously and this is the problem with your question; synchronous or asynchronous dispatch is relative to the task that is dispatching the new task.
Upvotes: 7
Reputation: 11
I think the question is not how the main thread runs, but how YOU run on it. For example - the main dispatch queue (runs on the main thread) can be referenced as sync or async:
DispatchQueue.main.async {
print("async on the main thread")
}
DispatchQueue.main.sync {
print("sync on the main thread")
}
Upvotes: 1
Reputation: 190
Main thread runs synchronously. All UI related operations should be preformed on main thread.
Server calls like downloading or uploading should be performed asynchronously ( on background thread ).
Upvotes: 2