how ios UI update code defined for use with main dispatch
I've been reading a lot of example code, i know that generally you want the code that updates your UI to execute on the main thread. And xcode will complain, at runtime, if i m missing something. Only sometimes though.
So how is code that updates your UI actually defined?
Is presenting an alert updating the UI? calling reloadData() on a UITableView? What about simply setting the text of a UILabel?
Especially with these three, i've been seeing and using it both ways in my app and can't really figure out a rule. Especially since xcode lets me get away with both... sometimes.
Edit: Ohh, i understand where my confusion came from. I thought it was random what thread you were on. But you are actually "always" on the main thread, unless you do something like a task, which runs on a background thread.
So the Main Thread Checker didn't actually let me "get away" with using reload() and changing labels in my viewController falsely. It's just that i was guaranteed to be on the main thread.
I thought i had to suddenly wrap every single label change everywhere in dispatch.async.
ios swift multithreading user-interface grand-central-dispatch
add a comment |
I've been reading a lot of example code, i know that generally you want the code that updates your UI to execute on the main thread. And xcode will complain, at runtime, if i m missing something. Only sometimes though.
So how is code that updates your UI actually defined?
Is presenting an alert updating the UI? calling reloadData() on a UITableView? What about simply setting the text of a UILabel?
Especially with these three, i've been seeing and using it both ways in my app and can't really figure out a rule. Especially since xcode lets me get away with both... sometimes.
Edit: Ohh, i understand where my confusion came from. I thought it was random what thread you were on. But you are actually "always" on the main thread, unless you do something like a task, which runs on a background thread.
So the Main Thread Checker didn't actually let me "get away" with using reload() and changing labels in my viewController falsely. It's just that i was guaranteed to be on the main thread.
I thought i had to suddenly wrap every single label change everywhere in dispatch.async.
ios swift multithreading user-interface grand-central-dispatch
If something on the screen changes, it's updating the UI. Just about anything done with a class starting withUI
needs to be on the main queue. There are few exceptions.
– rmaddy
Nov 14 '18 at 8:00
User Interface, or UI, is basically any and everything that you see on screen/viewport. It can be a label. It can be a table view. It can be a button. Now if any of the values related to them are changing. For example. text of label, content of table view, color of button, its called updating the UI and Apple recommends it to be done on main thread. If not then it will make your app looked freezed up, delaying the changes that were intended to be immediate. This amounts to BAD User Experience.
– iOSer
Nov 14 '18 at 8:06
add a comment |
I've been reading a lot of example code, i know that generally you want the code that updates your UI to execute on the main thread. And xcode will complain, at runtime, if i m missing something. Only sometimes though.
So how is code that updates your UI actually defined?
Is presenting an alert updating the UI? calling reloadData() on a UITableView? What about simply setting the text of a UILabel?
Especially with these three, i've been seeing and using it both ways in my app and can't really figure out a rule. Especially since xcode lets me get away with both... sometimes.
Edit: Ohh, i understand where my confusion came from. I thought it was random what thread you were on. But you are actually "always" on the main thread, unless you do something like a task, which runs on a background thread.
So the Main Thread Checker didn't actually let me "get away" with using reload() and changing labels in my viewController falsely. It's just that i was guaranteed to be on the main thread.
I thought i had to suddenly wrap every single label change everywhere in dispatch.async.
ios swift multithreading user-interface grand-central-dispatch
I've been reading a lot of example code, i know that generally you want the code that updates your UI to execute on the main thread. And xcode will complain, at runtime, if i m missing something. Only sometimes though.
So how is code that updates your UI actually defined?
Is presenting an alert updating the UI? calling reloadData() on a UITableView? What about simply setting the text of a UILabel?
Especially with these three, i've been seeing and using it both ways in my app and can't really figure out a rule. Especially since xcode lets me get away with both... sometimes.
Edit: Ohh, i understand where my confusion came from. I thought it was random what thread you were on. But you are actually "always" on the main thread, unless you do something like a task, which runs on a background thread.
So the Main Thread Checker didn't actually let me "get away" with using reload() and changing labels in my viewController falsely. It's just that i was guaranteed to be on the main thread.
I thought i had to suddenly wrap every single label change everywhere in dispatch.async.
ios swift multithreading user-interface grand-central-dispatch
ios swift multithreading user-interface grand-central-dispatch
edited Nov 14 '18 at 11:54
DFENS
asked Nov 14 '18 at 7:57
DFENSDFENS
184
184
If something on the screen changes, it's updating the UI. Just about anything done with a class starting withUI
needs to be on the main queue. There are few exceptions.
– rmaddy
Nov 14 '18 at 8:00
User Interface, or UI, is basically any and everything that you see on screen/viewport. It can be a label. It can be a table view. It can be a button. Now if any of the values related to them are changing. For example. text of label, content of table view, color of button, its called updating the UI and Apple recommends it to be done on main thread. If not then it will make your app looked freezed up, delaying the changes that were intended to be immediate. This amounts to BAD User Experience.
– iOSer
Nov 14 '18 at 8:06
add a comment |
If something on the screen changes, it's updating the UI. Just about anything done with a class starting withUI
needs to be on the main queue. There are few exceptions.
– rmaddy
Nov 14 '18 at 8:00
User Interface, or UI, is basically any and everything that you see on screen/viewport. It can be a label. It can be a table view. It can be a button. Now if any of the values related to them are changing. For example. text of label, content of table view, color of button, its called updating the UI and Apple recommends it to be done on main thread. If not then it will make your app looked freezed up, delaying the changes that were intended to be immediate. This amounts to BAD User Experience.
– iOSer
Nov 14 '18 at 8:06
If something on the screen changes, it's updating the UI. Just about anything done with a class starting with
UI
needs to be on the main queue. There are few exceptions.– rmaddy
Nov 14 '18 at 8:00
If something on the screen changes, it's updating the UI. Just about anything done with a class starting with
UI
needs to be on the main queue. There are few exceptions.– rmaddy
Nov 14 '18 at 8:00
User Interface, or UI, is basically any and everything that you see on screen/viewport. It can be a label. It can be a table view. It can be a button. Now if any of the values related to them are changing. For example. text of label, content of table view, color of button, its called updating the UI and Apple recommends it to be done on main thread. If not then it will make your app looked freezed up, delaying the changes that were intended to be immediate. This amounts to BAD User Experience.
– iOSer
Nov 14 '18 at 8:06
User Interface, or UI, is basically any and everything that you see on screen/viewport. It can be a label. It can be a table view. It can be a button. Now if any of the values related to them are changing. For example. text of label, content of table view, color of button, its called updating the UI and Apple recommends it to be done on main thread. If not then it will make your app looked freezed up, delaying the changes that were intended to be immediate. This amounts to BAD User Experience.
– iOSer
Nov 14 '18 at 8:06
add a comment |
3 Answers
3
active
oldest
votes
Xcode letting you do something does not mean you should do that thing.
Reloading a table view, showing an alert, and changing a label's text are all UI updates. They all change what you see on screen, so they are UI updates. Always do these on the main thread.
Things that are not UI changes: sending HTTP requests, crunching numbers, loading data from some database. They don't change what you see on screen.
add a comment |
You must update your UI on main thread so that user can not feel some lag or disturbance during interacting with your UI. i.e. reload table, change text, change color, etc.
And you can perform your time taking operations on background thread so that your app performs well during the operations. i.e. HTTP requests, Database operations, long running loops, large nested conditions etc.
You can write your code like this....
// define these methods in your helper class or wherever you want.
public func BACKGROUND_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async(execute: codeBlock)
public func MAIN_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.main.async(execute: codeBlock)
and use like this.
func fetchData()
BACKGROUND_QUEUE
//your http request to get some text
let text = yourResult
MAIN_QUEUE
textLabel.text = text
add a comment |
The rule “update the UI only on the main thread” covers only part of the truth, better stick to what the UIKit documentation states:
Important
Use UIKit classes only from your app’s main thread or main
dispatch queue, unless otherwise indicated. This restriction
particularly applies to classes derived from UIResponder or that
involve manipulating your app’s user interface in any way.
This restriction applies not only to (visible) updates of what is presented to the user, but to any UIKit classes, unless otherwise specified.
As an example, accessing the applications delegate does not affect the
UI, but doing so on a non-main thread causes a “Main Thread Checker violation”:
DispatchQueue.global().async
let shared = UIApplication.shared.delegate
// Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295400%2fhow-ios-ui-update-code-defined-for-use-with-main-dispatch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Xcode letting you do something does not mean you should do that thing.
Reloading a table view, showing an alert, and changing a label's text are all UI updates. They all change what you see on screen, so they are UI updates. Always do these on the main thread.
Things that are not UI changes: sending HTTP requests, crunching numbers, loading data from some database. They don't change what you see on screen.
add a comment |
Xcode letting you do something does not mean you should do that thing.
Reloading a table view, showing an alert, and changing a label's text are all UI updates. They all change what you see on screen, so they are UI updates. Always do these on the main thread.
Things that are not UI changes: sending HTTP requests, crunching numbers, loading data from some database. They don't change what you see on screen.
add a comment |
Xcode letting you do something does not mean you should do that thing.
Reloading a table view, showing an alert, and changing a label's text are all UI updates. They all change what you see on screen, so they are UI updates. Always do these on the main thread.
Things that are not UI changes: sending HTTP requests, crunching numbers, loading data from some database. They don't change what you see on screen.
Xcode letting you do something does not mean you should do that thing.
Reloading a table view, showing an alert, and changing a label's text are all UI updates. They all change what you see on screen, so they are UI updates. Always do these on the main thread.
Things that are not UI changes: sending HTTP requests, crunching numbers, loading data from some database. They don't change what you see on screen.
answered Nov 14 '18 at 8:05
SweeperSweeper
69.2k1074140
69.2k1074140
add a comment |
add a comment |
You must update your UI on main thread so that user can not feel some lag or disturbance during interacting with your UI. i.e. reload table, change text, change color, etc.
And you can perform your time taking operations on background thread so that your app performs well during the operations. i.e. HTTP requests, Database operations, long running loops, large nested conditions etc.
You can write your code like this....
// define these methods in your helper class or wherever you want.
public func BACKGROUND_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async(execute: codeBlock)
public func MAIN_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.main.async(execute: codeBlock)
and use like this.
func fetchData()
BACKGROUND_QUEUE
//your http request to get some text
let text = yourResult
MAIN_QUEUE
textLabel.text = text
add a comment |
You must update your UI on main thread so that user can not feel some lag or disturbance during interacting with your UI. i.e. reload table, change text, change color, etc.
And you can perform your time taking operations on background thread so that your app performs well during the operations. i.e. HTTP requests, Database operations, long running loops, large nested conditions etc.
You can write your code like this....
// define these methods in your helper class or wherever you want.
public func BACKGROUND_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async(execute: codeBlock)
public func MAIN_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.main.async(execute: codeBlock)
and use like this.
func fetchData()
BACKGROUND_QUEUE
//your http request to get some text
let text = yourResult
MAIN_QUEUE
textLabel.text = text
add a comment |
You must update your UI on main thread so that user can not feel some lag or disturbance during interacting with your UI. i.e. reload table, change text, change color, etc.
And you can perform your time taking operations on background thread so that your app performs well during the operations. i.e. HTTP requests, Database operations, long running loops, large nested conditions etc.
You can write your code like this....
// define these methods in your helper class or wherever you want.
public func BACKGROUND_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async(execute: codeBlock)
public func MAIN_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.main.async(execute: codeBlock)
and use like this.
func fetchData()
BACKGROUND_QUEUE
//your http request to get some text
let text = yourResult
MAIN_QUEUE
textLabel.text = text
You must update your UI on main thread so that user can not feel some lag or disturbance during interacting with your UI. i.e. reload table, change text, change color, etc.
And you can perform your time taking operations on background thread so that your app performs well during the operations. i.e. HTTP requests, Database operations, long running loops, large nested conditions etc.
You can write your code like this....
// define these methods in your helper class or wherever you want.
public func BACKGROUND_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async(execute: codeBlock)
public func MAIN_QUEUE(_ codeBlock:@escaping (() -> Void)) -> Void
return DispatchQueue.main.async(execute: codeBlock)
and use like this.
func fetchData()
BACKGROUND_QUEUE
//your http request to get some text
let text = yourResult
MAIN_QUEUE
textLabel.text = text
answered Nov 14 '18 at 8:24
Avineet GuptaAvineet Gupta
519816
519816
add a comment |
add a comment |
The rule “update the UI only on the main thread” covers only part of the truth, better stick to what the UIKit documentation states:
Important
Use UIKit classes only from your app’s main thread or main
dispatch queue, unless otherwise indicated. This restriction
particularly applies to classes derived from UIResponder or that
involve manipulating your app’s user interface in any way.
This restriction applies not only to (visible) updates of what is presented to the user, but to any UIKit classes, unless otherwise specified.
As an example, accessing the applications delegate does not affect the
UI, but doing so on a non-main thread causes a “Main Thread Checker violation”:
DispatchQueue.global().async
let shared = UIApplication.shared.delegate
// Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
add a comment |
The rule “update the UI only on the main thread” covers only part of the truth, better stick to what the UIKit documentation states:
Important
Use UIKit classes only from your app’s main thread or main
dispatch queue, unless otherwise indicated. This restriction
particularly applies to classes derived from UIResponder or that
involve manipulating your app’s user interface in any way.
This restriction applies not only to (visible) updates of what is presented to the user, but to any UIKit classes, unless otherwise specified.
As an example, accessing the applications delegate does not affect the
UI, but doing so on a non-main thread causes a “Main Thread Checker violation”:
DispatchQueue.global().async
let shared = UIApplication.shared.delegate
// Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
add a comment |
The rule “update the UI only on the main thread” covers only part of the truth, better stick to what the UIKit documentation states:
Important
Use UIKit classes only from your app’s main thread or main
dispatch queue, unless otherwise indicated. This restriction
particularly applies to classes derived from UIResponder or that
involve manipulating your app’s user interface in any way.
This restriction applies not only to (visible) updates of what is presented to the user, but to any UIKit classes, unless otherwise specified.
As an example, accessing the applications delegate does not affect the
UI, but doing so on a non-main thread causes a “Main Thread Checker violation”:
DispatchQueue.global().async
let shared = UIApplication.shared.delegate
// Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
The rule “update the UI only on the main thread” covers only part of the truth, better stick to what the UIKit documentation states:
Important
Use UIKit classes only from your app’s main thread or main
dispatch queue, unless otherwise indicated. This restriction
particularly applies to classes derived from UIResponder or that
involve manipulating your app’s user interface in any way.
This restriction applies not only to (visible) updates of what is presented to the user, but to any UIKit classes, unless otherwise specified.
As an example, accessing the applications delegate does not affect the
UI, but doing so on a non-main thread causes a “Main Thread Checker violation”:
DispatchQueue.global().async
let shared = UIApplication.shared.delegate
// Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
edited Nov 14 '18 at 9:16
answered Nov 14 '18 at 8:50
Martin RMartin R
400k56880984
400k56880984
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295400%2fhow-ios-ui-update-code-defined-for-use-with-main-dispatch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If something on the screen changes, it's updating the UI. Just about anything done with a class starting with
UI
needs to be on the main queue. There are few exceptions.– rmaddy
Nov 14 '18 at 8:00
User Interface, or UI, is basically any and everything that you see on screen/viewport. It can be a label. It can be a table view. It can be a button. Now if any of the values related to them are changing. For example. text of label, content of table view, color of button, its called updating the UI and Apple recommends it to be done on main thread. If not then it will make your app looked freezed up, delaying the changes that were intended to be immediate. This amounts to BAD User Experience.
– iOSer
Nov 14 '18 at 8:06