iOS-UIActivityIndi​​catorView没有停止

时间:2018-10-10 14:08:14

标签: ios swift uiactivityindicatorview

我正在使用func启动UIActivityIndi​​catorView,并希望成功停止它,出于某种原因它会发生:

2006-02-01 15:04:05.000000000 -0700 MST

上面的代码是网络呼叫。

这是方法调用:

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() 
 }

我也在情节提要中检查了“停止时隐藏”,还尝试使用DispatchQueue.main.async {}包裹它

3 个答案:

答案 0 :(得分:1)

您的函数中有很多分支没有调用successfailure块,并且还需要在failure回调中停止活动指示器。您还应该确保将所有与UI相关的活动调度到主线程。

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()
}

然后从stopAnimating块中调用failure

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的无限循环从成功完成处理程序中调用自身

答案 1 :(得分:0)

尝试;

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()
        }
      }

答案 2 :(得分:0)

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

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

并确保同时处理成功和失败