2016-04-21 10 views
0

Ich arbeite gerade an einer Upload-Funktion in einer Swift iPhone App.Tracking totalBytesSent blockiert Animationen für ein paar Sekunden

Während des Uploads möchte ich eine Fortschrittsanzeige anzeigen. Ein Code-Befehl, der die zu aktualisierende Anzeige benötigt, wird jedoch nicht vor einigen Sekunden ausgeführt.

Zum Beispiel würde PROGRESS mit dem folgenden Code sofort in der Konsole erscheinen, wenn Daten gesendet werden würden, aber die Draufsicht würde erst nach etwa 10 Sekunden verschwinden.

func URLSession(_ session: NSURLSession, 
        task task: NSURLSessionTask, 
         didSendBodyData bytesSent: Int64, 
             totalBytesSent totalBytesSent: Int64, 
                 totalBytesExpectedToSend totalBytesExpectedToSend: Int64) { 
    //uploadProgressCallback(uploadProgressRatio: min(Double(totalBytesSent)/self.uploadedFileTotalSize, 1.0)) 

    print("PROGRESS") 
    let topController:UINavigationController = UIApplication.sharedApplication().keyWindow!.rootViewController as! UINavigationController 
    topController.view.alpha = 0.0 


} 

Irgendeine Idee warum?

Vielen Dank im Voraus für jede Hilfe.

Raphaël

Antwort

0

Update UI nur vom Hauptthread effiziente Leistung haben:

dispatch_async(dispatch_get_main_queue()) { [unowned self] in 
    //update UI hear 
} 
+1

Oder erstellen Sie eigene 'NSURLSession' die Delegatmethoden auf Hauptwarteschlange ruft:' session = NSURLSession (Konfiguration lassen: NSURLSessionConfiguration .defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue()) ' –

+0

Vielen Dank Alexander! Das scheint der Trick zu sein! – user1875631

Verwandte Themen