Structuring real time location data request from firebase using swift









up vote
1
down vote

favorite












So I'm trying to retrieve the location of a "paired user" (i.e. not the current user but a contact of the current user) and monitor the distance travelled and distance relative to the currentUser. When using the code below I am only able to retrieve a list with multiple entries of the same location rather than consistently getting only the new location update of the 'paired user'.



func retrieveLocations() {
let locationDB = Database.database().reference().child("Locations").child("nameOfUser")

locationDB.queryLimited(toLast: 1).observe(.childAdded)
(snapshot) in

let snapshotValue = snapshot.value as! Dictionary<String,Any>

let lats = snapshotValue["Lat"]
let longs = snapshotValue["Lon"]
let timeOf = snapshotValue["Time"]
let sender = snapshotValue["Sender"]


let otherUsersLocations = CLLocation(latitude: CLLocationDegrees(truncating: lats! as! NSNumber), longitude: CLLocationDegrees(truncating: longs! as! NSNumber), timestamp: timeOf as! Int)
print("Latitude: (otherUsersLocations.coordinate.latitude), Longitude: (otherUsersLocations.coordinate.longitude), Time: (timeOf), Sender: (sender)")


I later call the function in the same func that I use to update the current users location. The code below is used to update the current users location to firebase.Firebase Database screenshot



let locationDictionary = [

"Sender": Auth.auth().currentUser?.email,
"Lat": latitude,
"Lon": longitude,
"Speed": speed,
"Time": ServerValue.timestamp()

] as [String : Any]


locationDB.childByAutoId().setValue(locationDictionary)
(error, reference) in
if error != nil
print(error!)

else







retrieveLocations()


I have tried creating a new "LocationModel" object but had troubles feeding the dictionary of LocationModel type with the retrieved data from firebase.
So my objective is to retrieve an array/dictionary of the location data for the paired user from "now" and subsequently measure the distance to the currentUser.



I've been sitting at this for a while so I really appreciate any suggestions.
Thanks a lot!



Pat










share|improve this question

























    up vote
    1
    down vote

    favorite












    So I'm trying to retrieve the location of a "paired user" (i.e. not the current user but a contact of the current user) and monitor the distance travelled and distance relative to the currentUser. When using the code below I am only able to retrieve a list with multiple entries of the same location rather than consistently getting only the new location update of the 'paired user'.



    func retrieveLocations() {
    let locationDB = Database.database().reference().child("Locations").child("nameOfUser")

    locationDB.queryLimited(toLast: 1).observe(.childAdded)
    (snapshot) in

    let snapshotValue = snapshot.value as! Dictionary<String,Any>

    let lats = snapshotValue["Lat"]
    let longs = snapshotValue["Lon"]
    let timeOf = snapshotValue["Time"]
    let sender = snapshotValue["Sender"]


    let otherUsersLocations = CLLocation(latitude: CLLocationDegrees(truncating: lats! as! NSNumber), longitude: CLLocationDegrees(truncating: longs! as! NSNumber), timestamp: timeOf as! Int)
    print("Latitude: (otherUsersLocations.coordinate.latitude), Longitude: (otherUsersLocations.coordinate.longitude), Time: (timeOf), Sender: (sender)")


    I later call the function in the same func that I use to update the current users location. The code below is used to update the current users location to firebase.Firebase Database screenshot



    let locationDictionary = [

    "Sender": Auth.auth().currentUser?.email,
    "Lat": latitude,
    "Lon": longitude,
    "Speed": speed,
    "Time": ServerValue.timestamp()

    ] as [String : Any]


    locationDB.childByAutoId().setValue(locationDictionary)
    (error, reference) in
    if error != nil
    print(error!)

    else







    retrieveLocations()


    I have tried creating a new "LocationModel" object but had troubles feeding the dictionary of LocationModel type with the retrieved data from firebase.
    So my objective is to retrieve an array/dictionary of the location data for the paired user from "now" and subsequently measure the distance to the currentUser.



    I've been sitting at this for a while so I really appreciate any suggestions.
    Thanks a lot!



    Pat










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      So I'm trying to retrieve the location of a "paired user" (i.e. not the current user but a contact of the current user) and monitor the distance travelled and distance relative to the currentUser. When using the code below I am only able to retrieve a list with multiple entries of the same location rather than consistently getting only the new location update of the 'paired user'.



      func retrieveLocations() {
      let locationDB = Database.database().reference().child("Locations").child("nameOfUser")

      locationDB.queryLimited(toLast: 1).observe(.childAdded)
      (snapshot) in

      let snapshotValue = snapshot.value as! Dictionary<String,Any>

      let lats = snapshotValue["Lat"]
      let longs = snapshotValue["Lon"]
      let timeOf = snapshotValue["Time"]
      let sender = snapshotValue["Sender"]


      let otherUsersLocations = CLLocation(latitude: CLLocationDegrees(truncating: lats! as! NSNumber), longitude: CLLocationDegrees(truncating: longs! as! NSNumber), timestamp: timeOf as! Int)
      print("Latitude: (otherUsersLocations.coordinate.latitude), Longitude: (otherUsersLocations.coordinate.longitude), Time: (timeOf), Sender: (sender)")


      I later call the function in the same func that I use to update the current users location. The code below is used to update the current users location to firebase.Firebase Database screenshot



      let locationDictionary = [

      "Sender": Auth.auth().currentUser?.email,
      "Lat": latitude,
      "Lon": longitude,
      "Speed": speed,
      "Time": ServerValue.timestamp()

      ] as [String : Any]


      locationDB.childByAutoId().setValue(locationDictionary)
      (error, reference) in
      if error != nil
      print(error!)

      else







      retrieveLocations()


      I have tried creating a new "LocationModel" object but had troubles feeding the dictionary of LocationModel type with the retrieved data from firebase.
      So my objective is to retrieve an array/dictionary of the location data for the paired user from "now" and subsequently measure the distance to the currentUser.



      I've been sitting at this for a while so I really appreciate any suggestions.
      Thanks a lot!



      Pat










      share|improve this question













      So I'm trying to retrieve the location of a "paired user" (i.e. not the current user but a contact of the current user) and monitor the distance travelled and distance relative to the currentUser. When using the code below I am only able to retrieve a list with multiple entries of the same location rather than consistently getting only the new location update of the 'paired user'.



      func retrieveLocations() {
      let locationDB = Database.database().reference().child("Locations").child("nameOfUser")

      locationDB.queryLimited(toLast: 1).observe(.childAdded)
      (snapshot) in

      let snapshotValue = snapshot.value as! Dictionary<String,Any>

      let lats = snapshotValue["Lat"]
      let longs = snapshotValue["Lon"]
      let timeOf = snapshotValue["Time"]
      let sender = snapshotValue["Sender"]


      let otherUsersLocations = CLLocation(latitude: CLLocationDegrees(truncating: lats! as! NSNumber), longitude: CLLocationDegrees(truncating: longs! as! NSNumber), timestamp: timeOf as! Int)
      print("Latitude: (otherUsersLocations.coordinate.latitude), Longitude: (otherUsersLocations.coordinate.longitude), Time: (timeOf), Sender: (sender)")


      I later call the function in the same func that I use to update the current users location. The code below is used to update the current users location to firebase.Firebase Database screenshot



      let locationDictionary = [

      "Sender": Auth.auth().currentUser?.email,
      "Lat": latitude,
      "Lon": longitude,
      "Speed": speed,
      "Time": ServerValue.timestamp()

      ] as [String : Any]


      locationDB.childByAutoId().setValue(locationDictionary)
      (error, reference) in
      if error != nil
      print(error!)

      else







      retrieveLocations()


      I have tried creating a new "LocationModel" object but had troubles feeding the dictionary of LocationModel type with the retrieved data from firebase.
      So my objective is to retrieve an array/dictionary of the location data for the paired user from "now" and subsequently measure the distance to the currentUser.



      I've been sitting at this for a while so I really appreciate any suggestions.
      Thanks a lot!



      Pat







      swift firebase firebase-realtime-database location data-retrieval






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 15:42









      Spectrism12

      61




      61



























          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',
          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%2f53240547%2fstructuring-real-time-location-data-request-from-firebase-using-swift%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240547%2fstructuring-real-time-location-data-request-from-firebase-using-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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線