将自定义对象与自定义对象数组存储在Firebase中:Swift [英] Storing custom objects with array of custom objects in Firebase: Swift
问题描述
如何使用Swift在Firebase Realtime数据库中实现此设置:
How would I achieve this setup in Firebase Realtime Database with Swift:
数据库层次结构
当前,我通过将较大的元素(具有属性familyKey,geofences和phoneNumbers)存储为自定义对象来实现此目的.此外,geofences属性本身是自定义对象的数组.我得到了NSException以描述的方式执行此操作.我还能怎么做呢?
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)
在此行上引发了NSException:
The NSException is thrown on this line:
databaseRef.child(uid).setValue(parent)
父类:
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]
}
}
和GeofenceData类:
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)
}
}
推荐答案
父项不是Firebase识别的对象,因此会引发错误.
Parent is not an object that Firebase recognizes so it throws an error.
Firebase指南阅读和阅读写入数据显示了可以写入的四种对象.字符串,数字,字典,数组.
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
}
}
在实践中
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)
但是,令人担忧的是以"0"作为键的子节点.看起来您可能正在使用数组.如果有很好的理由,但是在NoSQL数据库中使用数组通常会有更好的选择.请参阅旧版但仍然准确的Firebase帖子,名为 Arrays Evil
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
每个注释/问题如何在"0"节点之后添加另一个子节点"
Per a comment/question 'how to add another child node following the "0" node"
假设我们知道父节点qSaEE ...,让我们添加一个"1"节点
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)
这篇关于将自定义对象与自定义对象数组存储在Firebase中:Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!