Data not being fetched from firebase database swift 4
up vote
2
down vote
favorite
I'm simply trying to show how far away the user is from the location but I can't seem to get anything being printed to my console. If I place the code in the viewDidLoad and manually put in the coordinates for "postLocations"
I get the distance but I can't seem to get anything printed with the code I have currently.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0] as CLLocation
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? Double else { return }
guard let longitude = dictionary["longitude"] as? Double else { return }
let currentLocation = Coordinate(long: userLocation.coordinate.longitude, lat: userLocation.coordinate.latitude)
let postLocations = Coordinate(long: longitude, lat: latitude)
if currentLocation.distance(to: postLocations) <= 100 {
print("You are (currentLocation.distance(to: postLocations)) away from posts")
} else {
print("No posts in area")
}
}) { (error) in
print("There was an error getting posts", error)
}
}
swift firebase firebase-realtime-database swift4
|
show 1 more comment
up vote
2
down vote
favorite
I'm simply trying to show how far away the user is from the location but I can't seem to get anything being printed to my console. If I place the code in the viewDidLoad and manually put in the coordinates for "postLocations"
I get the distance but I can't seem to get anything printed with the code I have currently.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0] as CLLocation
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? Double else { return }
guard let longitude = dictionary["longitude"] as? Double else { return }
let currentLocation = Coordinate(long: userLocation.coordinate.longitude, lat: userLocation.coordinate.latitude)
let postLocations = Coordinate(long: longitude, lat: latitude)
if currentLocation.distance(to: postLocations) <= 100 {
print("You are (currentLocation.distance(to: postLocations)) away from posts")
} else {
print("No posts in area")
}
}) { (error) in
print("There was an error getting posts", error)
}
}
swift firebase firebase-realtime-database swift4
Is this method was calleddidUpdateLocations
?
– William Hu
Nov 5 at 3:50
Yeah it's being called
– Cody Host
Nov 5 at 3:56
Check what's insnapshot.value
maybe your 'guard` returned, like the type isn't[String: AnyObject]
, maybe try[String: Any]
, see what happens.
– William Hu
Nov 5 at 4:12
Are you trying to get the distance between your current location to the user location in the firebase? If you have solved your issue ignore this.
– Gregory Wilson Pullyattu
Nov 5 at 4:49
I'm trying to get the distance between your current location and the post location @GregoryWilsonPullyattu
– Cody Host
Nov 5 at 5:08
|
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm simply trying to show how far away the user is from the location but I can't seem to get anything being printed to my console. If I place the code in the viewDidLoad and manually put in the coordinates for "postLocations"
I get the distance but I can't seem to get anything printed with the code I have currently.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0] as CLLocation
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? Double else { return }
guard let longitude = dictionary["longitude"] as? Double else { return }
let currentLocation = Coordinate(long: userLocation.coordinate.longitude, lat: userLocation.coordinate.latitude)
let postLocations = Coordinate(long: longitude, lat: latitude)
if currentLocation.distance(to: postLocations) <= 100 {
print("You are (currentLocation.distance(to: postLocations)) away from posts")
} else {
print("No posts in area")
}
}) { (error) in
print("There was an error getting posts", error)
}
}
swift firebase firebase-realtime-database swift4
I'm simply trying to show how far away the user is from the location but I can't seem to get anything being printed to my console. If I place the code in the viewDidLoad and manually put in the coordinates for "postLocations"
I get the distance but I can't seem to get anything printed with the code I have currently.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0] as CLLocation
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? Double else { return }
guard let longitude = dictionary["longitude"] as? Double else { return }
let currentLocation = Coordinate(long: userLocation.coordinate.longitude, lat: userLocation.coordinate.latitude)
let postLocations = Coordinate(long: longitude, lat: latitude)
if currentLocation.distance(to: postLocations) <= 100 {
print("You are (currentLocation.distance(to: postLocations)) away from posts")
} else {
print("No posts in area")
}
}) { (error) in
print("There was an error getting posts", error)
}
}
swift firebase firebase-realtime-database swift4
swift firebase firebase-realtime-database swift4
edited Nov 5 at 14:46
KENdi
5,6492821
5,6492821
asked Nov 5 at 1:51
Cody Host
737
737
Is this method was calleddidUpdateLocations
?
– William Hu
Nov 5 at 3:50
Yeah it's being called
– Cody Host
Nov 5 at 3:56
Check what's insnapshot.value
maybe your 'guard` returned, like the type isn't[String: AnyObject]
, maybe try[String: Any]
, see what happens.
– William Hu
Nov 5 at 4:12
Are you trying to get the distance between your current location to the user location in the firebase? If you have solved your issue ignore this.
– Gregory Wilson Pullyattu
Nov 5 at 4:49
I'm trying to get the distance between your current location and the post location @GregoryWilsonPullyattu
– Cody Host
Nov 5 at 5:08
|
show 1 more comment
Is this method was calleddidUpdateLocations
?
– William Hu
Nov 5 at 3:50
Yeah it's being called
– Cody Host
Nov 5 at 3:56
Check what's insnapshot.value
maybe your 'guard` returned, like the type isn't[String: AnyObject]
, maybe try[String: Any]
, see what happens.
– William Hu
Nov 5 at 4:12
Are you trying to get the distance between your current location to the user location in the firebase? If you have solved your issue ignore this.
– Gregory Wilson Pullyattu
Nov 5 at 4:49
I'm trying to get the distance between your current location and the post location @GregoryWilsonPullyattu
– Cody Host
Nov 5 at 5:08
Is this method was called
didUpdateLocations
?– William Hu
Nov 5 at 3:50
Is this method was called
didUpdateLocations
?– William Hu
Nov 5 at 3:50
Yeah it's being called
– Cody Host
Nov 5 at 3:56
Yeah it's being called
– Cody Host
Nov 5 at 3:56
Check what's in
snapshot.value
maybe your 'guard` returned, like the type isn't [String: AnyObject]
, maybe try [String: Any]
, see what happens.– William Hu
Nov 5 at 4:12
Check what's in
snapshot.value
maybe your 'guard` returned, like the type isn't [String: AnyObject]
, maybe try [String: Any]
, see what happens.– William Hu
Nov 5 at 4:12
Are you trying to get the distance between your current location to the user location in the firebase? If you have solved your issue ignore this.
– Gregory Wilson Pullyattu
Nov 5 at 4:49
Are you trying to get the distance between your current location to the user location in the firebase? If you have solved your issue ignore this.
– Gregory Wilson Pullyattu
Nov 5 at 4:49
I'm trying to get the distance between your current location and the post location @GregoryWilsonPullyattu
– Cody Host
Nov 5 at 5:08
I'm trying to get the distance between your current location and the post location @GregoryWilsonPullyattu
– Cody Host
Nov 5 at 5:08
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Try this code in the viewDidLoad
and check the print statement in the console whether the latitude and longitude is printed.
func fetchUserLocation() {
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? String else { return }
guard let longitude = dictionary["longitude"] as? String else { return }
print("lat: (latitude), lon: (longitude)")
}) { (error) in
print("There was an error getting posts", error)
}
}
add a comment |
up vote
0
down vote
You don't need to call the Firebase observer in the didUpdateLocations
.
You should call the Firebase observer in the viewDidLoad
so that you can fetch the user location from the Firebase Databse. After the observer observe the user location, you need to fetch the current location so that you can compare the 2 locations. Just try this.
I'm not sure I understand. Would you be able to show me an example?
– Cody Host
Nov 5 at 5:19
can you call the firebase observer in the viewdidload
– Gregory Wilson Pullyattu
Nov 5 at 5:20
add a comment |
up vote
0
down vote
On viewDidLoad()
try accessing Firebase Database by calling this:
func getFirebaseUserLocation() {
let ref = Database.reference(withPath: "posts")
ref.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if let postSnapshots = child as? DataSnapshot {
for postSnapshot in postSnapshots.children {
guard let postDictionary = postSnapshot.value as? [String: Any] else { return }
guard let latitude = postDictionary["latitude"] as? String, let longitude = postDictionary["longitude"] as? String else { return }
print(latitude, longitude)
// here you can access your latitude and longitude as Strings from Firebase Database
}
}
}
}
}
for some reason I'm not getting the latitude and longitude being printed out still. Even when I place that code into myviewDidLoad()
– Cody Host
Nov 6 at 0:08
I think the latitude and longitude may be being returned
– Cody Host
Nov 6 at 0:12
Did it work or not?
– DionizB
Nov 6 at 6:58
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try this code in the viewDidLoad
and check the print statement in the console whether the latitude and longitude is printed.
func fetchUserLocation() {
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? String else { return }
guard let longitude = dictionary["longitude"] as? String else { return }
print("lat: (latitude), lon: (longitude)")
}) { (error) in
print("There was an error getting posts", error)
}
}
add a comment |
up vote
1
down vote
accepted
Try this code in the viewDidLoad
and check the print statement in the console whether the latitude and longitude is printed.
func fetchUserLocation() {
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? String else { return }
guard let longitude = dictionary["longitude"] as? String else { return }
print("lat: (latitude), lon: (longitude)")
}) { (error) in
print("There was an error getting posts", error)
}
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try this code in the viewDidLoad
and check the print statement in the console whether the latitude and longitude is printed.
func fetchUserLocation() {
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? String else { return }
guard let longitude = dictionary["longitude"] as? String else { return }
print("lat: (latitude), lon: (longitude)")
}) { (error) in
print("There was an error getting posts", error)
}
}
Try this code in the viewDidLoad
and check the print statement in the console whether the latitude and longitude is printed.
func fetchUserLocation() {
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? String else { return }
guard let longitude = dictionary["longitude"] as? String else { return }
print("lat: (latitude), lon: (longitude)")
}) { (error) in
print("There was an error getting posts", error)
}
}
answered Nov 5 at 5:32
Gregory Wilson Pullyattu
237113
237113
add a comment |
add a comment |
up vote
0
down vote
You don't need to call the Firebase observer in the didUpdateLocations
.
You should call the Firebase observer in the viewDidLoad
so that you can fetch the user location from the Firebase Databse. After the observer observe the user location, you need to fetch the current location so that you can compare the 2 locations. Just try this.
I'm not sure I understand. Would you be able to show me an example?
– Cody Host
Nov 5 at 5:19
can you call the firebase observer in the viewdidload
– Gregory Wilson Pullyattu
Nov 5 at 5:20
add a comment |
up vote
0
down vote
You don't need to call the Firebase observer in the didUpdateLocations
.
You should call the Firebase observer in the viewDidLoad
so that you can fetch the user location from the Firebase Databse. After the observer observe the user location, you need to fetch the current location so that you can compare the 2 locations. Just try this.
I'm not sure I understand. Would you be able to show me an example?
– Cody Host
Nov 5 at 5:19
can you call the firebase observer in the viewdidload
– Gregory Wilson Pullyattu
Nov 5 at 5:20
add a comment |
up vote
0
down vote
up vote
0
down vote
You don't need to call the Firebase observer in the didUpdateLocations
.
You should call the Firebase observer in the viewDidLoad
so that you can fetch the user location from the Firebase Databse. After the observer observe the user location, you need to fetch the current location so that you can compare the 2 locations. Just try this.
You don't need to call the Firebase observer in the didUpdateLocations
.
You should call the Firebase observer in the viewDidLoad
so that you can fetch the user location from the Firebase Databse. After the observer observe the user location, you need to fetch the current location so that you can compare the 2 locations. Just try this.
answered Nov 5 at 5:15
Gregory Wilson Pullyattu
237113
237113
I'm not sure I understand. Would you be able to show me an example?
– Cody Host
Nov 5 at 5:19
can you call the firebase observer in the viewdidload
– Gregory Wilson Pullyattu
Nov 5 at 5:20
add a comment |
I'm not sure I understand. Would you be able to show me an example?
– Cody Host
Nov 5 at 5:19
can you call the firebase observer in the viewdidload
– Gregory Wilson Pullyattu
Nov 5 at 5:20
I'm not sure I understand. Would you be able to show me an example?
– Cody Host
Nov 5 at 5:19
I'm not sure I understand. Would you be able to show me an example?
– Cody Host
Nov 5 at 5:19
can you call the firebase observer in the viewdidload
– Gregory Wilson Pullyattu
Nov 5 at 5:20
can you call the firebase observer in the viewdidload
– Gregory Wilson Pullyattu
Nov 5 at 5:20
add a comment |
up vote
0
down vote
On viewDidLoad()
try accessing Firebase Database by calling this:
func getFirebaseUserLocation() {
let ref = Database.reference(withPath: "posts")
ref.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if let postSnapshots = child as? DataSnapshot {
for postSnapshot in postSnapshots.children {
guard let postDictionary = postSnapshot.value as? [String: Any] else { return }
guard let latitude = postDictionary["latitude"] as? String, let longitude = postDictionary["longitude"] as? String else { return }
print(latitude, longitude)
// here you can access your latitude and longitude as Strings from Firebase Database
}
}
}
}
}
for some reason I'm not getting the latitude and longitude being printed out still. Even when I place that code into myviewDidLoad()
– Cody Host
Nov 6 at 0:08
I think the latitude and longitude may be being returned
– Cody Host
Nov 6 at 0:12
Did it work or not?
– DionizB
Nov 6 at 6:58
add a comment |
up vote
0
down vote
On viewDidLoad()
try accessing Firebase Database by calling this:
func getFirebaseUserLocation() {
let ref = Database.reference(withPath: "posts")
ref.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if let postSnapshots = child as? DataSnapshot {
for postSnapshot in postSnapshots.children {
guard let postDictionary = postSnapshot.value as? [String: Any] else { return }
guard let latitude = postDictionary["latitude"] as? String, let longitude = postDictionary["longitude"] as? String else { return }
print(latitude, longitude)
// here you can access your latitude and longitude as Strings from Firebase Database
}
}
}
}
}
for some reason I'm not getting the latitude and longitude being printed out still. Even when I place that code into myviewDidLoad()
– Cody Host
Nov 6 at 0:08
I think the latitude and longitude may be being returned
– Cody Host
Nov 6 at 0:12
Did it work or not?
– DionizB
Nov 6 at 6:58
add a comment |
up vote
0
down vote
up vote
0
down vote
On viewDidLoad()
try accessing Firebase Database by calling this:
func getFirebaseUserLocation() {
let ref = Database.reference(withPath: "posts")
ref.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if let postSnapshots = child as? DataSnapshot {
for postSnapshot in postSnapshots.children {
guard let postDictionary = postSnapshot.value as? [String: Any] else { return }
guard let latitude = postDictionary["latitude"] as? String, let longitude = postDictionary["longitude"] as? String else { return }
print(latitude, longitude)
// here you can access your latitude and longitude as Strings from Firebase Database
}
}
}
}
}
On viewDidLoad()
try accessing Firebase Database by calling this:
func getFirebaseUserLocation() {
let ref = Database.reference(withPath: "posts")
ref.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if let postSnapshots = child as? DataSnapshot {
for postSnapshot in postSnapshots.children {
guard let postDictionary = postSnapshot.value as? [String: Any] else { return }
guard let latitude = postDictionary["latitude"] as? String, let longitude = postDictionary["longitude"] as? String else { return }
print(latitude, longitude)
// here you can access your latitude and longitude as Strings from Firebase Database
}
}
}
}
}
answered Nov 5 at 10:32
DionizB
692310
692310
for some reason I'm not getting the latitude and longitude being printed out still. Even when I place that code into myviewDidLoad()
– Cody Host
Nov 6 at 0:08
I think the latitude and longitude may be being returned
– Cody Host
Nov 6 at 0:12
Did it work or not?
– DionizB
Nov 6 at 6:58
add a comment |
for some reason I'm not getting the latitude and longitude being printed out still. Even when I place that code into myviewDidLoad()
– Cody Host
Nov 6 at 0:08
I think the latitude and longitude may be being returned
– Cody Host
Nov 6 at 0:12
Did it work or not?
– DionizB
Nov 6 at 6:58
for some reason I'm not getting the latitude and longitude being printed out still. Even when I place that code into my
viewDidLoad()
– Cody Host
Nov 6 at 0:08
for some reason I'm not getting the latitude and longitude being printed out still. Even when I place that code into my
viewDidLoad()
– Cody Host
Nov 6 at 0:08
I think the latitude and longitude may be being returned
– Cody Host
Nov 6 at 0:12
I think the latitude and longitude may be being returned
– Cody Host
Nov 6 at 0:12
Did it work or not?
– DionizB
Nov 6 at 6:58
Did it work or not?
– DionizB
Nov 6 at 6:58
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147335%2fdata-not-being-fetched-from-firebase-database-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
Is this method was called
didUpdateLocations
?– William Hu
Nov 5 at 3:50
Yeah it's being called
– Cody Host
Nov 5 at 3:56
Check what's in
snapshot.value
maybe your 'guard` returned, like the type isn't[String: AnyObject]
, maybe try[String: Any]
, see what happens.– William Hu
Nov 5 at 4:12
Are you trying to get the distance between your current location to the user location in the firebase? If you have solved your issue ignore this.
– Gregory Wilson Pullyattu
Nov 5 at 4:49
I'm trying to get the distance between your current location and the post location @GregoryWilsonPullyattu
– Cody Host
Nov 5 at 5:08