Fetch posts from postId firebase & swift
I want to fetch the specific posts by they're postId
. Is this something that is possible to do with firebase and swift?
Currently I'm getting a key from geofire which has all the postId keys that I want in it. Is it possible to get the post data relating to these postIds
Here is my current attempt. My current attempt doesn't append any of the posts to my collectionView
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)
guard let uid = Auth.auth().currentUser?.uid else { return }
_ = circleQuery?.observe(.keyEntered, with: { (key, location) in
print(key)
let ref = Database.database().reference().child("posts")
var postId = key
ref.child(uid).child(postId).observe(.value, with: { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
guard let dictionaries = snapshot.value as? [String: Any] else { return }
dictionaries.forEach({ (key, value) in
guard let dictionary = value as? [String: Any] else { return }
guard let user = Auth.auth().currentUser?.uid as? User 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
})
})
}, withCancel: { (error) in
print("There was an error getting the posts:", error)
})
self.locationManager.stopUpdatingLocation()
})
}
swift firebase firebase-realtime-database geofire
add a comment |
I want to fetch the specific posts by they're postId
. Is this something that is possible to do with firebase and swift?
Currently I'm getting a key from geofire which has all the postId keys that I want in it. Is it possible to get the post data relating to these postIds
Here is my current attempt. My current attempt doesn't append any of the posts to my collectionView
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)
guard let uid = Auth.auth().currentUser?.uid else { return }
_ = circleQuery?.observe(.keyEntered, with: { (key, location) in
print(key)
let ref = Database.database().reference().child("posts")
var postId = key
ref.child(uid).child(postId).observe(.value, with: { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
guard let dictionaries = snapshot.value as? [String: Any] else { return }
dictionaries.forEach({ (key, value) in
guard let dictionary = value as? [String: Any] else { return }
guard let user = Auth.auth().currentUser?.uid as? User 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
})
})
}, withCancel: { (error) in
print("There was an error getting the posts:", error)
})
self.locationManager.stopUpdatingLocation()
})
}
swift firebase firebase-realtime-database geofire
Please show your db structure
– elbert rivas
Nov 14 '18 at 10:34
@elbertrivas added it to my question
– James Jamison
Nov 14 '18 at 12:55
Remove forEach() since you are already in the postId node. You can get the data using your "dictionaries" variable. To get the data, do like this. let caption = dictionaries["caption"] as! String
– elbert rivas
Nov 14 '18 at 13:06
add a comment |
I want to fetch the specific posts by they're postId
. Is this something that is possible to do with firebase and swift?
Currently I'm getting a key from geofire which has all the postId keys that I want in it. Is it possible to get the post data relating to these postIds
Here is my current attempt. My current attempt doesn't append any of the posts to my collectionView
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)
guard let uid = Auth.auth().currentUser?.uid else { return }
_ = circleQuery?.observe(.keyEntered, with: { (key, location) in
print(key)
let ref = Database.database().reference().child("posts")
var postId = key
ref.child(uid).child(postId).observe(.value, with: { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
guard let dictionaries = snapshot.value as? [String: Any] else { return }
dictionaries.forEach({ (key, value) in
guard let dictionary = value as? [String: Any] else { return }
guard let user = Auth.auth().currentUser?.uid as? User 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
})
})
}, withCancel: { (error) in
print("There was an error getting the posts:", error)
})
self.locationManager.stopUpdatingLocation()
})
}
swift firebase firebase-realtime-database geofire
I want to fetch the specific posts by they're postId
. Is this something that is possible to do with firebase and swift?
Currently I'm getting a key from geofire which has all the postId keys that I want in it. Is it possible to get the post data relating to these postIds
Here is my current attempt. My current attempt doesn't append any of the posts to my collectionView
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)
guard let uid = Auth.auth().currentUser?.uid else { return }
_ = circleQuery?.observe(.keyEntered, with: { (key, location) in
print(key)
let ref = Database.database().reference().child("posts")
var postId = key
ref.child(uid).child(postId).observe(.value, with: { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
guard let dictionaries = snapshot.value as? [String: Any] else { return }
dictionaries.forEach({ (key, value) in
guard let dictionary = value as? [String: Any] else { return }
guard let user = Auth.auth().currentUser?.uid as? User 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
})
})
}, withCancel: { (error) in
print("There was an error getting the posts:", error)
})
self.locationManager.stopUpdatingLocation()
})
}
swift firebase firebase-realtime-database geofire
swift firebase firebase-realtime-database geofire
edited Nov 14 '18 at 12:56
James Jamison
asked Nov 13 '18 at 23:41
James JamisonJames Jamison
346
346
Please show your db structure
– elbert rivas
Nov 14 '18 at 10:34
@elbertrivas added it to my question
– James Jamison
Nov 14 '18 at 12:55
Remove forEach() since you are already in the postId node. You can get the data using your "dictionaries" variable. To get the data, do like this. let caption = dictionaries["caption"] as! String
– elbert rivas
Nov 14 '18 at 13:06
add a comment |
Please show your db structure
– elbert rivas
Nov 14 '18 at 10:34
@elbertrivas added it to my question
– James Jamison
Nov 14 '18 at 12:55
Remove forEach() since you are already in the postId node. You can get the data using your "dictionaries" variable. To get the data, do like this. let caption = dictionaries["caption"] as! String
– elbert rivas
Nov 14 '18 at 13:06
Please show your db structure
– elbert rivas
Nov 14 '18 at 10:34
Please show your db structure
– elbert rivas
Nov 14 '18 at 10:34
@elbertrivas added it to my question
– James Jamison
Nov 14 '18 at 12:55
@elbertrivas added it to my question
– James Jamison
Nov 14 '18 at 12:55
Remove forEach() since you are already in the postId node. You can get the data using your "dictionaries" variable. To get the data, do like this. let caption = dictionaries["caption"] as! String
– elbert rivas
Nov 14 '18 at 13:06
Remove forEach() since you are already in the postId node. You can get the data using your "dictionaries" variable. To get the data, do like this. let caption = dictionaries["caption"] as! String
– elbert rivas
Nov 14 '18 at 13:06
add a comment |
1 Answer
1
active
oldest
votes
use snapshot haschild to check
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("(self.yourValueToCompare!)"){
print("Exists in Database")
}else{
print("Does not Exists in Database")
}
})
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291089%2ffetch-posts-from-postid-firebase-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
use snapshot haschild to check
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("(self.yourValueToCompare!)"){
print("Exists in Database")
}else{
print("Does not Exists in Database")
}
})
add a comment |
use snapshot haschild to check
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("(self.yourValueToCompare!)"){
print("Exists in Database")
}else{
print("Does not Exists in Database")
}
})
add a comment |
use snapshot haschild to check
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("(self.yourValueToCompare!)"){
print("Exists in Database")
}else{
print("Does not Exists in Database")
}
})
use snapshot haschild to check
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("(self.yourValueToCompare!)"){
print("Exists in Database")
}else{
print("Does not Exists in Database")
}
})
answered Nov 15 '18 at 10:14
Yogesh TandelYogesh Tandel
664811
664811
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291089%2ffetch-posts-from-postid-firebase-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please show your db structure
– elbert rivas
Nov 14 '18 at 10:34
@elbertrivas added it to my question
– James Jamison
Nov 14 '18 at 12:55
Remove forEach() since you are already in the postId node. You can get the data using your "dictionaries" variable. To get the data, do like this. let caption = dictionaries["caption"] as! String
– elbert rivas
Nov 14 '18 at 13:06