Friday, April 17, 2020

Be Careful with "UI API called on a background thread"


After maybe one months I'm quarantined, It's so bored to be at home without doing anything. Luckily I have projects to do. And Of course to write post for this ungapps blog. Today I'm glad I can write to make my english writting better. I think day after day my english gets better haha. Yes honestly I still used google translate to translate some vocabularies that I literally have forgotten.

Today, I want to dig a little bit deeper about Thread in iOS Swift. We need to learn this thing. Because It's very important when we're doing UI think with other thread like downloading video. Because if you don't pay attentation enough, your app would be like freeze in a moment. And It's very bad for User Experience.

Luckily Xcode helps us to catch something that's not right. It would show message like title above "UI API callend on a background thread" in a specific line of code. To do that you need Runtime API Checking is ticked. Edit Scheme->Run (Debug)->Diagnostics->Runtime API Checking. By default it should've been ticked.




I have given you an example when message "UI API called on a background thread" appears on your Xcode. If you want to try, you can try to remove "DispatchQueue.main.async" part don't forget to remove Curly Bracket too.

I will give a screenshoot that I have got when trying not to use "DispatchQueue.main.async". Remember "DispatchQueue.main.async" is used for UI things. "DispatchQueue.global" you can use for others heavy computation or loading big data from internet.






Thanks for visiting my simple blog. I hope I can share more knowledge not just iOS background. If I have more time, I'm interested to learn about Flutter.

No comments:

Post a Comment