Swift 4:将Firebase 4快照解析为Model

时间:2018-02-23 11:14:23

标签: swift firebase firebase-realtime-database

我正在使用Swift 4和Firebase 4

我有一个名为Part

的模型
class Part {
    var db_id: String?
    var can_8130: Bool?
    var description: String?
    var has_amm: Bool?
    var is_esd: Bool?
    var is_hazmat: Bool?
    var is_priority: Bool?
    var msn: String?
    var partNum: String?
    var received: String?
    var receiver: String?
    var tail: String?

    init(db_id:String?, can_8130: Bool, description: String?, has_amm: Bool?, is_esd: Bool?, is_hazmat: Bool?, is_priority: Bool?, msn: String?, partNum: String?, received: String?, receiver: String?, tail: String?) {
        self.db_id = db_id;
        self.can_8130 = can_8130;
        self.description = description;
        self.has_amm = has_amm;
        self.is_esd = is_esd;
        self.is_hazmat = is_hazmat;
        self.is_priority = is_priority;
        self.msn = msn;
        self.partNum = partNum;
        self.received = received;
        self.receiver = receiver;
        self.tail = tail;
    }
}

在我的ViewController中,我用作UITableViewDataSource

@IBOutlet weak var tableView: UITableView!

var partsArray: [Part] = []

在ViewDidLoad中,我调用retrieveParts()(来自Firebase4实时数据库)

function retrieveParts() {
    let rampPartsDB = Database.database().reference().child("ramp-parts")
    rampPartsDB.observe(.childAdded) { (snapshot) in
        if snapshot.childrenCount > 0 {
            //print(snapshot)
            if let snapValue = snapshot.value as? [String:Any] {
                print(snapValue)
                let dbID = snapValue["db_id"] as! String
                let can8130 = snapValue["can_8130"] as! Bool
                let description = snapValue["description"] as! String
                let hasAMM = snapValue["has_amm"] as! Bool
                let isESD = snapValue["is_esd"] as! Bool
                let isHazmat = snapValue["is_hazmat"] as! Bool
                let isPriority = snapValue["is_priority"] as! Bool
                let msn = snapValue["msn"] as! String
                let partNum = snapValue["partNum"] as! String
                let received = snapValue["received"] as! String
                let receiver = snapValue["receiver"] as! String
                let tail = snapValue["tail"] as! String

                var item = Part(db_id: dbID, can_8130: can8130, description: description, has_amm: hasAMM, is_esd: isESD, is_hazmat: isHazmat, is_priority: isPriority, msn: msn, partNum: partNum, received: received, receiver: receiver, tail: tail)
                print("ITEM: \(item)" )
                self.partsArray.append(item)
            }
        } // end if
    } // end observe()
} // end retrievePart()

现在,“打印零件阵列”显示[]

为每个成员生成语句print(snapValue)和print(“ITEM:(item)”):

  

[“tail”:N117UA,“is_priority”:0,“已收到”:2018-02-22T09:12:07S,   “can_8130”:1,“描述”:飞机上的部件,“is_hazmat”:0,   “is_esd”:1,“has_amm”:0,“partNum”:b51-09-1035,“msn”:,,   “接收者”:Jon Gravois,“db_id”:99990] ITEM:Aero_Eco.Part

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您的快照值是字典,只需输入此代码if let snapValue = snapshot.value as? [String:Any]' and inside that打印snapValue you can see all键值over there. From it get each value by key and create model and append to your partsArray`

在潜入firebase之前,您需要了解一些事项。

Firebase是基于JSON(键 - 值)实时数据库类型,允许观察任何具有多个观察者的键。

您可以在其中使用以下事件类型

typedef NS_ENUM(NSInteger, FIRDataEventType) {
    /// A new child node is added to a location.
    FIRDataEventTypeChildAdded,
    /// A child node is removed from a location.
    FIRDataEventTypeChildRemoved,
    /// A child node at a location changes.
    FIRDataEventTypeChildChanged,
    /// A child node moves relative to the other child nodes at a location.
    FIRDataEventTypeChildMoved,
    /// Any data changes at a location or, recursively, at any child node.
    FIRDataEventTypeValue
} NS_SWIFT_NAME(DataEventType);

但还有一个重要的事项是observeSingleEventOfTypeobserveEventType我的建议是你只需要访问文档https://firebase.google.com/docs/database/ios/start

如果您观察到singleEvent,它将在执行此语句时仅执行一次。 如果您观察到正常事件,只要您更改了任何子树,它就会通知您。所以建议明智地使用它,不要将观察者放在树对象的顶部。

如果只需要一次,你应该怎么做。

观察您的密钥上的单个事件,然后添加另一个childAdded

的观察者