Swift Spinner disappearing before queue task finished

This is doing my head in. I a trying to show a spinner and then remove it after a task has been performed.

Unfortunately the spinner remove code executes before the task is completed. Any help would be appreciated.

DispatchQueue.global(qos: .utility).async { [weak self] in                              // Create a new queue for our networking activity             guard let self = self else {                 return             }              switch airframe{             case .A_10:                 dcsBIOSConn.configureHawgWaypoint(name: name, lat: latitude!, long: longitude!, elev: elev, tot: timeOnTarget)             case .F_16:                 dcsBIOSConn.configureWaypoint(lat: latitude!, long: longitude!, elev: elev, aircraft: airframe!)             case .F_18:                 dcsBIOSConn.configureWaypoint(lat: latitude!, long: longitude!, elev: elev, aircraft: airframe!)             case .AV_8B:                 dcsBIOSConn.configureWaypoint(lat: latitude!, long: longitude!, elev: elev, aircraft: airframe!)             default:                 break             }              dcsBIOSConn.printMessageQueue()             print("1. Entering Queue")              let biosQueue = DispatchQueue(label: "dcs_bios")              biosQueue.async {                 dcsBIOSConn.sendMessageQueueToDCSBIOS()             }              print("2. Finishing Out of Loop")              // Send the designated coord to the CDU radio.             DispatchQueue.main.async { [weak self] in                                           // Update the spinner on the main thread.                 self?.removeSpinner()                 //ERProgressHud.sharedInstance.hide()                 print("3. Remove Spinner")             }         } 

Yields this result

AAP_PAGE CDU_CLR CDU_LSK_7R CDU_Q CDU_E CDU_S CDU_H CDU_M CDU_LSK_3R CDU_N CDU_2 CDU_6 CDU_4 CDU_5 CDU_1 CDU_0 CDU_0 CDU_LSK_7L CDU_E CDU_0 CDU_5 CDU_5 CDU_5 CDU_3 CDU_4 CDU_5 CDU_0 CDU_LSK_9L CDU_LSK_5L CDU_LSK_5R 1. Entering Queue 2. Finishing Out of Loop 3. Remove Spinner State: Preparing  Objects sent: 0 String was sent to UDP: AAP_PAGE 0  String was sent to UDP: CDU_CLR 1  String was sent to UDP: CDU_CLR 0  String was sent to UDP: CDU_LSK_7R 1  String was sent to UDP: CDU_LSK_7R 0  String was sent to UDP: CDU_Q 1  String was sent to UDP: CDU_Q 0  String was sent to UDP: CDU_E 1 

The text labelled 1,2,3 all execute before the task starts sending text. The 3. removing text spinner should be last. Thoughts?

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.