0

I have this piece of working code that saves data to Firebase:

let locRef = locationRef.childByAutoId()
let locItem = [
    senderId : [
        "location": getLocationID()
    ]
]

And I want to retrieve the user's (identified by senderID) "location".

I want to call the data from another function and present it where appropriate, check my comments in the code below:

// Retrieve data from firebase here

let message = messages[indexPath.item] // 1

// Call data I have retrieved below with message 
let text = "Sending from: " + // User Location

if message.senderId == senderId { // 2
    return nil
} else { // 3
    return NSAttributedString(string: text)
}

What is the syntax that would allow me to do this?

Thanks!

2
  • 1
    actually with the code you attached you are not saving the locationId. Also, do you want to retrieve the user location by the user id? Commented Jun 27, 2016 at 11:44
  • I didn't show it but I have another line of code where I save the data. Anyways, yes I want to retrieve location by user ID. Commented Jun 27, 2016 at 17:13

1 Answer 1

1

It sounds pretty straight forward then.

locationRef.child(locItemId).child(senderId).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        let locationId = snapshot.value!["location"] as! String
    }) { (error) in
        print(error.localizedDescription)
}
Sign up to request clarification or add additional context in comments.

4 Comments

One question...where did you get the (locItemId) from? What if I need to retrieve the data from another method?
@AhadSheriff you changed your question to another thing totally different. In this case you should open a new one not just edit it the way its convenient for you. Thanks.
Okay will do, but can you still let me know what locItemId was?
The locItemId is the Id you are generating with childByAutoId(). My answer is assuming that you have this structure /locations/locItemID/senderID and when querying the data you already have the locItemID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.