如何通过Firebase中的值查询所有ID

时间:2016-04-18 05:54:06

标签: swift firebase

以下是我在Firebase上的数据

  {
  "messages" : {
    "-KFdphRHtKSovYfCRYWE" : {
      "senderId" : "95cf99d8-377b-4685-a8ee-2afe69a82419",
      "text" : "Chechen"
    },
    "-KFdpisBYxFaSrm2CO67" : {
      "senderId" : "425c9449-b3f7-4d21-b57c-4134b76712dd",
      "text" : "sdd"
    },
    "-KFdpmKXuRrzDnl_JRmF" : {
      "senderId" : "425c9449-b3f7-4d21-b57c-4134b76712dd",
      "text" : "c dđcv"
    },
    "-KFdpnSJIX48yh1KpjFo" : {
      "senderId" : "95cf99d8-377b-4685-a8ee-2afe69a82419",
      "text" : "Bilbo,m"
    }
  },
  "tasks" : {
    "-KEPuc8aGpYLT0-NOo50" : {
      "assignee" : "Kaito Kid",
      "description" : "Task1",
      "endTime" : "13:58 Thursday,07/04",
      "from" : "Khuong Pham",
      "priority" : 1,
      "progress" : 10,
      "startTime" : "13:58 Tuesday,05/04"
    },
    "-KFbsI0Q6WB5dWc90pIR" : {
      "assignee" : "Khoa Pham",
      "description" : "Task2",
      "endTime" : "12:38 Thursday,21/04",
      "from" : "Khuong Pham",
      "priority" : 2,
      "progress" : 0,
      "startTime" : "12:38 Wednesday,20/04"
    },
    "-KFdngdnHyfG2pygRx3L" : {
      "assignee" : "Khoa Pham",
      "description" : "Task3",
      "endTime" : "21:37 Wednesday,20/04",
      "from" : "Khuong Pham",
      "priority" : 0,
      "progress" : 10,
      "startTime" : "21:37 Tuesday,19/04"
    },
    "-KFdo-Q8HrrPZoaQcGRj" : {
      "assignee" : "Khuong Pham",
      "description" : "task4",
      "endTime" : "23:44 Thứ Hai,18/04",
      "from" : "Khoa Pham",
      "priority" : 1,
      "progress" : 7,
      "startTime" : "21:41 Thứ Hai,18/04"
    },
    "-KFdpXmuGrLOBonb976Q" : {
      "assignee" : "Khoa Pham",
      "description" : "Task5",
      "endTime" : "21:46 Friday,22/04",
      "from" : "Khuong Pham",
      "priority" : 2,
      "progress" : 0,
      "startTime" : "21:46 Tuesday,19/04"
    },
    "-KFdpdnPb_BVIDQ32Ym6" : {
      "assignee" : "Khuong Pham",
      "description" : "task6",
      "endTime" : "23:46 Thứ Hai,18/04",
      "from" : "Khoa Pham",
      "priority" : 1,
      "progress" : 0,
      "startTime" : "23:46 Thứ Hai,18/04"
    }
  },
  "typingIndicator" : {
    "95cf99d8-377b-4685-a8ee-2afe69a82419" : false
  },
  "users" : {
    "33ab67aa-6357-4f3e-aee8-623e294c7de2" : {
      "dateJoin" : "19/04/2016",
      "dateOfBirth" : "01/01/1990",
      "firstName" : "Binh",
      "gender" : "Male",
      "group" : "PHP ",
      "lastName" : "Nguyen",
      "phone" : "0123456789",
      "position" : "PHP Developer"
    },
    "425c9449-b3f7-4d21-b57c-4134b76712dd" : {
      "dateJoin" : "01/01/2010",
      "dateOfBirth" : "03/03/1990",
      "firstName" : "Khoa",
      "gender" : "Male",
      "group" : "iOS Team",
      "lastName" : "Pham",
      "phone" : "0939133994",
      "position" : "Senior iOS Developer"
    },
    "95cf99d8-377b-4685-a8ee-2afe69a82419" : {
      "dateJoin" : "20/01/2016",
      "dateOfBirth" : "29/10/1993",
      "firstName" : "Khuong",
      "gender" : "Male",
      "group" : "iOS Team",
      "lastName" : "Pham",
      "phone" : "01655961231",
      "position" : "iOS Developer"
    },
    "f88a6c6f-cbc8-4d37-a7d9-4ee4c3a6fd32" : {
      "dateJoin" : "13/03/2016",
      "dateOfBirth" : "01/01/1993",
      "firstName" : "Kaito",
      "gender" : "Male",
      "group" : "Android Team",
      "lastName" : "Kid",
      "phone" : "0939133994",
      "position" : "Android Developer"
    }
  }
}

我想查询所有ID"来自"有价值的关键" Khuong Pham"。

FIREBASE_REF.childByAppendingPath("tasks").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
            print(snapshot.key) // all IDs
            print(snapshot.value)
            // Then Use this value to compare with "from" key has value "Khuong Pham"
            // Then find the ID of it.
        }) 

如果我这样做,我必须花更多的时间来过滤它们。

无论如何都要更快地查询我能获得ID。

任何帮助将不胜感激。感谢。

更新

以下是代码:

func deleteTaskInFireBase() {
      FIREBASE_REF.childByAppendingPath("tasks").queryOrderedByChild("from").observeEventType(.ChildAdded, withBlock: { snapshot in
        if let from = snapshot.value["from"] as? String where (snapshot.value["from"] as? String) == "Khuong Pham" {
            print("\(snapshot.key): \(from)")
        }
    })

}

结果是:

-KEPuc8aGpYLT0-NOo50: Khuong Pham
-KFbsI0Q6WB5dWc90pIR: Khuong Pham
-KFdngdnHyfG2pygRx3L: Khuong Pham
-KFdpXmuGrLOBonb976Q: Khuong Pham

0 个答案:

没有答案