Fetch posts by postId from firebase with swift 4












4















All I'm trying to achieve is to have the posts inside my radius append to the collection view. Currently I'm getting all the postId when the user enters the radius but nothing is being append to the collection view and I can't figure out why. Any help is very much appreciated.



var posts = [Post]()
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

guard let user = self.user else { return }
let ref = Database.database().reference().child("posts").child(key)
ref.observeSingleEvent(of: .value, with: { (snapshot) in

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

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

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

}, withCancel: { (error) in
print("There was an error getting the posts:", error)
})

})
}

fileprivate func fetchPostUserIds() {
Database.database().reference().child("users").observe(.value, with: { (snapshot) in

guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
userIdsDictionary.forEach({ (key, value) in

let uidKey = key
self.geoFireRef = Database.database().reference().child("posts").child(uidKey)
self.geoFire = GeoFire(firebaseRef: self.geoFireRef)

})
}) { (err) in
print("Failed to get user Id", err)
}
}


Firebase Database










share|improve this question

























  • If you runt the code in a debugger, is the self.posts.append(post) being reached? If so, you're probably just forgetting to notify the view that it needs to repaint itself. Typically his would be something like tableView.reloadData().

    – Frank van Puffelen
    Nov 20 '18 at 1:12











  • self.posts.append(post) isn't being executed. Do you think I might be accessing my database wrong? @FrankvanPuffelen

    – Cam Garl
    Nov 20 '18 at 1:56













  • That sounds unexpected. Is that even the case when you put a breakpoint on it? Also, does theprint("There was an error getting the posts:", error) get executed?

    – Frank van Puffelen
    Nov 20 '18 at 2:00











  • No print("There was an error getting the posts:", error) is not getting executed. When i placed a break point on self.posts.append(post) it didn't get reached. I have no idea why. @FrankvanPuffelen

    – Cam Garl
    Nov 20 '18 at 2:12













  • OK. So if you put a breakpoint on let ref = Database.database().reference().child("posts").child(key) does that get hit. Note that at this point it's probably more efficient if you set a breakpoint on each line of your code, check where it gets, what the values of the various variables are, and then report all back in one go.

    – Frank van Puffelen
    Nov 20 '18 at 3:36
















4















All I'm trying to achieve is to have the posts inside my radius append to the collection view. Currently I'm getting all the postId when the user enters the radius but nothing is being append to the collection view and I can't figure out why. Any help is very much appreciated.



var posts = [Post]()
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

guard let user = self.user else { return }
let ref = Database.database().reference().child("posts").child(key)
ref.observeSingleEvent(of: .value, with: { (snapshot) in

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

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

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

}, withCancel: { (error) in
print("There was an error getting the posts:", error)
})

})
}

fileprivate func fetchPostUserIds() {
Database.database().reference().child("users").observe(.value, with: { (snapshot) in

guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
userIdsDictionary.forEach({ (key, value) in

let uidKey = key
self.geoFireRef = Database.database().reference().child("posts").child(uidKey)
self.geoFire = GeoFire(firebaseRef: self.geoFireRef)

})
}) { (err) in
print("Failed to get user Id", err)
}
}


Firebase Database










share|improve this question

























  • If you runt the code in a debugger, is the self.posts.append(post) being reached? If so, you're probably just forgetting to notify the view that it needs to repaint itself. Typically his would be something like tableView.reloadData().

    – Frank van Puffelen
    Nov 20 '18 at 1:12











  • self.posts.append(post) isn't being executed. Do you think I might be accessing my database wrong? @FrankvanPuffelen

    – Cam Garl
    Nov 20 '18 at 1:56













  • That sounds unexpected. Is that even the case when you put a breakpoint on it? Also, does theprint("There was an error getting the posts:", error) get executed?

    – Frank van Puffelen
    Nov 20 '18 at 2:00











  • No print("There was an error getting the posts:", error) is not getting executed. When i placed a break point on self.posts.append(post) it didn't get reached. I have no idea why. @FrankvanPuffelen

    – Cam Garl
    Nov 20 '18 at 2:12













  • OK. So if you put a breakpoint on let ref = Database.database().reference().child("posts").child(key) does that get hit. Note that at this point it's probably more efficient if you set a breakpoint on each line of your code, check where it gets, what the values of the various variables are, and then report all back in one go.

    – Frank van Puffelen
    Nov 20 '18 at 3:36














4












4








4


3






All I'm trying to achieve is to have the posts inside my radius append to the collection view. Currently I'm getting all the postId when the user enters the radius but nothing is being append to the collection view and I can't figure out why. Any help is very much appreciated.



var posts = [Post]()
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

guard let user = self.user else { return }
let ref = Database.database().reference().child("posts").child(key)
ref.observeSingleEvent(of: .value, with: { (snapshot) in

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

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

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

}, withCancel: { (error) in
print("There was an error getting the posts:", error)
})

})
}

fileprivate func fetchPostUserIds() {
Database.database().reference().child("users").observe(.value, with: { (snapshot) in

guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
userIdsDictionary.forEach({ (key, value) in

let uidKey = key
self.geoFireRef = Database.database().reference().child("posts").child(uidKey)
self.geoFire = GeoFire(firebaseRef: self.geoFireRef)

})
}) { (err) in
print("Failed to get user Id", err)
}
}


Firebase Database










share|improve this question
















All I'm trying to achieve is to have the posts inside my radius append to the collection view. Currently I'm getting all the postId when the user enters the radius but nothing is being append to the collection view and I can't figure out why. Any help is very much appreciated.



var posts = [Post]()
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

