0

I am trying to update a label to indicate status, but it is not being refreshed/shown as expected

func setStatusText(status: String) {
    statusText.stringValue = status;
}

func doSomething() {
    setStatusText(status: "Activating license"); // This is never shown
    var rc = PerformAction()

    if (rc == 0) {
        setStatusText(status: "\(action) succeeded") // Correctly displayed
    } else {
        setStatusText(status: "\(action) failed") // Correctly displayed
    }

The status update before the action is never being shown, but the status set right after the action is correctly displayed. What do I do to be sure the first status is shown?

Edited: I tried something else to try to isolate it. I added a button press, which will set text, sleep, and then set text again. I never see "first" in the status.

@IBAction func TestSettingText(_ sender: Any) {
    self.statusText.stringValue = "first";
    sleep(15)
    self.statusText.stringValue = "second";
}
  • What type of variable is statusText? – Matt Bart Jul 23 '19 at 20:29
  • Have you tried using the debugger, and putting a breakpoint on the `var rc =` line and the following line. Make sure that the label updates before continuing to the following line – Matt Bart Jul 23 '19 at 20:55
  • Yes, tried in the debugger and the status isn't updated until after that line – user2957533 Jul 23 '19 at 21:59
  • 1
    Possible duplicate of [Swift: Trying to update NSTextField in a loop, but it only gets the last value](https://stackoverflow.com/questions/24230517/swift-trying-to-update-nstextfield-in-a-loop-but-it-only-gets-the-last-value) – Willeke Jul 23 '19 at 22:19
  • I do not think this is a duplicate of that, although there may be some similarities. – user2957533 Jul 23 '19 at 23:02

1 Answers1

0

I don't know what exactly PerformAction does but it seems like it finishes quickly so you just can't see the Activating license status. It is there but only for a very brief moment and instantly changed to the next status

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161