Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we increase or decrease an integer value by 1. #270

Closed
VikashSRathore opened this issue Dec 11, 2021 · 4 comments
Closed

How can we increase or decrease an integer value by 1. #270

VikashSRathore opened this issue Dec 11, 2021 · 4 comments
Labels

Comments

@VikashSRathore
Copy link

Suppose I have a firebase node named "Likes" that contains integer values.
How can I increase or decrease the value of this key by one without fetching the value of the key.

Fetching the key may create problem like :

=> user1 fetches value = 10
=> user2 fetches value = 10
=> user1 increases value by 1 = 11
=> user2 increases value by 1 = 11

Now database will have 11 as final value but if 2 users have incremented value after 10 then it should be 12.

I hope the problem is clear.

I am using Realtime firebase database.

@bezysoftware
Copy link
Collaborator

That isn't possible with this library

@bomosura
Copy link

bomosura commented Jan 17, 2022

Hi, I got good news for you, it is possible with this library!
Here is my sample of it

Based on their documentation: https://firebase.google.com/docs/reference/rest/database#section-server-values

public async Task Run()
{
    var firebaseClient = new FirebaseClient(
            "<Your URL here>",
            new FirebaseOptions
            {
                AuthTokenAsyncFactory = () => GetAccessToken(),
                AsAccessToken = true
            });

    var dataTest = new CountTest()
    {
        Counts = FieldValue.Increment(1)
    };

    await firebaseClient
        .Child("TestCount")
        .PatchAsync(dataTest);
}


private async Task<string> GetAccessToken() //Source: https://github.com/step-up-labs/firebase-database-dotnet/issues/221#issuecomment-704583145
{
    var jsonString = await File.ReadAllTextAsync("Creds\\firebaseCredential.json");

    var credential = GoogleCredential.FromJson(jsonString).CreateScoped(new string[] {
        "https://www.googleapis.com/auth/firebase.database",
        "https://www.googleapis.com/auth/userinfo.email"
    });

    ITokenAccess c = credential;
    return await c.GetAccessTokenForRequestAsync();
}


public class CountTest
{

    public ServerValue Counts { get; set; }

}


public class FieldValue
{
    public static ServerValue Increment(long value)
    {
        return new ServerValue(new FieldValueIncrement(value));
    }

    public class FieldValueIncrement
    {
        public FieldValueIncrement(long value)
        {
            increment = value;
        }
        public long increment { get; private set; }
    } 
}

public class ServerValue
{
    public ServerValue(object value)
    {
        Value = value;
    }

    [JsonProperty(".sv")]
    public object Value { get; set; }
}

If you want to "Decrease" the integer by 1, just use FieldValue.Increment(-1)

Let me know if this works for you

@stale
Copy link

stale bot commented Jul 31, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 31, 2022
@stale
Copy link

stale bot commented Sep 21, 2022

Closing the issue due to inactivity. Feel free to re-open

@stale stale bot closed this as completed Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants