lf2*_*215 5 ios firebase swift firebase-realtime-database
这是我的数据模型.
project-foo
-posts
-KLs123412341234
key: "-KLs123412341234"
message: "free cupcakes"
time: 1467675688163
-...
key: "..."
message: "..."
time: ...
Run Code Online (Sandbox Code Playgroud)
我想在过去的15分钟内只收到帖子.我在我的Firebase控制台中看到添加了子项,但问题是似乎没有调用观察者 - "hello"不打印.
我的iOS应用程序具有以下代码,不会被调用:
class ViewController: UIViewController {
var ref: FIRDatabaseReference?
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
ref!.queryOrderedByChild("time")
.queryStartingAtValue(startTime())
.observeEventType(.ChildAdded, withBlock: { snapshot in
print("hello")
})
}
}
Run Code Online (Sandbox Code Playgroud)
我的Android应用程序具有下面的代码,它不会被调用:
Query query = mDatabase.orderByChild("time").startAt((double)startTime());
query.addChildEventListener(new PostListener(this, mMap));
class PostListener implements ChildEventListener {
public PostListener(Context context, GoogleMap map) {
...
}
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "hello");
...
}
}
Run Code Online (Sandbox Code Playgroud)
更新:我的错误是没有意识到我已经mDatabase用路径初始化(在Android版本中)posts.修复只是ref用FIRDatabase.database().referenceWithPath("posts")而不是初始化FIRDatabase.database().reference().
对于 JSON 结构,:-
"posts" : {
"autoID1" : {
"key" : "autoID1",
"timestamp" : 101
},
"autoID2" : {
"key" : "autoID2",
"timestamp" : 102
},
"autoID3" : {
"key" : "autoID3",
"timestamp" : 103
},
"autoID4" : {
"key" : "autoID4",
"timestamp" : 104
},
"autoID5" : {
"key" : "autoID5",
"timestamp" : 105
},
"autoID6" : {
"key" : "autoID6",
"timestamp" : 106
},
"autoID7" : {
"key" : "autoID7",
"timestamp" : 107
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码:-
FIRDatabase.database().reference().child("posts").queryOrderedByChild( "timestamp").queryStartingAtValue(101).queryEndingAtValue(103).observeEvenType(.Value, with: { (snap) in
if let snapDict = snap.value as? [String:AnyObject]{
for each in snapDict{
print(each.value)
}
}
}, withCancel: {(err) in
})
Run Code Online (Sandbox Code Playgroud)
确保您的安全规则允许用户检索posts值
.ChildAdded仅当子节点添加到该数据库节点时才会调用 using 。使用.Value将检索时间戳在这些值之间的整个数据
现在很可能您无法从中获取时间戳值, FIRServerValue.timestamp()因为它只是发送到 firebase 服务器以在该特定节点添加时间戳的命令。
因此,请使用自定义时间戳来存储时间戳,并且对于最终值,只需在该时间戳上添加 15 分钟即可。
阅读此操作 NSDate
| 归档时间: |
|
| 查看次数: |
1176 次 |
| 最近记录: |