Accessing MultiLevel Nested Children in Firebase Database Swift 3

北城余情 提交于 2019-12-13 04:34:01

问题


I am using Firebase Realtime Database.

I have a customer id which corresponds to customer table. I need to fetch its respective apartment name. Then search the record having same apartment name in apartment table. Once found, I need to get all serviceType values from its respective segments. Also need to fetch its block from apartment table.

Table structure is as follows:

customer
  -L1x2AKUL_KNTKXyza
    apartment_name:"ABC Residency"


appartment
  -L1Ohec4nW-ya_SkiG49
    apartment_name:"ABC Residency"
    block: "A Wing"
    segments
      -L1OhecGtEk_8xdNs67T
        serviceType:"Mopping"
      -L1OhecGtEk_8xdNs631
        serviceType:"Cleaning"

I want to use only one firebase database reference object and achieve this multilevel access.

Any help would be greatly appreciated!


回答1:


This should help you. I've created a class called CRUD to perform all of my firebase functions.

import Firebase

/**
 CRUD Object to maintain Firebase DB
 - MUST use crud.creatRef() before any save or read functions!!
 */
class CRUD: NSObject {
    var ref: DatabaseReference!
    var id: String!

    /**Create FirebaseDB reference for use of CRUD Object to interact with DB*/
    func createRef() {
        id = PageDataSource.sharedInstance.myID!
        ref = Database.database().reference(fromURL: "https://yourURL.firebaseio.com/")
    }

}

When calling CRUD to save or load data within my app I have to do this:

let crud = CRUD()
crud.createRef()
crud.doSomething()

I've had great success with this. You'll need to design your model and implement the rest of the code accordingly. When you create your save functions they should go inside of the CRUD object.



来源:https://stackoverflow.com/questions/48961835/accessing-multilevel-nested-children-in-firebase-database-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!