【发布时间】:2018-05-05 10:19:47
【问题描述】:
我在加载屏幕时遇到了这个问题,我启动了一个活动指示器并调用了一个函数来获取数据。起初显示活动但从未开始,然后我在不同的线程中运行每个调用(开始活动和获取数据)。动画有效,但只有一次,当我尝试再次加载屏幕时,它再次卡住,直到加载数据。 这是 viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
//indicator
indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
indicator.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
indicator.color = commons.getButtonColor()
indicator.center = self.view.center
indicator.hidesWhenStopped = true
self.view.addSubview(indicator)
DispatchQueue.main.async {
print("We finished that.")
// only back on the main thread, may you access UI:
self.indicator.startAnimating()
}
//connect to database
commons.ref = Database.database().reference()
//table view
tableV.dataSource = self
tableV.delegate = self
tableV.tableFooterView = UIView()
tableV.separatorStyle = .none
tableV.allowsSelection = false
//change left btn
let leftButtonItem = UIBarButtonItem.init(
title: "",
style: .done,
target: self,
action: #selector(backButtonPressed)
)
leftButtonItem.image = UIImage(named:"back")
leftButtonItem.tintColor = commons.getButtonColor()
self.navigationItem.leftBarButtonItem = leftButtonItem
//style
self.title = flag
DispatchQueue.main.async {
//get values
if(self.flag == "Accounts"){
self.color = "d0f0dd"
self.getAccounts()
}else if(self.flag == "Loans"){
self.color = "f8ff90"
self.getLoans()
}else if(self.flag == "Cards"){
self.color = "ffa669"
self.getCards()
}
}
}
卡屏时的截图:
编辑:获取数据的函数:
func getAccounts(){
commons.ref.child("Accounts").observeSingleEvent(of: .value, with: { (snapshot) in
for item in snapshot.children {
self.values.append(item as! DataSnapshot)
}
self.tableV.reloadData()
self.indicator.stopAnimating()
}) { (error) in
print(error.localizedDescription)
}
}
func getCards(){
commons.ref.child("Cards").observeSingleEvent(of: .value, with: { (snapshot) in
self.indicator.stopAnimating()
for item in snapshot.children {
self.values.append(item as! DataSnapshot)
}
self.tableV.reloadData()
}) { (error) in
print(error.localizedDescription)
}
}
func getLoans(){
commons.ref.child("Loans").observeSingleEvent(of: .value, with: { (snapshot) in
self.indicator.stopAnimating()
for item in snapshot.children {
self.values.append(item as! DataSnapshot)
}
self.tableV.reloadData()
}) { (error) in
print(error.localizedDescription)
}
}
【问题讨论】:
-
你从来没有打电话给
self.indicator.stopAnimating(),那为什么活动指示器会消失呢? -
哦,是的,我在调用它,在我获取数据的函数中,这不是出现/消失的问题,指示器根本没有 stsrt 动画,页面卡住直到加载数据那么一切都很好。
-
这表明你在某处阻塞了主线程。请包括所有相关功能,因为在您问题的代码中似乎没有任何问题可能导致您的问题。
-
我猜你可能从主线程中的 Web 服务获取数据。在数据到达之前,用户界面将被冻结。使用获取数据的代码更新您的问题。确保将服务分派到非主线程。 @yasserh
-
问题已更新。
标签: ios iphone swift animation activity-indicator