简体   繁体   English

如何在删除导致无限循环的对象时修复基于Android的Firebase错误?

[英]How to fix android based Firebase error while deleting objects causing an infinite loop?

I am developing an app using Firebase. 我正在使用Firebase开发应用程序。 In one of my activities, I have added a delete functionality. 在我的一项活动中,我添加了删除功能。 But when I delete a node and monitor the behaviour in the Firebase console. 但是,当我删除节点并在Firebase控制台中监视行为时。

First it deletes the object, adds 2 more. 首先,它删除对象,再添加2个对象。 Then deletes the 2, adds 3-4 more. 然后删除2,再增加3-4。 This operation continues to such an extent that the device becomes unstable (gets stuck in an infinite loop). 此操作持续到设备变得不稳定(陷入无限循环)的程度。 The app causes the screen to go completely black. 该应用程序导致屏幕完全变黑。 Nothing works. 什么都没有。 Logcat shows "too much output to process." Logcat显示“有太多输出要处理”。 Navigation buttons takes the user back the the unstable app. 导航按钮将用户带回不稳定的应用程序。

It does not Uninstall. 它不会卸载。 The only solution is to force reboot it. 唯一的解决方案是强制重新启动它。 The code used to do this operation was 用于执行此操作的代码是

mChatsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            chatToDelete = ((TextView) view.findViewById(R.id.sender_roll_text_view)).getText().toString().trim();
            final AlertDialog.Builder builder = new AlertDialog.Builder(ChatsActivity.this);
            builder.setMessage("Delete Chat?").setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Query query = mDatabaseReference.orderByChild("roll").equalTo(chatToDelete);
                    query.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                                dataSnapshot1.getRef().removeValue();
                                chats.remove(position);
                                mChatsAdapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {}
                    });
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.show();
            return true;
        }
    });

This code was working fine just yesterday. 这段代码昨天才运行良好。 But now it is a huge problem. 但是现在这是一个巨大的问题。 So, please do help if you figure it out. 因此,如果您发现问题,请提供帮助。 Thanks. 谢谢。

I think it happen because you use addValueEventListener instead of addListenerForSingleValueEvent . 我认为这是因为您使用addValueEventListener而不是addListenerForSingleValueEvent By using addValueEventListener the onDataChange will be triggered every time database is changed. 通过使用addValueEventListener ,每次更改数据库时都会触发onDataChange

Inside onClick please change from onClick请从

query.addValueEventListener to query.addListenerForSingleValueEvent query.addValueEventListenerquery.addListenerForSingleValueEvent

This should solve your problem 这应该可以解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM