Reputation: 66
How would I achieve this setup in Firebase Realtime Database with Swift:
Database hierarchy
Currently, I am doing this by storing the larger element (with properties familyKey, geofences, and phoneNumbers) as a custom object. Also, the geofences property itself is an array of custom objects. I get an NSException doing this in the described fashion. How else would I go about doing this?
var tempGeofences = [GeofenceData]()
tempGeofences.append(GeofenceData(name: "Hello WOrld", latitude: 0, longitude: 0, radius: 1000))
let familyKey:String = String(Int.random(in: 1000...99999))
let uid:String = Auth.auth().currentUser!.uid
let phoneNumber = "1111111111"
let parent = Parent(phoneNumber: phoneNumber, familyKey: familyKey, geofences: tempGeofences)
databaseRef.child(uid).setValue(parent)
The NSException is thrown on this line:
databaseRef.child(uid).setValue(parent)
Parent class:
import Foundation
public class Parent {
var phoneNumber: String?
var familyKey: String?
var geofences: [GeofenceData]?
init() {
self.phoneNumber = ""
self.familyKey = ""
self.geofences = nil
}
init(phoneNumber: String?, familyKey: String?, geofences:[GeofenceData]) {
self.phoneNumber = phoneNumber
self.familyKey = familyKey
self.geofences = geofences
}
public func getPhoneNumber() -> String {
return phoneNumber!
}
public func getFamilyKey() -> String {
return familyKey!
}
public func getGeofences() -> [GeofenceData] {
return geofences!
}
// left off here, trying to send geofence object to firebase
public func toDictionary() -> Any {
return ["familyKey": familyKey, "geofences": geofences, "phoneNumber": phoneNumber]
}
}
And the GeofenceData class:
import Foundation
import Firebase
public class GeofenceData {
var name: String?
var latitude: Double?
var longitude: Double?
var radius: Float?
init() {
}
init(name: String?, latitude: Double, longitude: Double, radius: Float) {
self.name = name
self.latitude = latitude
self.longitude = longitude
self.radius = radius
}
// left off here, trying to send geofence object to firebase
public func toDictionary() -> Any {
return ["name": name, "latitude": latitude, "longitude": longitude, "radius": radius]
}
public func getName() -> String {
return name!
}
public func getLatitude() -> Double {
return latitude!
}
public func getLongitude() -> Double {
return longitude!
}
public func getRadius() -> Float {
return radius!
}
public func setName(name: String?) {
self.name = name
}
public func saveToFirebase(reference: DatabaseReference) {
let dict = ["name": name, "latitude": latitude, "longitude": longitude, "radius": radius] as Any
reference.child("geofences").child("0").setValue(dict)
}
}
Upvotes: 3
Views: 2096
Reputation: 35648
Parent is not an object that Firebase recognizes so it throws an error.
The Firebase guide Reading & Writing Data shows the four types of objects that can be written; String, Number, Dictionary, Array.
One solution is to build a function into the class that returns the data you want to write.
public class Parent {
var phoneNumber: String?
var familyKey: String?
var geofences: [GeofenceData]?
init() {
self.phoneNumber = ""
self.familyKey = ""
self.geofences = nil
}
//other init functions
func getParentDict() -> [String: Any] {
let geoDict = ["name": name,
"latitude": latitude,
"longitude": longitude,
"radius": radius
]
let zeroNode = ["0": geoDict]
let dictForFirebase: [String: Any] = [
"phoneNumber": phoneNumber,
"familyKey": familyKey,
"geofences": zeroNode
]
return dictForFirebase
}
}
and in practice
var tempGeofences = [GeofenceData]()
tempGeofences.append(GeofenceData(name: "Hello WOrld", latitude: 0, longitude: 0, radius: 1000))
let familyKey:String = String(Int.random(in: 1000...99999))
let uid:String = Auth.auth().currentUser!.uid
let phoneNumber = "1111111111"
let parent = Parent(phoneNumber: phoneNumber, familyKey: familyKey, geofences: tempGeofences)
let parentDict = parent.getParentDict
databaseRef.child(uid).setValue(parentDict)
However, one concern is the child node with "0" as the key. That looks like you may be using an array. If there's a good reason that's fine but there are usually much better alternatives to using array's in NoSQL databases. See the legacy but still accurate Firebase post called Arrays Are Evil
EDIT:
Per a comment/question 'how to add another child node following the "0" node"
Assume we know the parent node, qSaEE..., lets add a "1" node
let parentNode = "qSaEE..."
let geofenceRef = firebaseRef.child(parentNode).child("geofences")
let geoDict = ["name": name,
"latitude": latitude,
"longitude": longitude,
"radius": radius
]
let oneNode = ["1": geoDict]
geofenceNode.setValue(oneNode)
Upvotes: 1