1

I have an array of lat and long coordinates which I would like to save to firebase. I know that there is a normal way which I could use. But I was wondering how I could save the whole array.

The array I would like to save looks like the following:

(1.323, 2.334),(6.323, 7.334),(0.323, 65.334),(43.323, 32.334)...

I am using Real-time database.

2
  • Real-time database? Commented Feb 9, 2019 at 18:21
  • Yes @Callam that is correct Commented Feb 9, 2019 at 18:29

1 Answer 1

1
let databaseRef = Database.database().reference()
let coordinatesRef = databaseRef.child("path/to/coordinates")

func save(coordinates: [(Double, Double)], completion: @escaping (Error?) -> ()) {
    coordinatesRef.setValue(coordinates.map { [$0, $1] }) { completion($0) }
}

save(coordinates: [
    (1.323, 2.334),
    (6.323, 7.334),
    (0.323, 65.334),
    (43.323, 32.334)
]) { error in
    if let error = error {
        print("❌ Saving coordinates to Firebase failed", error)
    } else {
        print("🔥 Saving coordinates to Firebase succeeded")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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