I put in a new data loading feature in my app. It's meant to transfer and process the contents of a large database from the mobile device into the backend. In ever function I run in this pipeline, the entire contents of the function are inside a
dispatch_async
This dispatches to a non-main thread. I have also verified with logs that these are working. Every function along the pipeline is off the main thread. And yet I am experiencing UI freeze-up.
Questions:
if(Looper. getMainLooper(). getThread() == Thread. currentThread()) { // Current Thread is Main Thread. }
As such, the main thread is also sometimes called the UI thread. However, under special circumstances, an app's main thread might not be its UI thread; for more information, see Thread annotations. The system does not create a separate thread for each instance of a component.
Only the Main thread, also known as the UI thread can update the UI a.f.a.i.k.
If you put long running work on the UI thread, you can get ANR errors. If you have multiple threads and put long running work on the non-UI threads, those non-UI threads can't inform the user of what is happening.
You should profile your app with Instruments. Time Profiler (make sure to use "Record Waiting Threads" option) can be useful, as is System Trace. For both, you might want to use the "Thread Strategy" view, , focusing on the main thread. There are a bunch of WWDC videos that describe various approaches, including the dated, yet still relevant, 2012 video Building Concurrent User Interfaces on iOS. Also look for newer WWDC videos that reference "profiling" and "instruments".
Regarding non-main threads adversely affecting performance, it's generally negligible and you probably have something else going on. The only time it's a significant problem is when you're using a very old device that doesn't support multithreading.
By the way, are you 100% sure the main thread is really not being responsive? Or is it possibly that you're just not seeing UI updates being reflected in a timely manner? This can be a result of accidentally doing UI updates from the background thread rather than dispatching them back to the main queue.
If you want more concrete counsel, we need a reproducible example of the performance problem. But in the abstract,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With