无法使用未指定的索引从Firebase实时数据库中删除

时间:2017-01-03 18:06:52

标签: android firebase firebase-realtime-database

当我点击应用程序中的删除图标时,收到此消息。我不明白发生了什么,因为它有效。请帮帮我。

W/PersistentConnection: pc_0 - Using an unspecified index. Consider adding '".indexOn": "task"' at / to your security and Firebase Database rules for better performance
Application terminated.

这是我的活动:

package **********;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.SharedPreferences;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;



public class AimActivity extends AppCompatActivity implements
        GoogleApiClient.OnConnectionFailedListener{

    public static class AimViewHolder extends RecyclerView.ViewHolder{
        public TextView aimName;
        public TextView aimText;
        public TextView aimUrl;

        public AimViewHolder(View v){
            super(v);
            aimName = (TextView)itemView.findViewById(R.id.name);
            aimText = (TextView)itemView.findViewById(R.id.text);
            aimUrl = (TextView)itemView.findViewById(R.id.url);
        }
    }

    public static final String AIMS = "aims";
    private RecyclerView mAimRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;

    public static DatabaseReference mFirebaseDatabaseReference;
    private FirebaseRecyclerAdapter<AimToAim, AimViewHolder> mFirebaseAdapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aim);

        final Button bProf = (Button) findViewById(R.id.bProf);

        mAimRecyclerView = (RecyclerView)findViewById(R.id.aimRecyclerView);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mLinearLayoutManager.setStackFromEnd(true);
//        mAimRecyclerView.setLayoutManager(mLinearLayoutManager);

        //Database Initialization
        mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
        mFirebaseAdapter = new FirebaseRecyclerAdapter<AimToAim, AimViewHolder>(
                AimToAim.class,
                R.layout.activity_aim_to_aim,
                AimViewHolder.class,
                mFirebaseDatabaseReference.child(AIMS)) {
            @Override
            protected void populateViewHolder(AimViewHolder viewHolder, AimToAim model, int position) {
                viewHolder.aimName.setText(model.getName());
                viewHolder.aimText.setText(model.getText());
                viewHolder.aimUrl.setText(model.getUrl());
            }
        };



        mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver(){
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount){
                super.onItemRangeInserted(positionStart, itemCount);
                int aimCount = mFirebaseAdapter.getItemCount();
                int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
                if (lastVisiblePosition == -1 || (positionStart >= (aimCount -1) && lastVisiblePosition == (positionStart -1))){
                    mAimRecyclerView.scrollToPosition(positionStart);
                }
            }
        });
        mAimRecyclerView.setLayoutManager(mLinearLayoutManager);
        mAimRecyclerView.setAdapter(mFirebaseAdapter);

        bProf.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent registerIntent = new Intent(AimActivity.this, UserAreaActivity.class);
                AimActivity.this.startActivity(registerIntent);
            }
        });
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}

和任务类:

public class Task {
    private String task;
    public Task() {}
    public Task(String task) {
        this.task = task;
    }
    public String getTask() {
        return task;
    }
}

1 个答案:

答案 0 :(得分:1)

您需要将.index添加到控制台中的firebase规则中:

{
    "rules" : {
        ".read" : ...,
        ".write" : ...,
        ".indexOn": "task"
    }
}