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.

1 Answers1

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")
    }
}
Callam
  • 11,409
  • 2
  • 34
  • 32