Posts being loaded for all users in collection view firebase & swift












6















All I'm trying to do is fetch all the posts to the collection view that are within a certain radius of the current user. Currently I'm getting all the current users posts within the location but that is all. I can't figure out how to convert it to fetch all the posts from all the users.



FetchPostUserIds Is returning a snapshot of all the users and there UID



The geoFire query is only returning the postId from the current user. It shouldn't be I assume



Note: Updated Code



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)
}

}









share|improve this question

























  • Can you explain your problem more? It is really unclear

    – J. Doe
    Nov 26 '18 at 9:26











  • @J.Doe Only the current users post is being fetched. Not all the users like I want.

    – Cody Host
    Nov 26 '18 at 12:43











  • Hi Cody, now that you have added the userIDKey.forEach and are iterating through the snapshot of the users reference, are you still having the same problem?

    – hackerman58888
    Nov 29 '18 at 14:55
















6















All I'm trying to do is fetch all the posts to the collection view that are within a certain radius of the current user. Currently I'm getting all the current users posts within the location but that is all. I can't figure out how to convert it to fetch all the posts from all the users.



FetchPostUserIds Is returning a snapshot of all the users and there UID



The geoFire query is only returning the postId from the current user. It shouldn't be I assume



Note: Updated Code



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)
}

}









share|improve this question

























  • Can you explain your problem more? It is really unclear

    – J. Doe
    Nov 26 '18 at 9:26











  • @J.Doe Only the current users post is being fetched. Not all the users like I want.

    – Cody Host
    Nov 26 '18 at 12:43











  • Hi Cody, now that you have added the userIDKey.forEach and are iterating through the snapshot of the users reference, are you still having the same problem?

    – hackerman58888
    Nov 29 '18 at 14:55














6












6








6


3






All I'm trying to do is fetch all the posts to the collection view that are within a certain radius of the current user. Currently I'm getting all the current users posts within the location but that is all. I can't figure out how to convert it to fetch all the posts from all the users.



FetchPostUserIds Is returning a snapshot of all the users and there UID



The geoFire query is only returning the postId from the current user. It shouldn't be I assume



Note: Updated Code



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)
}

}









share|improve this question
















All I'm trying to do is fetch all the posts to the collection view that are within a certain radius of the current user. Currently I'm getting all the current users posts within the location but that is all. I can't figure out how to convert it to fetch all the posts from all the users.



FetchPostUserIds Is returning a snapshot of all the users and there UID



The geoFire query is only returning the postId from the current user. It shouldn't be I assume



Note: Updated Code



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)
}

}






swift firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 0:01







Cody Host

















asked Nov 23 '18 at 13:02









Cody HostCody Host

6810




6810













  • Can you explain your problem more? It is really unclear

    – J. Doe
    Nov 26 '18 at 9:26











  • @J.Doe Only the current users post is being fetched. Not all the users like I want.

    – Cody Host
    Nov 26 '18 at 12:43











  • Hi Cody, now that you have added the userIDKey.forEach and are iterating through the snapshot of the users reference, are you still having the same problem?

    – hackerman58888
    Nov 29 '18 at 14:55



















  • Can you explain your problem more? It is really unclear

    – J. Doe
    Nov 26 '18 at 9:26











  • @J.Doe Only the current users post is being fetched. Not all the users like I want.

    – Cody Host
    Nov 26 '18 at 12:43











  • Hi Cody, now that you have added the userIDKey.forEach and are iterating through the snapshot of the users reference, are you still having the same problem?

    – hackerman58888
    Nov 29 '18 at 14:55

















Can you explain your problem more? It is really unclear

– J. Doe
Nov 26 '18 at 9:26





Can you explain your problem more? It is really unclear

– J. Doe
Nov 26 '18 at 9:26













@J.Doe Only the current users post is being fetched. Not all the users like I want.

– Cody Host
Nov 26 '18 at 12:43





@J.Doe Only the current users post is being fetched. Not all the users like I want.

– Cody Host
Nov 26 '18 at 12:43













Hi Cody, now that you have added the userIDKey.forEach and are iterating through the snapshot of the users reference, are you still having the same problem?

– hackerman58888
Nov 29 '18 at 14:55





Hi Cody, now that you have added the userIDKey.forEach and are iterating through the snapshot of the users reference, are you still having the same problem?

– hackerman58888
Nov 29 '18 at 14:55












2 Answers
2






active

oldest

votes


















3





+25









Try to debug and look what you got in the snapshot in your function, and also what fetchUserWithUID return



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)
}

}


Maybe with some more information I can help you






share|improve this answer
























  • When I print the Key from userIdKey.forEach I get all user Id's

    – Cody Host
    Nov 26 '18 at 23:33













  • Okay so it works good and it takes different user in the next step right?

    – Francesco Destino
    Nov 27 '18 at 8:17



















