如何在Firebase中不实时加载数据?

时间:2019-01-16 10:48:57

标签: swift firebase firebase-realtime-database

我要解决以下问题:

enter image description here

所有数据都是实时加载的(这次每个帖子甚至多次加载),但是我只想使用已有的刷新器进行刷新。

这是我的复习:

Set-Cookie: SyncGatewaySession=xxxx; Path=/bucketname; Expires=Fri, 18 Jan 2019 08:50:36 GMT

与此同时,我加载了所有帖子:

// Refresher
func refresh() {
    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Aktualisieren")
    refresher.addTarget(self, action: #selector(DiscoveryViewController.refreshData) , for: UIControl.Event.valueChanged)
    tableView.addSubview(refresher)
}

@objc func refreshData(sender: Any) {
    loadTopPosts()
    refresher.endRefreshing()
}

这是我从Firebase上提供数据的函数:

func loadTopPosts() {
    ProgressHUD.show("Lade...", interaction: false)
    self.postArray.removeAll()
    self.tableView.reloadData()

    // Aktuelle Location des aktuell eingeloggten Users laden
    guard let currentUserUid = UserApi.shared.CURRENT_USER_ID else { return }
    let databaseRef = LocationApi.shared.geoRef
    databaseRef.getLocationForKey(currentUserUid) { (location, error) in
        if error != nil {
            ProgressHUD.showError("Posts konnten nicht geladen werden")
        } else if location != nil {
            print("Location for \(currentUserUid) is [\(location!.coordinate.latitude), \(location!.coordinate.longitude)]")

            // Alle Posts im vorgegebenen Umkreis laden
            let REF_GEO_POSTS = Database.database().reference().child("geolocation_posts")
            let geoRef = GeoFire(firebaseRef: REF_GEO_POSTS)
            // Lade den aktuell eingestellten Radius aus der Datenbank
            self.observeRadius(completion: { (radius) in
                let currentRadius = radius
            // Üperprüfe, welche Posts im Umkreis erstellt wurden
                let circleQuery = geoRef.query(at: location!, withRadius: Double(currentRadius)!)

            circleQuery.observe(.keyEntered, with: { (postIds, location) in

                self.observePost(withPostId: postIds, completion: { (posts) in
                    guard let userUid = posts.uid else { return }
                    self.observeUser(uid: userUid, completion: { (users) in
                        let postArray = UserPostModel(post: posts, user: users)
                        self.postArray.append(postArray)
                        self.postArray.sort(by: {$0.post!.secondsFrom1970! > $1.post!.secondsFrom1970!})
                        self.tableView.reloadData()
                        self.tableView.setContentOffset(CGPoint.zero, animated: true)
                        ProgressHUD.dismiss()

                    })
                })

                })
            })
            if self.postArray.count == 0 {
                ProgressHUD.dismiss()
            }
        } else {
            ProgressHUD.showError("Posts konnten nicht geladen werden")
        }
    }
}

我现在想做的是禁用实时功能(仅当刷新时才更新tableView)。因此,如果我刷新,一切都会正确显示。

如何解决这个问题?

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

observe,它每次在数据库中发生更新时都会通知。还有observeSingleEvent,它将仅在需要时为您提供数据。

https://firebase.google.com/docs/database/ios/read-and-write

此外,您的geofire参考信息将不断通知您每次更新,如果您不希望这样做,请按以下步骤删除它:

“如果您不希望在初始查询后获得有关新用户/移动用户的更新,这也是通过致电removeObserverWithFirebaseHandleremoveAllObservers删除观察者的好时机。”

在您的情况下是帖子,在这里提到: https://stackoverflow.com/a/50722984/8869493