guard let user = self.user else { return }
let ref = Database.database().reference().child("posts").child(key)
ref.observeSingleEvent(of: .value, with: { (snapshot) in

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

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

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

}, withCancel: { (error) in
print("There was an error getting the posts:", error)
})

})
}

fileprivate func fetchPostUserIds() {
Database.database().reference().child("users").observe(.value, with: { (snapshot) in

guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
userIdsDictionary.forEach({ (key, value) in

let uidKey = key
self.geoFireRef = Database.database().reference().child("posts").child(uidKey)
self.geoFire = GeoFire(firebaseRef: self.geoFireRef)

})
}) { (err) in
print("Failed to get user Id", err)
}
}


Firebase Database







swift firebase firebase-realtime-database geofire






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 4:30









Frank van Puffelen

229k28375399




229k28375399










asked Nov 20 '18 at 0:38









Cam GarlCam Garl

212




212













  • If you runt the code in a debugger, is the self.posts.append(post) being reached? If so, you're probably just forgetting to notify the view that it needs to repaint itself. Typically his would be something like tableView.reloadData().

    – Frank van Puffelen
    Nov 20 '18 at 1:12











  • self.posts.append(post) isn't being executed. Do you think I might be accessing my database wrong? @FrankvanPuffelen

    – Cam Garl
    Nov 20 '18 at 1:56













  • That sounds unexpected. Is that even the case when you put a breakpoint on it? Also, does theprint("There was an error getting the posts:", error) get executed?

    – Frank van Puffelen
    Nov 20 '18 at 2:00











  • No print("There was an error getting the posts:", error) is not getting executed. When i placed a break point on self.posts.append(post) it didn't get reached. I have no idea why. @FrankvanPuffelen

    – Cam Garl
    Nov 20 '18 at 2:12













  • OK. So if you put a breakpoint on let ref = Database.database().reference().child("posts").child(key) does that get hit. Note that at this point it's probably more efficient if you set a breakpoint on each line of your code, check where it gets, what the values of the various variables are, and then report all back in one go.

    – Frank van Puffelen
    Nov 20 '18 at 3:36



















  • If you runt the code in a debugger, is the self.posts.append(post) being reached? If so, you're probably just forgetting to notify the view that it needs to repaint itself. Typically his would be something like tableView.reloadData().

    – Frank van Puffelen
    Nov 20 '18 at 1:12











  • self.posts.append(post) isn't being executed. Do you think I might be accessing my database wrong? @FrankvanPuffelen

    – Cam Garl
    Nov 20 '18 at 1:56













  • That sounds unexpected. Is that even the case when you put a breakpoint on it? Also, does theprint("There was an error getting the posts:", error) get executed?

    – Frank van Puffelen
    Nov 20 '18 at 2:00











  • No print("There was an error getting the posts:", error) is not getting executed. When i placed a break point on self.posts.append(post) it didn't get reached. I have no idea why. @FrankvanPuffelen

    – Cam Garl
    Nov 20 '18 at 2:12













  • OK. So if you put a breakpoint on let ref = Database.database().reference().child("posts").child(key) does that get hit. Note that at this point it's probably more efficient if you set a breakpoint on each line of your code, check where it gets, what the values of the various variables are, and then report all back in one go.

    – Frank van Puffelen
    Nov 20 '18 at 3:36

















If you runt the code in a debugger, is the self.posts.append(post) being reached? If so, you're probably just forgetting to notify the view that it needs to repaint itself. Typically his would be something like tableView.reloadData().

– Frank van Puffelen
Nov 20 '18 at 1:12





If you runt the code in a debugger, is the self.posts.append(post) being reached? If so, you're probably just forgetting to notify the view that it needs to repaint itself. Typically his would be something like tableView.reloadData().

– Frank van Puffelen
Nov 20 '18 at 1:12













self.posts.append(post) isn't being executed. Do you think I might be accessing my database wrong? @FrankvanPuffelen

– Cam Garl
Nov 20 '18 at 1:56







self.posts.append(post) isn't being executed. Do you think I might be accessing my database wrong? @FrankvanPuffelen

– Cam Garl
Nov 20 '18 at 1:56















That sounds unexpected. Is that even the case when you put a breakpoint on it? Also, does theprint("There was an error getting the posts:", error) get executed?

– Frank van Puffelen
Nov 20 '18 at 2:00





That sounds unexpected. Is that even the case when you put a breakpoint on it? Also, does theprint("There was an error getting the posts:", error) get executed?

– Frank van Puffelen
Nov 20 '18 at 2:00













No print("There was an error getting the posts:", error) is not getting executed. When i placed a break point on self.posts.append(post) it didn't get reached. I have no idea why. @FrankvanPuffelen

– Cam Garl
Nov 20 '18 at 2:12







No print("There was an error getting the posts:", error) is not getting executed. When i placed a break point on self.posts.append(post) it didn't get reached. I have no idea why. @FrankvanPuffelen

– Cam Garl
Nov 20 '18 at 2:12















OK. So if you put a breakpoint on let ref = Database.database().reference().child("posts").child(key) does that get hit. Note that at this point it's probably more efficient if you set a breakpoint on each line of your code, check where it gets, what the values of the various variables are, and then report all back in one go.

– Frank van Puffelen
Nov 20 '18 at 3:36





OK. So if you put a breakpoint on let ref = Database.database().reference().child("posts").child(key) does that get hit. Note that at this point it's probably more efficient if you set a breakpoint on each line of your code, check where it gets, what the values of the various variables are, and then report all back in one go.

– Frank van Puffelen
Nov 20 '18 at 3:36












0






active

oldest

votes











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%2f53384620%2ffetch-posts-by-postid-from-firebase-with-swift-4%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53384620%2ffetch-posts-by-postid-from-firebase-with-swift-4%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

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$