Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use Firebase update function for updating priority as well

Tags:

firebase

I am updating some properties of my object in firebase using the update method. As part of the update call, can i also update the priority of my object?

I have a collection of objects stored in firebase. I am using the Javascript sdk to interact with my data. Whenever i update any property of an object, i set the priority of the object to Firebase.ServerValue.TIMESTAMP constant.

Now, i want to start storing the priority of each object in a property called UpdatedOnTimestampTs defined on the object. Whenever i update any property i will also update UpdatedOnTimestampTs with TIMESTAMP constant.

Now the problem is, I am unable to find a way to call the update function along with priority (no updateWithPriority like setWithPriority). So if I update the UpdatedOnTimestampTs and priority in two different operations using the TIMESTAMP constant i may (or will) end up with a different values.

How can i address this?

like image 812
Kartik Avatar asked Sep 04 '13 12:09

Kartik


People also ask

How to update data in Firebase FireStore in Android?

How to Update Data in Firebase Firestore in Android? Step 1: Creating a new Activity for updating the data For creating a new Activity navigate to the app > res > layout >... Step 2: Updating our Modal Class where we were storing our data In previous articles, we have seen creating our Modal... Step ...

How to implement Firebase Cloud Functions?

Implementation path Set up Cloud Functions Install the Firebase CLI and initialize ... Write functions Write JavaScript code (or TypeScript cod ... Test functions Use the local emulator to test your func ... Deploy and monitor Enable billing for your project and depl ...

How do I deploy a project to Firebase?

Write JavaScript code (or TypeScript code to transpile at deployment) to handle events from Firebase services, Google Cloud services, or other event providers. Use the local emulator to test your functions. Enable billing for your project and deploy your functions using the Firebase CLI.

How do I enable billing for my Firebase project?

Enable billing for your project and deploy your functions using the Firebase CLI. You can use the Firebase console to view and search through your logs. Get started setting up, creating, and deploying functions.


1 Answers

I know I'm way late on an answer here, but I'm just coming across the need for something similar now and updateWithPriority doesn't exist (nor a way to easily view/edit priorities in the Forge).

You can include the specially-named key, .priority in your update and it ought to achieve the desired effect.

The .priority key is documented in the Firebase REST API, but can be used in the other libraries.

So, in Javascript, the following would work if you only wanted to update the priority (which, there is setPriority for that):

var fb = new Firebase("http://a_firebase.firebaseio.com");
var ref = fb.child("/path/to/child");
var data = { ".priority": (new Date()).getTime() } //timestamp, as you mentioned
ref.update(data);

If you wanted to change the priority while you're updating other properties, which is what you had asked, just include it with the data you want to update:

var fb = new Firebase("http://a_firebase.firebaseio.com");
var ref = fb.child("/path/to/child");
var data = { 
    "property1": "updated_val",
    "property2": "updated_val2",
    ".priority": (new Date()).getTime(), //timestamp, as you mentioned
}
ref.update(data);

EDIT: To be clear, this will update the priority at the location of the child reference (i.e. /path/to/child) not the priorities of the individual children that are being updated (i.e. priorities of property1 and property2). If you wanted to include a priority for the children that are being updated, and the children are leaf nodes (i.e. string, number, boolean), then you can use the specially-named .value key in conjunction with the .priority key to do so.

The following example shows how to update the priority for the root location that the update is occurring from (i.e. path/to/child) as well as for both the children that are being update (i.e. property1 and property2). Obviously, you could selectively pick which priorities to update if you didn't want to update them all.

var fb = new Firebase("http://a_firebase.firebaseio.com");
var ref = fb.child("/path/to/child");
var newPriority = (new Date()).getTime(); //timestamp, as you mentioned
var data = { 
    "property1": {".value": "updated_val", ".priority": newPriority},
    "property2": {".value": "updated_val2", ".priority": newPriority},
    ".priority": newPriority
}
ref.update(data);
like image 158
MandM Avatar answered Oct 18 '22 03:10

MandM