【问题标题】:Firebase: Query data start at specific valueFirebase:查询数据从特定值开始
【发布时间】:2018-05-25 16:00:34
【问题描述】:

我在聊天中使用 Firebase。

我想获取特定时间的所有消息。 但我只收到来自 Firebase 的一条消息。

这是我的代码:

[[[_conversationDBRef queryOrderedByChild:@"time"] queryStartingAtValue:@(time)] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

        for (FIRDataSnapshot* child in snapshot.children) {
            NSDictionary* msgData = child.value;
            NSLog(@"msgData: %@", msgData);
        }
    }];

查询:

(lldb) po _conversationDBRef
https://xxxxx-xxxxxx.firebaseio.com/Strike-Fighter/messages/4_5

(lldb) po [[_conversationDBRef queryOrderedByChild:@"time"] queryStartingAtValue:@(time)]
(/Strike-Fighter/messages/4_5 {
    i = time;
    sp = "548914514.2122542";
})

数据

【问题讨论】:

  • 如果您欣赏一些建议,请在时间戳中使用整数值而不是浮点数。

标签: ios objective-c firebase firebase-realtime-database


【解决方案1】:

您只收到一个值,因为您正在使用queryStartingAtValue 进行查询。如果您想收到约会的所有聊天消息。您需要使用isGreaterThan

因此,如果您想从特定日期检索消息,您需要这样做:

Firestore

[[[_conversationDBRef queryWhereField:@"time" isGreaterThan:@100000]
    queryOrderedByField:@"time"];

Firebase 实时数据库

 [[[_conversationDBRef queryWhereField:@"time" queryStartingAtValue:@100000]
        queryOrderedByChild:@"time"];

您还可以限制要检索的消息数量(例如集成延迟加载),将queryLimitedTo 添加到您的查询中。

我认为你的代码没问题。尝试使用queryOrderedByValue,并查看https://firebase.google.com/docs/database/ios/lists-of-data中的官方文档

无论如何,如果您的应用程序正在启动。我建议您将数据库更新到 Firebase Firestore。它更易于使用、更具可扩展性,并且看起来 Firebase 团队将比 REaltime 更专注于它。

希望对你有帮助!

【讨论】:

  • 嗯...我找不到函数queryWhereField
  • 我的错,我将它与 Firebase Firestore 混淆了。我正在更新我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
相关资源
最近更新 更多