osx - Asynchronous dispatch resource hog in Swift -


i new swift programming language, making guis, , using asynchronous dispatches in general, please forgive question — i'd imagine — has excruciatingly simple answer.

what attempting disable , re-enable next button in gui based on whether or not valid settings have been selected. these settings found in 2 checkboxes , text field.

my idea have process in background continually checking settings , — if valid — enable next button. code block recent attempt:

func checkvalidsettings() {     let priority = dispatch_queue_priority_default     dispatch_async(dispatch_get_global_queue(priority, 0)) {         while true {             if self.textfield.stringvalue == "test" {                 dispatch_async(dispatch_get_main_queue()) {                     self.nextbutton.enabled = false                 }             } else if (self.checkbox1.state == nsoffstate && self.checkbox2.state == nsoffstate){                 dispatch_async(dispatch_get_main_queue()) {                     self.nextbutton.enabled = false                 }             } else {                 dispatch_async(dispatch_get_main_queue()) {                     self.nextbutton.enabled = true                 }             }         }     } } 

though runs correctly, causes application huge resource hog (100% cpu , several gbs of memory), assume because ton of background tasks being created. messed somewhere pretty badly, haha.

my question this: how should structure while loop continually checks settings while not commandeering of resources? or there simpler way of gui management?

thanks in advance advice or can give!

in ios don't have create own listeners. can assign ibaction , bools.

@iboutlet weak var field1: uitextfield! @iboutlet weak var field2: uitextfield!  var fieldcheck1: bool = false var fieldcheck2: bool = false  @ibaction func field1changed(sender: anyobject) {     if field1.text == "true statement" {         fieldcheck1 = true     }     self.checkcomplete() }   @ibaction func field2changed(sender: anyobject) {     if field2.text == "true statement" {         fieldcheck2 = true     }     self.checkcomplete() }  func checkcomplete() {     if fieldcheck1 == true && fieldcheck2 == true{         // enable next button     } } 

if interests you, can use monitored properties check if property changed value , run code based off that:

var mybool: bool = false {      willset(newvalue) {           if newvalue == true {                 // run code bool true           }      } } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -