-1

My app has bits of information that exist for 24 hrs. This information has the potential to be voted on by other users. The number of votes is recorded in the database. If someone votes on a piece of information, I want it to be updated only if the path currently exists, as the data may have reached the 24 hour limit and been deleted since the display time and the vote time.

The problem with using something like Datasnapshot.hasChild is that I will need to write 2 separate read and write instructions. The data may exist for the read instruction but then may reach the 24 hour mark and be deleted before the write instruction.


This is the structure of my database, the node status is duplicated in another part of the database in order to reduce the amount of reads. If this node becomes nil, the other one still exists, but this node is here so that I can do one write to obtain all the newest statuses that are less than 24 hours old.

I would like a rule that does not allow the value of votes to change if the key of status has changed, or if status is no longer a node.

enter image description here

Sam
  • 1,765
  • 11
  • 82
  • 176
  • Are you using the real-time database? are you saying that the information might be on the user device but no longer on the server when they are voting? – STerrier Nov 18 '19 at 01:58

1 Answers1

1

There are a couple of ways to approach this.

One way is to leverage Firebase Rules. Before doing that, let me first say this to keep the Firebasers happy:

Rules are not filters

However, you can craft a rule that will prevent a write if a certain node does not exist. I would need to know your specific structure and use case to suggest a solution but Rules are pretty well covered in the Firebase Rules Docs andReferencing Data In Other Paths is a place to start.

But, a super clean an easy option is the following code that will only write to a node if it exists. One read and write.

func onlyWriteIfNodeExists() {
    let ref = your_firebase.ref.child("may_not_exist")
    ref.observeSingleEvent(of: .value, with: { snapshot in
        if snapshot.exists() {
            snapshot.ref.setValue("updated value")
        } else {
            print("node didn't exist")
        }
    })
}

and the structure would be

firebase_root
   may_not_exist: "some value"

so if the node may_not_exist exists, "some value" will be replaced with "updated value" otherwise will will print "node didn't exist" to console.

That being said, if the intention is to not allow users to vote on items that don't exist, the UI should reflect that. In other words, if the app presents topics to vote on and a topic goes out of scope, the app should receive an event of that and perhaps remove it from the UI or draw a line through the topic heading to indicate it's no longer available.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • I updated my question to show my database structure and explain a little bit more what I'm trying to do. I think a rule is what I'm looking for. – Sam Nov 20 '19 at 21:28