2016-11-18 49 views
1

我正在構建一個2階應用程序,其中一個用於發佈訂單,另一個用於驅動程序接受,所以從另一個應用程序我無法訪問用戶標識,它幾乎無法正常工作,但很難處理Firebase ,現在我試圖向我的數據結構添加/讀取新的訂單,但無法確定如何在不創建一大堆循環來篩選數據的情況下閱讀新訂單,因此,當我檢測到數據更改時,我刪除一切,然後重新裝入所有數據,我可以改進我的Firebase結構嗎?

enter image description here

這裏是我的代碼

override func viewDidLoad() { 
    super.viewDidLoad() 





    loadData() 


    ref.observe(.childChanged, with: { snapshot in 
     print("\n\n\nthe changed user is: \(snapshot.value)\n\n\n") 
     self.usrData.removeAll() //if change is detected, remove  everything then readd them 

     self.loadData() //load firebase Data to my data structure 
    }) 

} 

這裏是我的負載數據方法

func loadData(){ 

    ref.observe(.childAdded, with: { snapshot in 
     if let result = snapshot.children.allObjects as? [FIRDataSnapshot] { 
      print("snapshot.value is snapshot \(snapshot.valueInExportFormat())") 

      if let tes = snapshot.value as? Dictionary<String, AnyObject> { 
       print("snapshot dictionarty is snapshot \(tes.reversed())") 



       let est = tes.reversed() 

       for t in est { 
        print("snapshot dictionarty name snapshot \(t.value["name"] as! String)") 
        print("snapshot dictionarty name snapshot \(t.value["email"] as! String)") 
        print("snapshot dictionarty name key \(t.key)") 



        let usr = userData(name: t.value["name"] as! String, email: t.value["email"] as! String, mobile: "", date: 0, latitude: "", longititude: "", ordertxt: "", status: "", usrKey:t.key, orderNumb:"") 

        self.usrData.append(usr) 

       } 

      } 

      var count = 0 


      for child in result { 

       let dict = child.childSnapshot(forPath: "orders").value as! Dictionary<String, AnyObject> 
       print("dict is \(dict)") 


       for dic in dict { 

        if (dic.value["status"] as! String) == "pending" { 

         self.usrData[count].mobile = dic.value["mobile"] as! String 
         self.usrData[count].date = dic.value["date"] as! NSNumber 
         self.usrData[count].latitude = dic.value["latitude"] as! String 
         self.usrData[count].longititude = dic.value["longitude"] as! String 
         self.usrData[count].ordertxt = dic.value["orderText"] as! String 
         self.usrData[count].status = dic.value["status"] as! String 
         self.usrData[count].orderNumb = dic.key 

         print("\(count) dic key is\(dic.key)") 

        } else { 
         print("\n\n\n status isn't pending for some reason -> \(self.usrData[count].status)") 
         self.usrData.remove(at: count) 
         count-=1 
         continue 
        } 

       } 
       count += 1 
      } 
     } else { 
      print("\n\n result is nil or something\n\n"); 
     } 


     self.tableView.reloadData() 

    }) 



} 

反正要改進嗎?

回答

1

你可以試試這個: -

users:{ 

    userID1 :{...// DATA 

    ORDERS :{ 
     orderID1 : true, 
     orderID2 : true, 
     orderID3 : true 
     } 
     }, 
    userID1 :{...// DATA 

    ORDERS :{ 
     orderID1 : true, 
     orderID2 : true, 
     orderID3 : true 
     } 
     }, 
    userID1 :{...// DATA 

    ORDERS :{ 
     orderID1 : true, 
     orderID2 : true, 
     orderID3 : true 
     } 
     } 

    orderes_Placed :{ 

    orderID1 : {//order details}, 
    orderID2 : {//order details}, 

    } 
} 

應避免在您的NoSQL數據結構嵌套,並嘗試儘可能多的展平越好,因爲它會增加你的帶寬消耗當您導航。通過你的深層節點。

要收聽newData添加到您的數據庫節點,只是(.observe(.childAdded ..)在那節點啓動一個傾聽者。

FIRDatabase.database().reference().child("orders_Placed").observe(.childAdded, with: {(Snapshot) in 


     print(Snapshot) // Will print out every new order that is placed via your app. 

    }) 
相關問題