如何在后端作业中实现纺车?
How to implement spinning wheel during a backend job?
我需要在后端作业中实现纺车。我在单独的 class.
中有我的后端作业
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,以便停止动画。或者如果在执行后端作业时有更好的启动/停止动画的方法(在单独的 class 中)请告诉我。
最好使用 MBProgressHUD 而不是 activityIndicator。
https://github.com/jdg/MBProgressHUD
显示 MBProgressHUD
let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.Indeterminate
loadingNotification.labelText = "Loading"
隐藏 MBProgressHUD
DispatchQueue.main.async { () -> Void in
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
})
您可以在单独的 class 中实现 show MBProgressHUD,从那里启动后端作业并在后端进程完成后隐藏代码。
如果您需要知道何时响应 returns 以便您可以停止加载程序,您需要向进行 Internet 调用的方法添加一个完成处理程序。您通常应该在大多数进行 Internet 调用的方法上都有完成处理程序,特别是如果您需要 UI 只有在收到响应后才会发生的事情。
所以对于 fetchRecord
函数:
我已将完成处理程序添加到此调用。最好你会在 @escaping
之后在这里传递一些东西(如果它是 JSON 响应,则类似于字典或数组),然后用另一种方法处理该响应。但是,如果您希望代码使用您在此处设置的线程处理此方法中的响应,那么我已经相应地编写了它。
func fetch_record(withCompletion comp: @escaping () ->()){
do{
DispatchQueue.global(qos: .userInitiated).async {
//code
DispatchQueue.main.async { () -> Void in
//code to process response from backend
//this tells whatever called this method that it is done
comp()
})
}
}
}
然后在您的视图控制器中调用 GetBackendRecordObj.fetch_record()
GetBackendRecordObj.fetch_record(withCompletion: { [weak self] () in
//when it hits this point, the process is done
self?.activityIndicator.stopAnimating()
}
将完成处理程序添加到 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()
}
我需要在后端作业中实现纺车。我在单独的 class.
中有我的后端作业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,以便停止动画。或者如果在执行后端作业时有更好的启动/停止动画的方法(在单独的 class 中)请告诉我。
最好使用 MBProgressHUD 而不是 activityIndicator。
https://github.com/jdg/MBProgressHUD
显示 MBProgressHUD
let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.Indeterminate
loadingNotification.labelText = "Loading"
隐藏 MBProgressHUD
DispatchQueue.main.async { () -> Void in
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
})
您可以在单独的 class 中实现 show MBProgressHUD,从那里启动后端作业并在后端进程完成后隐藏代码。
如果您需要知道何时响应 returns 以便您可以停止加载程序,您需要向进行 Internet 调用的方法添加一个完成处理程序。您通常应该在大多数进行 Internet 调用的方法上都有完成处理程序,特别是如果您需要 UI 只有在收到响应后才会发生的事情。
所以对于 fetchRecord
函数:
我已将完成处理程序添加到此调用。最好你会在 @escaping
之后在这里传递一些东西(如果它是 JSON 响应,则类似于字典或数组),然后用另一种方法处理该响应。但是,如果您希望代码使用您在此处设置的线程处理此方法中的响应,那么我已经相应地编写了它。
func fetch_record(withCompletion comp: @escaping () ->()){
do{
DispatchQueue.global(qos: .userInitiated).async {
//code
DispatchQueue.main.async { () -> Void in
//code to process response from backend
//this tells whatever called this method that it is done
comp()
})
}
}
}
然后在您的视图控制器中调用 GetBackendRecordObj.fetch_record()
GetBackendRecordObj.fetch_record(withCompletion: { [weak self] () in
//when it hits this point, the process is done
self?.activityIndicator.stopAnimating()
}
将完成处理程序添加到 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()
}