-2

i'm making a bluetooth app and i want to put 2 labels to control if i'm connected and to display the RSSI so i have write this function

func updateLabel(){
    LRSSI.text = String(RSSINb)
    if(Connected == true){
        LEtat.text = "Connected"
        LEtat.textColor = UIColor.green
    }else{
        LEtat.text = "Disconnected"
        LEtat.textColor = UIColor.red
    }
}


I call this function inside my ReadRSSI function from CBPeripheral.
There is this text in my terminal

Main Thread Checker: UI API called on a background thread: -[UILabel setText:]

But when i rotate my phone it update labels, i tried to put 'self' before my labels.
I also tried to call my function inside a timer but it gives me a SIGBART error
So is there a method to update labels or to reload viewcontroller ?

EDIT : Call of my function:

 func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {
    print(RSSI)
    peripheral.readRSSI()

    RSSINb = Int(RSSI) * -1
    updateLabel()
    if(RSSINb > 150){
        WriteValue(valeur: "z")
    }
}

EDIT :

What's the proper way in architecture design to avoid UI API to be called on a background thread?

Rombond
  • 129
  • 2
  • 11
  • Show the function that calls `updateLabel`. Clearly you need to change it so it is called on the main queue. – rmaddy Mar 18 '19 at 01:13
  • i have edited it @rmaddy – Rombond Mar 18 '19 at 01:15
  • See https://stackoverflow.com/questions/47066259/whats-the-proper-way-in-architecture-design-to-avoid-ui-api-to-be-called-on-a-b and [other posts](https://stackoverflow.com/search?page=2&tab=Relevance&q=Main%20Thread%20Checker%3a%20UI%20API%20called%20on%20a%20background%20thread%20%5bswift%5d) related to the error. – rmaddy Mar 18 '19 at 01:16

1 Answers1

0

Problem Solved:

func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {
    print(RSSI)
    peripheral.readRSSI()

    RSSINb = Int(RSSI) * -1
    DispatchQueue.main.sync {
        updateLabel()
    }
    if(RSSINb > 150){
        WriteValue(valeur: "z")
    }
}
Rombond
  • 129
  • 2
  • 11