I really enjoy coding with Firebase. It's a great backend with a variety of different toolsets. But I miss a simple way to check a path for updated data when persistance is enabled. I think that this is not a seldom usecase, because I often need my app to act in a certain way depending on the latest server data, that only need to be read once.
I would usually use observeSingleEventOfType
, but it's quite useless when perisistance is enabled, since it will never retrieve the latest server data. Which I don't understand why. There should be an option added to skip the local cache and only look for server data.
Disabling persistance solves this problem and observeSingleEventOfType
will work as expected. But that would mean that one needs to reimplement all the offline capabilities on his own.
One example:
// chats contain meta information about the chat like last message and count of unread messages
let chatRef = ref.child("chats").child(receiverId).child(chatId)
chatRef.observeSingleEventOfType(.Value, withBlock: { (snapshot) -> Void in
if !snapshot.exists() {
print("snapshot does not exist")
// the other side has deleted the chat
// now delete all messages and member objects
ref.child("messages").child(chatId).setValue(nil)
ref.child("members").child(chatId).setValue(nil)
} else {
print("snapshot exists")
}
})
I also tried chatRef.keepSynced(true)
before observing for events with no luck. Which doesn't make sense in all situations anyway:
func removeOlderMessages() {
let dateInThePast = NSDate().addDays(-30).timeIntervalSince1970 * 1000
self.messagesRef.queryOrderedByChild("timestamp")
.queryEndingAtValue(dateInThePast)
.observeSingleEventOfType(.Value, withBlock: { (snapshot) -> Void in
snapshot.ref.removeValue()
})
}
Using keepSynced
here would lead to downloading all messages in messagesRef
, which is not wanted at all.
So is there a clever workaround for these two scenarios? Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire