UIActivityIndi​​cator需要几秒钟才能被解雇

时间:2018-06-16 17:06:50

标签: swift swift4 nsoperationqueue uiactivityindicatorview

我有一些代码使用OperationQueue执行一些后台工作,因此我在操作运行时使用UIAlertController来管理微调器。

问题是在调用dismiss()后,微调器在屏幕上说了很长时间。

下面我展示了我正在运行的代码,以及我在模拟器中运行代码时得到的输出。

此功能位于ViewController

@IBAction func runTheTest(_ sender: Any) {

  // MARK: Define web interaction parameters
  let config = URLSessionConfiguration.default
  config.waitsForConnectivity = true
  let defaultSession = URLSession(configuration: config)
  let url = URL(string: "https://www.google.com")
  var getUrlRequest = URLRequest(url: url!)
  getUrlRequest.httpMethod = "GET"

  // MARK: operations CREATION
  let getFormOp = GetFormOperation(getRequest: getUrlRequest, defaultSession: defaultSession, credentials: ["test":"test"], viewController: self)
  let finishOp = FinishOperation(viewController: self)

  // MARK: operations SET DEPENDENCIES
  finishOp.addDependency(getFormOp) // finish op is dependent on get op finishing
  getFormOp.defineFollowOnOperation(finishOp) // This would be if we needed to share data between the two

  // MARK: start the SPINNER
  LoaderController.sharedInstance.showLoader(viewController: self, title: "Please Wait...", message: "Getting data from the web")

  // MARK: initiate the long-running set of operations
  let operationQueue = OperationQueue()
  operationQueue.addOperations([getFormOp, finishOp], waitUntilFinished: false)

  print("operations are running")
}

这是管理活动指标的类:

class LoaderController: NSObject {

  static let sharedInstance = LoaderController()
  private let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
  private var start = DispatchTime.now()

  func showLoader(viewController: ViewController, title: String, message: String) {
    start = DispatchTime.now()
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = .gray
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    self.activityIndicator.startAnimating()
    alert.view.addSubview(activityIndicator)
    viewController.present(alert, animated: true, completion: nil)
    print("load indicator presented")
  }

  func removeLoader(viewController: ViewController){
    let removeCalled = DispatchTime.now()
    let spinnerSeconds =  (removeCalled.uptimeNanoseconds - start.uptimeNanoseconds) / 1000000000
    print("load indicator remove request after \(spinnerSeconds) seconds.")
    viewController.dismiss(animated: true, completion: {
      let finallyGone = DispatchTime.now()
      let spinnerSeconds =  (finallyGone.uptimeNanoseconds - removeCalled.uptimeNanoseconds) / 1000000000
      print("load indicator gone after \(spinnerSeconds) additional seconds.")
    })
  }
}

输出如下:

load indicator presented
operations are running
load indicator remove request after 6 seconds.
load indicator gone after 4 additional seconds.

我在模拟器中观察到的与print语句对齐;操作完成,但旋转器再保持4秒钟。

我在这里做错了什么,如何解决?

如果缺少您认为必要的任何属性或者删除问题,我会致力于改进此问题,所以请使用评论告诉我。

2 个答案:

答案 0 :(得分:1)

removeLoader,因为所有UI操作都必须仅从主线程调用。如果您从Operation(而非OperationQueue.main)调用该方法,则应该如下所示:

DispatchQueue.main.async {
   viewController.removeLoader(...)
}

该延迟几乎总是暗示您正在从后台队列执行UI操作。

在模拟器上调试时打开Main Thread Checker也是一个好主意。

答案 1 :(得分:0)

它会帮助你。试试这个

DispatchQueue.main.async {

   viewController.removeLoader()
//call your removeLoader method inside the asynchronous queue

}