-1

Using Xcode 10.1 running the app on mini iPad OS 12.1.1

I am posting a notification to NotificationCenter and the handler function updates UILabel setupProgress to show the progress of a data import.

This used to work fine but has recently stopped working, very possibly as a result of something I have done, but I can't think what.

PM in the code is a function which prints to the console - and it tells me that self.setupProgress.text is, in fact, set correctly and changes as expected as the data loads, however the corresponding UILabel does not update.

The initial UILabel text is set like this

if !self.update_type.isEmpty && self.update_type == "setup_import" {
    self.setupProgress.text = "I've told the server that this is a clean install"
}

and that works fine - but then, as the import progresses, in the handler function (below) I get no updates until import_progress == "And now the end is upon us ..." at which point the UILabel updates correctly and everything carries on as expected.

func handleImportNotification(_ notification:Notification) {

    self.setupProgress.text = import_progress

    // should show something like 
    // `Importing F4RES : 0 of : 1395` 
    // `Importing F4RES : 500 of : 1395`
    // etc...

    PM(#line, function: #function, str1: self.setupProgress.text)
    // prints the correct updates to the console

    if import_progress == "And now the end is upon us ..." {
        self.makeShopOptions()
        self.loadingImage.stopAnimating()
        self.performSegue(withIdentifier: "setup_to_splash", sender: self)
    }
}

The app continues to work just fine - just without the expected UILabel updates in between.

Thanks in advance for any ideas.

toberblue
  • 23
  • 3
  • I don't get your problem. You said you update the text, and 2 lines later the label updates correctly. There are not even long running tasks in between the 2 lines. It is almost the same as update immediately. What's the problem? Though it is expected that UI update takes some times. If you make some UI update calls and want to make sure some other codes execute after the UI update, you should put them in `DispatchQueue.main.async{}`. – Ricky Mo Jan 31 '19 at 09:49
  • Sorry I may be over-complicating - the issue is that `self.setupProgress.text` contains the correct value and I can print it to the console but the UILabel `setupProgress` does not update – toberblue Jan 31 '19 at 12:38
  • But you said that when it reaches `if import_progress == "And now the end is upon us ..."`, the label updates correctly. Does it really matter that the updates delay just 2 lines of code? UI update DOES take time. – Ricky Mo Feb 01 '19 at 01:20
  • There are many calls to this function where import_progress has a different value and so skips the if block in question `if import_progress == "And now the end is upon us ..." {` – toberblue Feb 02 '19 at 02:06

1 Answers1

0

You are posting a notification to the Notification Center. Would you mind showing how and when?

Assuming you're using NotificationCenter.default.post notificationName: method, the handler should take an argument of type Notification. Otherwise it won't respond to notification updates.

The handler function should look like: func handleImportNotification(notification: Notification) { ... }

And the observer:

var observer: NSObjectProtocol?

func someFunction() {
    observer = NotificationCenter.default.addObserver(forName: object: queue: using: { (Notification) in
        //Update UILabel or call the associated function
    }))
}

Try this.

  • Posting like this `let nc = NotificationCenter.default nc.post(name: Notification.Name("CompleteImportNotification"), object: nil)` – toberblue Jan 31 '19 at 12:39