Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在集合视图firebase&;中为所有用户加载的帖子;敏捷的_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Swift 在集合视图firebase&;中为所有用户加载的帖子;敏捷的

Swift 在集合视图firebase&;中为所有用户加载的帖子;敏捷的,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我所要做的就是将当前用户的某个半径内的所有帖子获取到集合视图。目前,我正在获取该位置内所有当前用户的帖子,但仅此而已。我不知道如何将其转换为从所有用户获取所有帖子 fetchpostserids正在返回所有用户的快照,其中有UID geoFire查询仅从当前用户返回postId。我想不应该是这样 注意:更新的代码 var PostKey: String? var geoFire: GeoFire? var regionQuery: GFRegionQuery? var foundQuery: G

我所要做的就是将当前用户的某个半径内的所有帖子获取到集合视图。目前,我正在获取该位置内所有当前用户的帖子,但仅此而已。我不知道如何将其转换为从所有用户获取所有帖子

fetchpostserids
正在返回所有用户的快照,其中有
UID

geoFire查询仅从当前用户返回
postId
。我想不应该是这样

注意:更新的代码

var PostKey: String?
var geoFire: GeoFire?
var regionQuery: GFRegionQuery?
var foundQuery: GFCircleQuery?
var geoFireRef: DatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()

    guard let uid = Auth.auth().currentUser?.uid else { return }
    geoFireRef = Database.database().reference().child("posts").child(uid)
    geoFire = GeoFire(firebaseRef: geoFireRef)

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let userLocation: CLLocation = locations[0] as CLLocation
    let currentUserLocation = userLocation

    let circleQuery = geoFire?.query(at: currentUserLocation, withRadius: 100.0)
    _ = circleQuery?.observe(.keyEntered, with: { (key, location) in
        self.PostKey = key
        self.locationManager.stopUpdatingLocation()
    })
}

 fileprivate func fetchPostsWithUser(user: User) {

    guard let key = PostKey else { return }

    let ref = Database.database().reference().child("posts").child(user.uid).child(key)
    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        self.collectionView?.refreshControl?.endRefreshing()

        guard let dictionary = snapshot.value as? [String: Any] else { return }

        var post = Post(user: user, dictionary: dictionary)
        post.id = key

        self.posts.append(post)
        self.posts.sort(by: { (post1, post2) -> Bool in
            return post1.creationDate.compare(post2.creationDate) == .orderedDescending
        })
        self.collectionView?.reloadData()
    }) { (error) in
        print(error)
    }

}

fileprivate func fetchPostUserIds() {

    let ref = Database.database().reference().child("users")
    ref.observeSingleEvent(of: .value, with: { (snapshot) in

        guard let userIdKey = snapshot.value as? [String: Any] else { return }

        userIdKey.forEach({ (key, value) in
            Database.fetchUserWithUID(uid: key, completion: { (user) in
                self.fetchPostsWithUser(user: user)
            })
        })

    }) { (error) in
        print(error)
    }

}

您将参数表
user:user
传递给方法
fetchPostsWithUser
,但始终使用当前用户

guard let uid = Auth.auth().currentUser?.uid else { return }
还要注意这些

let ref = Database.database().reference().child("users")
ref.observe(.value, with: { (snapshot) in

将加载每个更改,因此请考虑singleObserve

尝试调试并查看函数中快照中的内容,以及fetchUserWithUID返回的内容

fileprivate func fetchPostUserIds() {

     let ref = Database.database().reference().child("users")
        ref.observeSingleEvent(of: .value, with: { (snapshot) in

            guard let userIdKey = snapshot.value as? [String: Any] else { return }

            userIdKey.forEach({ (key, value) in
            Database.fetchUserWithUID(uid: key, completion: { (user) in
                self.fetchPostsWithUser(user: user)
            })
        })

    }) { (error) in
        print(error)
    }

}

也许有更多的信息,我可以帮助您

我将
ref.observe
更改为
ref.observeSingleEvent
,但没有任何更改。你认为我把
uid
改成什么?你能解释一下你的问题吗?真是unclear@J.Doe仅获取当前用户的帖子。并非所有的用户都像我想要的那样。嗨,科迪,现在您已经添加了
userIDKey.forEach
并正在迭代
用户
引用的快照,你仍然有同样的问题吗?当我从
userIdKey.forEach
打印
键时,我得到了所有的用户Id'sOkay,所以它工作正常,下一步需要不同的用户,对吗?