1














You pass paremeter user: User to method fetchPostsWithUser, but you always use the current user



guard let uid = Auth.auth().currentUser?.uid else { return }


also note these



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


will load every change so think about singleObserve






share|improve this answer
























  • I change ref.observe to ref.observeSingleEvent but nothing changed. What do you suppose I change the uid to?

    – Cody Host
    Nov 25 '18 at 23:20













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447224%2fposts-being-loaded-for-all-users-in-collection-view-firebase-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3





+25









Try to debug and look what you got in the snapshot in your function, and also what fetchUserWithUID return



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)
}

}


Maybe with some more information I can help you






share|improve this answer
























  • When I print the Key from userIdKey.forEach I get all user Id's

    – Cody Host
    Nov 26 '18 at 23:33













  • Okay so it works good and it takes different user in the next step right?

    – Francesco Destino
    Nov 27 '18 at 8:17
















3





+25









Try to debug and look what you got in the snapshot in your function, and also what fetchUserWithUID return



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)
}

}


Maybe with some more information I can help you






share|improve this answer
























  • When I print the Key from userIdKey.forEach I get all user Id's

    – Cody Host
    Nov 26 '18 at 23:33













  • Okay so it works good and it takes different user in the next step right?

    – Francesco Destino
    Nov 27 '18 at 8:17














3





+25







3





+25



3




+25





Try to debug and look what you got in the snapshot in your function, and also what fetchUserWithUID return



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)
}

}


Maybe with some more information I can help you






share|improve this answer













Try to debug and look what you got in the snapshot in your function, and also what fetchUserWithUID return



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)
}

}


Maybe with some more information I can help you







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 13:58









Francesco DestinoFrancesco Destino

24111




24111













  • When I print the Key from userIdKey.forEach I get all user Id's

    – Cody Host
    Nov 26 '18 at 23:33













  • Okay so it works good and it takes different user in the next step right?

    – Francesco Destino
    Nov 27 '18 at 8:17



















  • When I print the Key from userIdKey.forEach I get all user Id's

    – Cody Host
    Nov 26 '18 at 23:33













  • Okay so it works good and it takes different user in the next step right?

    – Francesco Destino
    Nov 27 '18 at 8:17

















When I print the Key from userIdKey.forEach I get all user Id's

– Cody Host
Nov 26 '18 at 23:33







When I print the Key from userIdKey.forEach I get all user Id's

– Cody Host
Nov 26 '18 at 23:33















Okay so it works good and it takes different user in the next step right?

– Francesco Destino
Nov 27 '18 at 8:17





Okay so it works good and it takes different user in the next step right?

– Francesco Destino
Nov 27 '18 at 8:17













1














You pass paremeter user: User to method fetchPostsWithUser, but you always use the current user



guard let uid = Auth.auth().currentUser?.uid else { return }


also note these



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


will load every change so think about singleObserve






share|improve this answer
























  • I change ref.observe to ref.observeSingleEvent but nothing changed. What do you suppose I change the uid to?

    – Cody Host
    Nov 25 '18 at 23:20


















1














You pass paremeter user: User to method fetchPostsWithUser, but you always use the current user



guard let uid = Auth.auth().currentUser?.uid else { return }


also note these



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


will load every change so think about singleObserve






share|improve this answer
























  • I change ref.observe to ref.observeSingleEvent but nothing changed. What do you suppose I change the uid to?

    – Cody Host
    Nov 25 '18 at 23:20
















1












1








1







You pass paremeter user: User to method fetchPostsWithUser, but you always use the current user



guard let uid = Auth.auth().currentUser?.uid else { return }


also note these



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


will load every change so think about singleObserve






share|improve this answer













You pass paremeter user: User to method fetchPostsWithUser, but you always use the current user



guard let uid = Auth.auth().currentUser?.uid else { return }


also note these



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


will load every change so think about singleObserve







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 13:11









Sh_KhanSh_Khan

40.4k51125




40.4k51125













  • I change ref.observe to ref.observeSingleEvent but nothing changed. What do you suppose I change the uid to?

    – Cody Host
    Nov 25 '18 at 23:20





















  • I change ref.observe to ref.observeSingleEvent but nothing changed. What do you suppose I change the uid to?

    – Cody Host
    Nov 25 '18 at 23:20



















I change ref.observe to ref.observeSingleEvent but nothing changed. What do you suppose I change the uid to?

– Cody Host
Nov 25 '18 at 23:20







I change ref.observe to ref.observeSingleEvent but nothing changed. What do you suppose I change the uid to?

– Cody Host
Nov 25 '18 at 23:20




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447224%2fposts-being-loaded-for-all-users-in-collection-view-firebase-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Fiat S.p.A.