0

I'm trying to translate this other stackoverflow answer which was using the javascript sdk into a swift/obj-c ios solution.

Firebase chat - removing old messages

Here is my code:

    messagesRef?.observeEventType(FEventType.ChildAdded, withBlock: { (snapshot:FDataSnapshot!) -> Void in
        if (snapshot != nil)
        {
            msg = snapshot.value["avatar"] as? Dictionary<String,AnyObject>

        }
    })

So a few questions:

  1. it seems to fire the childadded event for all past messages, not just new events - is that the way it's supposed to work? Is there a way to limit it to just new messages/children and not already existing messages?

  2. How do I add a limit to the reference, like in the js example?

  3. How do I translate this piece of js code into swift?

sample:

messagesRef.endAt(timestamp).on("child_added", function(snap) {
   snap.ref().remove();
});
Community
  • 1
  • 1
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

1 Answers1

2

The question you refer to is pretty old, by Firebase standards. So while you can translate it to iOS, there may be better ways to accomplish what you're trying. Spending an hour or so in Firebase's iOS documentation, will probably save you a ton of headaches going forward.

  1. Yes, that is the normal behavior of Firebase. If you want only new messages, look here: Firebase child_added only get child added or How to retrieve only new data?

  2. Firebase JavaScript endAt translates to iOS queryEndingAtValue:. See https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-queries

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • so expiring content using the queryendingAtValue - is there documentation on what the special characters are in the queries (e.g. I see "b~" in the example but not sure if ~ is a special character). Also would it work with greather than or less than queries versus an exact match? I.e. I'd want to find the end at for dates say older than a day which probably won't match up to an exact equals in the query of the actual data. – MonkeyBonkey May 11 '15 at 14:42
  • There is nothing special about the `~`, except that it is pretty high up in the ASCII range. As such it will indeed work with range queries. – Frank van Puffelen May 11 '15 at 15:55
  • I went ahead and tried the js code on the server using limitToFirst instead of endAt and I get the correct set of messages but when I snap.remove() it removes the entire parent rather than just single records : `messagesRef.limitToFirst(count-n).on('child_added', function (snap) { snap.ref().remove(); });` – MonkeyBonkey May 14 '15 at 11:46
  • Is that the problem you posted here too? http://stackoverflow.com/q/30237298 Firebase queries are more like a continuous monitor, so once you remove the first message another one becomes the new first, so you remove that one and another becomes the first.... until the list is empty (at which Firebase indeed also removes the parent node, if that is empty). – Frank van Puffelen May 15 '15 at 23:24