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

}


Firebase Database










share|improve this question
























  • 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















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

}


Firebase Database










share|improve this question
























  • 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













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

}


Firebase Database










share|improve this question















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

}


Firebase Database







swift firebase firebase-realtime-database swift4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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
















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












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





share|improve this answer




























    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.






    share|improve this answer





















    • 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


















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





    share|improve this answer





















    • 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










    • Did it work or not?
      – DionizB
      Nov 6 at 6:58











    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',
    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%2f53147335%2fdata-not-being-fetched-from-firebase-database-swift-4%23new-answer', 'question_page');
    }
    );

    Post as a guest
































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





    share|improve this answer

























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





      share|improve this answer























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





        share|improve this answer












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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 5 at 5:32









        Gregory Wilson Pullyattu

        237113




        237113
























            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.






            share|improve this answer





















            • 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















            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.






            share|improve this answer





















            • 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













            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • 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










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





            share|improve this answer





















            • 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










            • Did it work or not?
              – DionizB
              Nov 6 at 6:58















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





            share|improve this answer





















            • 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










            • Did it work or not?
              – DionizB
              Nov 6 at 6:58













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





            share|improve this answer












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






            share|improve this answer












            share|improve this answer



            share|improve this answer










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










            • 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










            • 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


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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




















































































            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini