ios - 如何在后端作业中实现旋转轮?

标签 ios swift grand-central-dispatch

我需要在后端工作期间实现旋转轮。我的后端工作在一个单独的类中。

class ViewControllerA: UITableViewController {

  // Code
  var GetBackendRecordObj = GetBackendRecord(initparam:param);

  // CODE TO START ANIMATION (SPINNING WHEEL)  

  self.view.addSubview(self.activityIndicator)
  self.activityIndicator.bringSubview(toFront: self.view)
  self.activityIndicator.startAnimating()

  // CODE TO CALL THE BACKEND IS IN ANOTHER CLASS         
  GetBackendRecordObj.fetch_record()


}



class GetBackendRecord{

  var transaction_id: String = ""
  var current_email_id: String = ""

  init(initparam: String) {
    self.initparam = initparam

  }

func fetch_record (){

    do{
        DispatchQueue.global(qos: .userInitiated).async {
            //code
            DispatchQueue.main.async { () -> Void in
                //code to process response from backend
                // NEED CODE TO STOP ANIMATION (SPINNING WHEEL)THAT WAS STARTED IN VIEWCONTROLLERA
            })
        }
   }
}              

后端调用完成后如何访问 UITableViewcontroller,以便停止动画。或者如果在执行后端作业(在单独的类中)时有更好的方法来启动/停止动画,请告诉我。

最佳答案

fetch_record 添加完成处理程序:

func fetch_record(_ completionHandler: @escaping () -> Swift.Void) {
    do{
        DispatchQueue.global(qos: .userInitiated).async {
            //code
            DispatchQueue.main.async { () -> Void in
                //code to process response from backend
                completionHandler()
            })
        }
   }
}  

ViewController 中调用它时,您可以指定完成后要执行的操作:

GetBackendRecordObj.fetch_record() {
    self.activityIndicator.stopAnimating()
}

关于ios - 如何在后端作业中实现旋转轮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40914480/

相关文章:

ios - 如何获得 subview 在我的 UITableViewCell 中相对于整个窗口的位置?

ios - Swift 3 - 如何提高 Tesseract 的图像质量?

ios - 将 Apple Watch 添加到具有部署目标 IOS 7 的 iOS 应用程序

ios - 为什么堆栈 View 中的自定义 View 重叠?

crash - 使用 Swift 的间歇性 iPad 应用程序崩溃

swift - For 循环中的 DispatchGroup

iphone - UIActionSheet 在 UIPopoverController 中有 20 多个选项

swift - 如果 nameTextField.text!.isEmpty 不起作用?

ios - 使用 GMS 的 Swift map 样式

objective-c - iPhone : Using dispatch_after to mimick NSTimer