Firebase:从未知子项中检索嵌套数据

时间:2017-05-16 11:33:17

标签: ios swift firebase firebase-realtime-database

我有像这样的firebase结构

<style>
table {
    border-collapse: collapse;
}

td {
    border-bottom: 10px solid white;
    width:100px;
    height:80px;
    vertical-align: top;
}

th {
    height:10px;
    width:100px;
}

.red {
    background-color: red;
}

.event {
    border-bottom-color: green;
}
</style>
<table>
    <thead>
        <tr>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th>Saturday</th>
            <th>Sunday</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="red event">1</td>
            <td class="event">2</td>
            <td class="event">3</td>
            <td class="event">4</td>
            <td class="event">5</td>
            <td class="event">6</td>
            <td class="event">7</td>
        </tr>
        <tr>
            <td class="event">8</td>
            <td class="event">9</td>
            <td>10</td>
            <td>11</td>
            <td>12</td>
            <td>13</td>
            <td>14</td>
        </tr>
    </tbody>
</table>

我想要检索所有具有广播的节点,其中c等于true。我怎么能在swift中做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以通过重组数据库来实现此目的:

"userBroadcasts": {
    "ghjghjFc1S3KO0y8yJwORdfgret": { // user ID
        "ryrtybgzMiI858YyGua": true, // broadcastID
        "jkhjkbgzMiI858YyGua": true, // another broadcastID
        // etc..
    },
    // more users down here...
}

"broadcast": {
    "ryrtybgzMiI858YyGua": { // broadcast ID
        "a": "xxx",   // broadcast Detail
        "b": "yyy",
        "c": false
    },
    "jkhjkbgzMiI858YyGua": {
        "a": "xxx",
        "b": "yyy",
        "c": true
    },
    // more broadcasts down here...
}

然后您可以通过以下方式获取所需的所有广播:

FIRDatabase.database().reference().child("broadcast").queryOrdered(byChild: "c").queryEqual(toValue: true)

如果您还需要userID,请在创建广告时将其存储在广播中,例如:

"broadcast": {
    "ryrtybgzMiI858YyGua": { // broadcast ID
        "a": "xxx",   // broadcast Detail
        "b": "yyy",
        "c": false,
        "userID":"ghjghjFc1S3KO0y8yJwORdfgret"
    },
    "jkhjkbgzMiI858YyGua": {
        "a": "xxx",
        "b": "yyy",
        "c": true,
        "userID":"ghjghjFc1S3KO0y8yJwORdfgret"
    },
    // more broadcasts down here...
}

使用Firebase数据库包含非规范化!