how do i auto increment a value in firebase

家住魔仙堡 提交于 2019-12-08 07:31:13

问题


im currently working on an app that downloads files using the firebase storage and firebase database to store the url linking to the storage. the fire base database tree looks a bit like this

Apps | app1 | name my app download 1 url http://link to download about About the app your downloading image image url for the app your downloading downloads = 0 | app2 (Same data as above)

i have custom base adapter which allows me to add the image url and about value. when somebody click an item in my list it downloads and installs the app you selected, by grabbing the position of the item and then grabbing the url from the database using httpget.

what id like is when they click the item it auto increments the child downloads by 1 so it acts as a download counter.

 mrootRef = new Firebase("https://admob-app-id-3020090926.firebaseio.com/Apps");
    mlv = (ListView) findViewById(R.id.lvFilmapps);
    mrootRef.child("Filmapps").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            final String appname = dataSnapshot.child("name").getValue(String.class);
            final String url = dataSnapshot.child("url").getValue(String.class);
            final String apkname = dataSnapshot.child("apk").getValue(String.class);
            final String about = dataSnapshot.child("about").getValue(String.class);
            final String appimg = dataSnapshot.child("image").getValue(String.class);


            FilmArray.add(appname);
            urlList.add(url);
            ApkList.add(apkname);
            Aboutapp.add(about);
            appImage.add(appimg);

            //String[] convertion  for the BaseAdpater
            String[] arr = FilmArray.toArray(new String[FilmArray.size()]);
            String[] arr1 = Aboutapp.toArray(new String[Aboutapp.size()]);
            String[] arr2 = appImage.toArray(new String[appImage.size()]);

            mlv.setAdapter(new dataListAdapter(arr,arr1,arr2));
            new dataListAdapter(arr,arr1,arr2).notifyDataSetChanged();

            mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                     newnamestring = ApkList.get(i).toString();
                    apkNames = FilmArray.get(i).toString();
                    new DownloadFileFromURL().execute(urlList.get(i));
                }
            });
        }

回答1:


I think you might want to look at Transaction. If reference is your FirebaseReference pointing to the number to be incremented, you can do something like this:

reference.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData mutableData) {
        if (mutableData.getValue() == null) {
            mutableData.setValue(1);
        } else {
            int count = mutableData.getValue(Integer.class);
            mutableData.setValue(count + 1);
        }
        return Transaction.success(mutableData);
    }

    @Override
    public void onComplete(DatabaseError databaseError, boolean success, DataSnapshot dataSnapshot) {
        // Analyse databaseError for any error during increment
    }
});

Note that two Transactions can't be executed at the same time, so you are sure that the count is updated properly. You can have more details here.



来源:https://stackoverflow.com/questions/41838131/how-do-i-auto-increment-a-value-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!