IOS Firebase sorting and restriction - observer not called

This is my data model.

project-foo -posts -KLs123412341234 key: "-KLs123412341234" message: "free cupcakes" time: 1467675688163 -... key: "..." message: "..." time: ... 

I would like to receive messages only in the last 15 minutes. I can see that a problem is being added to my Firebase Console child components, but the problem is that the observer does not seem to be called - "hello" does not print.

In my iOS application, there is the following code that does not call:

 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") }) } } 

My Android application has the following code that calls does :

 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"); ... } } 

Update : My error was not that I initialized mDatabase (in the Android version) using the posts path. The fix was to simply initialize ref with FIRDatabase.database().referenceWithPath("posts") instead of FIRDatabase.database().reference() .

+5
source share
1 answer

For the JSON structure: -

 "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 } } 

Use this code: -

  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 }) 

Make sure your security rules allow the user to retrieve posts value

Using .ChildAdded only called when a child node is added to this node database. Using .Value , you get integer data whose timestamp is between these values

Now, most likely, you cannot get the Timestamp value from FIRServerValue.timestamp() , as this is just a command sent to the firebase server to add a label to this especially node.

Instead, use a custom timestamp to save the timestamp, and for the final value, simply add 15 minutes to this timestamp.

Read This Manipulate NSDate

+1
source

Source: https://habr.com/ru/post/1258000/


All Articles