我如何在Firebase中自动增加一个值 [英] how do i auto increment a value in firebase

查看:66
本文介绍了我如何在Firebase中自动增加一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im目前正在开发一个应用程序,该应用程序使用firebase存储器和firebase数据库下载文件,以存储链接到存储器的URL.火力基地数据库树看起来像这样

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

应用 | app1 | 命名我的应用下载1 网址 http://link 下载 关于关于您下载的应用程序 您下载的应用程序的图片图片网址 下载= 0 | app2 (与上述数据相同)

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)

我有自定义的基本适配器,它允许我添加图像url和大约值. 当有人单击我列表中的项目时,它会抓住项目的位置,然后使用httpget从数据库中获得URL,从而下载并安装所选的应用程序.

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.

当他们单击该项目时,它的ID是什么,它会自动将子级下载次数增加1,从而充当下载计数器.

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));
                }
            });
        }

推荐答案

我认为您可能想看看Transaction.如果reference是您的FirebaseReference指向要递增的数字,则可以执行以下操作:

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
    }
});

请注意,不能同时执行两个Transaction,因此您可以确保正确更新了计数.您可以此处.

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.

这篇关于我如何在Firebase中自动增加一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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