使用iOS Swift在Firebase中进行高级查询和过滤

时间:2021-09-25 16:50:06

I'm building ticketing app using Firebase Database. Here's my database format: 使用iOS Swift在Firebase中进行高级查询和过滤

我正在使用Firebase数据库构建票务应用程序。这是我的数据库格式:

if I write the code like this:

如果我写这样的代码:

let ref = FIRDatabase.database().reference()

    ref.child("jadwal")
        .queryOrderedByChild("tanggal")
        .queryEqualToValue("17/08/2016")
        .observeEventType(.ChildAdded, withBlock: { (snapshot) in

        print(snapshot)

    }, withCancelBlock: nil)

the output will be:

输出将是:

Snap (-KPMQefHSuk7j9kb3GTJ) {
   asal = BNA;
   harga = 177000;
   idBus = id;
   jam = "20:00";
   tanggal = "17/08/2016";
   tersedia = 35;
   tujuan = BRN;
}
Snap (-KPMQnCGrXY36NEMxSOU) {
    asal = BNA;
    harga = 150000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 37;
    tujuan = MDN;
}
Snap (-KPMQqFmku_XzItElzjV) {
    asal = BNA;
    harga = 160000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 40;
    tujuan = MDN;
}

it will show all the "tanggal" with the value equal to "17/08/2016", when i do this...

它将显示所有“tanggal”,其价值等于“17/08/2016”,当我这样做时......

let ref = FIRDatabase.database().reference()

    ref.child("jadwal")
        .queryOrderedByChild("harga")
        .observeEventType(.ChildAdded, withBlock: { (snapshot) in

        print(snapshot)

    }, withCancelBlock: nil)

it will show all the "harga" ordered from low to high like this:

它将显示从低到高排序的所有“harga”,如下所示:

Snap (-KPNgVeXXnqnzevh5w6u) {
     asal = BNA;
     harga = 120000;
     idBus = id;
     jam = "22:30";
     tanggal = "30/08/2016";
     tersedia = 38;
     tujuan = TKG;
}
Snap (-KPMQnCGrXY36NEMxSOU) {
    asal = BNA;
    harga = 150000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 37;
    tujuan = MDN;
}
Snap (-KPMQqFmku_XzItElzjV) {
    asal = BNA;
    harga = 160000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 40;
    tujuan = MDN;
}
Snap (-KPMQefHSuk7j9kb3GTJ) {
    asal = BNA;
    harga = 177000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 35;
    tujuan = BRN;
}
Snap (-KPNgJPRlEuu0FZvjU2J) {
    asal = BNA;
    harga = 180000;
    idBus = id;
    jam = "21:30";
    tanggal = "25/08/2016";
    tersedia = 40;
    tujuan = MDN;
}
Snap (-KPNgQ2qQXQpY4eT2Jqd) {
    asal = BNA;
    harga = 230000;
    idBus = id;
    jam = "22:30";
    tanggal = "30/08/2016";
    tersedia = 38;
    tujuan = BNJ;
}

see that the "harga" is sorted but i need to show the list of snapshots which shows "harga" is sorted and only "tanggal" is "17/08/2016". i'm always failed to do this and the error mostly with "multiple orderBy()", i need to query and filter data based on two or more child values. Basically I want to sort based on "harga", filter based on "tanggal", "asal", "tujuan", and minimun number of "tersedia" Please help me i want to switch my db from SQL to Firebase. Thank you.

看到“harga”已经排序,但我需要显示快照列表,其中显示“harga”已排序,只有“tanggal”是“2016/08/08”。我总是没有做到这一点,错误主要是“multiple orderBy()”,我需要根据两个或多个子值查询和过滤数据。基本上我想基于“harga”排序,基于“tanggal”,“asal”,“tujuan”和最小数量的“tersedia”进行排序请帮助我,我想将我的数据库从SQL切换到Firebase。谢谢。

1 个解决方案

#1


1  

There is currently no way to query by more than one child key in Firebase. I would suggest two potential solutions to this:

目前无法通过Firebase中的多个子键进行查询。我建议两个可能的解决方案:

First solution: Flatten your database structure.

第一个解决方案:展平数据库结构。

Explanation: Instead of just a "jadwal" section with all the children, implement a structure like this:

解释:不是只包含所有子节点的“jadwal”部分,而是实现如下结构:

jadwal: {
    "17/08/2016": {
        harga:
    },
    "17/07/2016": {
        harga:
    }
}

Then, you could choose the path according to which "tanggal" to filter by, then sort the childen by the "harga" value.

然后,您可以根据过滤的“tanggal”选择路径,然后按“harga”值对孩子进行排序。

Second solution: Do some filtering on client side

第二种解决方案:在客户端进行一些过滤

Explanation: Do you query in jadwal using the orderBy("harga"), then on the childAdded listener on client side, filter the results. This means you would check if the child has the intended tanggal. If it does, add it to results array. Otherwise, move on to next child.

说明:您是使用orderBy(“harga”)在jadwal中查询,然后在客户端的childAdded侦听器上查询结果。这意味着您将检查孩子是否有预期的唐格。如果是,请将其添加到结果数组中。否则,继续前进到下一个孩子。

Let me know if you have any more questions.

如果您还有其他问题,请与我们联系。

#1


1  

There is currently no way to query by more than one child key in Firebase. I would suggest two potential solutions to this:

目前无法通过Firebase中的多个子键进行查询。我建议两个可能的解决方案:

First solution: Flatten your database structure.

第一个解决方案:展平数据库结构。

Explanation: Instead of just a "jadwal" section with all the children, implement a structure like this:

解释:不是只包含所有子节点的“jadwal”部分,而是实现如下结构:

jadwal: {
    "17/08/2016": {
        harga:
    },
    "17/07/2016": {
        harga:
    }
}

Then, you could choose the path according to which "tanggal" to filter by, then sort the childen by the "harga" value.

然后,您可以根据过滤的“tanggal”选择路径,然后按“harga”值对孩子进行排序。

Second solution: Do some filtering on client side

第二种解决方案:在客户端进行一些过滤

Explanation: Do you query in jadwal using the orderBy("harga"), then on the childAdded listener on client side, filter the results. This means you would check if the child has the intended tanggal. If it does, add it to results array. Otherwise, move on to next child.

说明:您是使用orderBy(“harga”)在jadwal中查询,然后在客户端的childAdded侦听器上查询结果。这意味着您将检查孩子是否有预期的唐格。如果是,请将其添加到结果数组中。否则,继续前进到下一个孩子。

Let me know if you have any more questions.

如果您还有其他问题,请与我们联系。