iOS - UIActivityIndi​​catorView 没有停止

iOS - UIActivityIndicatorView not stopping

我在函数中启动 UIActivityIndi​​catorView 并希望在成功时停止它,由于某种原因它确实发生了:

func imageCaller(url: String , success: @escaping (UIImage) -> Void, failure: @escaping () -> Void) {
    self.imageLoaderIndicator.startAnimating()

    let handler = AuthenticateHandler()
     self.urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: handler, delegateQueue: OperationQueue.main)
    self.imageThumbnailTask = urlSession?.dataTask(with: URL(string:url)!) { data, res, err in

        if err != nil {
            guard let dataResponse = data,
                err == nil else {
                    print("error from dataResponse:\(err?.localizedDescription ?? "Response Error")")
                    return

        }

        if let imageData = data {
            if let image = UIImage(data: imageData) {
                success(image)
            }
        }
    }
    self.imageThumbnailTask?.resume() 
 }

以上代码为网络调用

这个是方法调用:

func imageThumbnailcall() {
    self.imageCaller( url: self.isShowingThermal ? self.thermalUrl : self.visualUrl, success: { (image) in
            self.imageLoaderIndicator.stopAnimating()
            self.backGroundImageView.image = image
            if self.isInVC {
            self.imageThumbnailcall()
            }
    }) {
        self.imageLoaderIndicator.stopAnimating()
    }
  } 

我也在故事板中检查了 "hide when stopped" 并尝试用 DispatchQueue.main.async {}

包装它

尝试一下;

func imageThumbnailcall() {
        self.imageCaller( url: self.isShowingThermal ? self.thermalUrl : self.visualUrl, success: { (image) in
                self.imageLoaderIndicator.stopAnimating()
                self.backGroundImageView.image = image
                if self.isInVC {
                self.imageThumbnailcall()
                }
        }) {
           self.imageLoaderIndicator.stopAnimating()
        }
      }

您的函数中有很多分支既不调用 success 也不调用 failure 块,您还需要停止 activity 指示器 failure回调。您还应该确保将所有 UI 相关的 activity 分派到主线程。

func imageCaller(url: String , success: @escaping (UIImage) -> Void, failure: @escaping () -> Void) {
    self.imageLoaderIndicator.startAnimating()

    let handler = AuthenticateHandler()
    self.urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: handler, delegateQueue: OperationQueue.main)
    self.imageThumbnailTask = urlSession?.dataTask(with: URL(string:url)!) { data, res, err in

        if err != nil {
            guard let dataResponse = data, err == nil else {
                print("error from dataResponse:\(err?.localizedDescription ?? "Response Error")")
                failure()
                return
            }
            do{
                //here dataResponse received from a network request
                let jsonResponse = try JSONSerialization.jsonObject(with: dataResponse, options: [])
                print("erro after parsing data:\(jsonResponse)") //Response result
                failure()
            } catch let parsingError {
                failure()
                print("Error", parsingError)
            }
        } else if let imageData = data, let image = UIImage(data: imageData) {
            success(image)
        } else {
            failure()
        }
    }
    self.imageThumbnailTask?.resume()
}

然后也从 failure 块调用 stopAnimating

func imageThumbnailcall() {
    self.imageCaller(url: self.isShowingThermal ? self.thermalUrl : self.visualUrl, success: { image in
        DispatchQueue.main.async{
            self.imageLoaderIndicator.stopAnimating()
            self.backGroundImageView.image = image
        }
        if self.isInVC {
            self.imageThumbnailcall()
        }
    }, failure: { _ in
        DispatchQueue.main.async{
            self.imageLoaderIndicator.stopAnimating()
        }
    })
}

不确定 isInVC 是什么或它是如何设置的,但是您根据它的值再次调用相同的方法,这可能会导致 imageThumbnailcall 从成功调用自身的无限循环完成处理程序。

URLSession.dataTask 的回调在后台队列中,所以

DispatchQueue.main.async { 
   self.imageLoaderIndicator.stopAnimating()
}

并确保处理成功和失败