Firebase更新儿童字段的值 [英] Firebase updating value for a field in a child

查看:107
本文介绍了Firebase更新儿童字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
posts:{
-KNnbTHJGm83zpSELWwB:{
.priority:6gydj46kg0,
branch:Branch,
category:Categoria,
expiration:24h,
g:6gydj46kg0,
l:[-23.5415083,-46.88642],
photo:mPostPic_20160728192627.jpeg ,
price:10,
priority:0,
product:ipad,
userKey:BNd8RP5qHoc0q7f9gn8cLjPy5Ux1,
offerMessage:Valido吃了14:00 hs,
likeCount:0
}
}
}
pre>

我只需要读取likeCount值,并在特定情况下将其值加1即可。



阅读了大量的Firebase文档和示例,其中大部分示例与推送数据或读取数据和处理值有关。

我只需要读取值,增量, SA ve值,但我是新手在Firebase中,并没有找到一个有效的方式来做到这一点。

解决方案

2文件,现在下面的代码工作正如我所料。

  public void writeUserSawPost(final String postkey,int mode){


最终DatabaseReference mPostsRef = mDatabase.child(posts /+ postkey);
String user = mAuth.getCurrentUser()。getUid();
final DatabaseReference mPostUser = mDatabase.child(posts-user /+ user +/+ postkey +/ sentiment);

if(mode == Constants.LIKE){



mPostsRef.runTransaction(new Transaction.Handler(){
@覆盖
public Transaction.Result doTransaction(MutableData mutableData){
Post p = mutableData.getValue(Post.class);
if(p == null){
return Transaction。成功(mutableData);
}
p.likeCount = p.likeCount + 1;
mutableData.setValue(p);
返回Transaction.success(mutableData);

$ b @Override
public void onComplete(DatabaseError databaseError,boolean b,
DataSnapshot dataSnapshot){
//完成的事务


}
});


mPostUser.setValue(like);

} else if(mode == Constants.DISLIKE){
mPostUser.setValue(dislike);
}



}


I have a Child in Firebase like this in my ANDROID project:

{
  "posts" : {
    "-KNnbTHJGm83zpSELWwB" : {
      ".priority" : "6gydj46kg0",
      "branch" : "Branch",
      "category" : "Categoria",
      "expiration" : "24h",
      "g" : "6gydj46kg0",
      "l" : [ -23.5415083, -46.88642 ],
      "photo" : "mPostPic_20160728192627.jpeg",
      "price" : 10,
      "priority" : 0,
      "product" : "ipad",
      "userKey" : "BNd8RP5qHoc0q7f9gn8cLjPy5Ux1",
      "offerMessage": "Valido ate as 14:00 hs",
      "likeCount": 0
    }
  }
}

I just need to read the likeCount value and increment it by 1 in specific situation.

I have read a lot of Firebase documentation and sample and most part of examples is related to push data or read data and handle value changed.

I just need to read value, increment, save value again but I am newbie in Firebase and not finding a efficient way to do that.

解决方案

I have splited my Json in 2 files and now the code below is working as i expected.

public void writeUserSawPost(final String postkey, int mode){


        final DatabaseReference mPostsRef = mDatabase.child("posts/" + postkey);
        String user = mAuth.getCurrentUser().getUid();
        final DatabaseReference mPostUser = mDatabase.child("posts-user/" + user + "/" + postkey + "/sentiment");

        if (mode == Constants.LIKE) {



            mPostsRef.runTransaction(new Transaction.Handler() {
                @Override
                public Transaction.Result doTransaction(MutableData mutableData) {
                    Post p = mutableData.getValue(Post.class);
                    if (p == null) {
                        return Transaction.success(mutableData);
                    }
                    p.likeCount = p.likeCount + 1;
                    mutableData.setValue(p);
                    return Transaction.success(mutableData);
                }

                @Override
                public void onComplete(DatabaseError databaseError, boolean b,
                                       DataSnapshot dataSnapshot) {
                    // Transaction completed


                }
            });


            mPostUser.setValue("like");

        } else if (mode == Constants.DISLIKE){
            mPostUser.setValue("dislike");
        }



    }

这篇关于Firebase更新儿童字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