当应用程序从Firebase实时数据库接收数据时,如何显示UIActivityIndi​​cator

时间:2018-05-20 03:07:24

标签: ios swift firebase firebase-realtime-database uiactivityindicatorview

在从firebase实时数据库接收数据时,我没有找到任何显示UIActivityIndi​​cator的内容。到目前为止,我已经使用过此代码,但它对我没用。

{% assign sorted_posts = site.categories[category] | sort: post.title %}

1 个答案:

答案 0 :(得分:2)

您需要在firebase观察者回调范围外启动UIActivityIndi​​catorView动画,并在观察者返回快照后停止它。

var activityIndicator: UIActivityIndicatorView! 

override func viewDidLoad() {
  var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: 
   UIActivityIndicatorViewStyle.Gray) 
   activityIndicator.hidesWhenStopped = true; 
   activityIndicator.isHidden = true 
   activityIndicator.center = view.center;
   addSubview(myActivityIndicator)
   super.viewDidLoad()
}

func firebaseObserver() {
  // Show indicator 
  activityIndicator.isHidden = false
  activityIndicator.startAnimating()

  // Firebase network observer 
  let connectedRef = Database.database().reference(withPath: ".info/connected")
connectedRef.observe(.value, with: { snapshot in

   // Stop and hide indicator
   self.activityIndicator.stopAnimating()
   self.activityIndicator.isHidden = true

   // Proceed with other operations.
     })
}