Camille Basbous
Camille Basbous

Reputation: 397

didSet thread error on data retrieval, why is called function not fully executing?

I am testing a situation that some users might face where they might open the app without an internet connection, and at some point, while using the app the connection reappears and thus I would like for them to retrieve their data. The data is retrieved from the backend, it is appended to the array but the function that needs to follow is strangely not fully executed and I get a thread error. Below are the concerned chunk of code:

the Array in question with didSet and called function:

var customButtons:[CustomColors]=[

        CustomColors(color: UIColor(red: 1, green: 1, blue: 1, alpha: 1 )),
        CustomColors(color: UIColor(red: 247/255, green: 214/255, blue: 217/255, alpha: 1 )),
]{
   didSet {
       if(customButtons[customButtons.count-2].nature.frame.minX == 0){
           print("bugged")
           sortCColor()
       }
  }
}

the fetch request and connection detector on viewDidLoad():

 override func viewDidLoad() {

        Net.shared.monitorNet(){ [self]value in
            if(value == "connected"){
                connection = true
                print("on")
                initFetchRequests()
                
                // send  avatar and other reqs add nested conditional to avoid more reloads
            } else {
                print("none")
                connection = false
            }
    }
        setupScene()

       print("loaded")
    }

The function to run:

func sortCColor()->Void{
        for (i, val) in customButtons.enumerated(){
            val.nature.tag = i
            let xDeduct = 6*Int(floor(CGFloat(Int(val.nature.tag)/6)))
            let width = Float(bounds.size.width) > 450 ? CGFloat(Int(0.10*Double(colorScrollW))) : CGFloat(Int(0.15*Double(colorScrollW)))
            let x = CGFloat(0.0073*bounds.size.width) + CGFloat((colorScrollW+Int(width*0.2))*(i - xDeduct))*0.15 // -6*floor
            let y = CGFloat(0.0073*bounds.size.width)
            
            if(val.nature.tag == customButtons.count - 1){
                val.nature.setImage(UIImage(named: "art.scnassets/addColor.png")?.resized(to: CGSize(width: CGFloat(width), height: CGFloat(width))), for: .normal)
            }
            
            else{val.nature.backgroundColor = val.color}
            
            val.nature.frame = CGRect(x: x , y: y + y*floor(CGFloat(Int(val.nature.tag)/6)) + width*floor(CGFloat(Int(val.nature.tag)/6)), width: width, height: width)
            // divisble by 7 for y and use the count for x and dont forget conatainer and scroll extra space
            val.nature.layer.cornerRadius = 0.5 * width
            

            val.nature.addTarget(self, action: #selector(chooseCustomColor), for: UIControl.Event.touchDown)
            
            val.nature.addTarget(self, action: #selector(buttonOut), for: UIControl.Event.touchUpInside)
            val.nature.addTarget(self, action: #selector(buttonOut), for: UIControl.Event.touchDragExit)
           
            colorContainer.addSubview(val.nature)
        }
    }
    

thanks a lot for your time and effort

Upvotes: 0

Views: 53

Answers (0)

Related Questions