Swift OS X disable/hide button in view controller if download is in progress from table view in separate classes

44 Views Asked by At

I have a table of products and each has a download button. Each button is its own NSTableCellView in a separate class. I want to disable/hide a button in the original Product View Controller class if a download is in progress. But whenever I try to do that my app crashes with very little error messaging as to why. Is there anyway to accomplish my goal?

let viewCon = ProductViewController()

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    
    let filesize: Int64 = Int64(passedLongBytes)!
    
    let percentage = CGFloat(totalBytesWritten) / CGFloat(filesize)
    
    DispatchQueue.main.async{
        self.shapeLayer.strokeEnd = percentage
        print("PERCENTAGE: \(Int(percentage * 100))%")
        
        if((Int(percentage * 100) < 100)){

            self.viewCon.backButtonOutlet.isHidden = true
            
        }else{
            
            self.viewCon.backButtonOutlet.isHidden = false
        }
        
    }
    
} 

I just get an error message everything "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"

1

There are 1 best solutions below

0
Nick On

So I cam up with an alternate solution where I created a global Int variable that checks the percentage and then I can use that global variable in my other ProductViewController. I don't know if this is the best way to do it but it seems to have achieved my goal.

CellView

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    
    let filesize: Int64 = Int64(passedLongBytes)!
    
    let percentage = CGFloat(totalBytesWritten) / CGFloat(filesize)
    
    DispatchQueue.main.async{
        self.shapeLayer.strokeEnd = percentage
        globalPercentage = (Int(percentage * 100))
        
    }
    
    
    
}

ProductviewController

@IBAction func backButtonClicked(_ sender: NSButton) {
    
    addFileInstall.removeAll()
    addFileUpdates.removeAll()
    
    print(globalPercentage)
    
    if(globalPercentage < 100){
        
        dialogOKCancel(text: "Wait for download to finish before closing this view")
        
    }else{
       
        self.view.window?.close()
        
    }

    
}

func dialogOKCancel(text: String) -> Bool {
    let alert = NSAlert()
    alert.informativeText = text
    alert.alertStyle = .warning
    alert.addButton(withTitle: "OK")
    return alert.runModal() == .alertFirstButtonReturn
}