首页 > 解决方案 > 在 Cloud Firestore 中设置安全规则

问题描述

底线是我正在编写一个应用程序,以便当您单击应用程序中的按钮时,有人从数据库中的数组接收促销代码并删除此接收到的行,但是出现了问题,同时(或在 1-2 秒内)在不同的设备上收到一个和一个值,因为它根本没有时间从基础中删除,所以在安全规则中是否可以要求用户一个一个地访问数据库或以某种方式解决这个问题?先感谢您

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    Button mButtonGive;
    Button mButtonGiveIvi;
    TextView mTextPromo;
    TextView mTextPromoIvi;

    FirebaseFirestore mRef = FirebaseFirestore.getInstance();
    DocumentReference mDelRef = mRef.collection("Promocode").document("Delivery");
    DocumentReference mIviRef = mRef.collection("Promocode").document("Ivi");


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

        mButtonGive = findViewById(R.id.mButtonGive);
        mButtonGiveIvi = findViewById(R.id.mButtonGiveIvi);
        mTextPromo = findViewById(R.id.mTextPromo);
        mTextPromoIvi = findViewById(R.id.mTextPromoIvi);

        mButtonGive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDelRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                            if (task.isSuccessful()) {
                                DocumentSnapshot document = task.getResult();
                                assert document != null;
                                if (document.exists()) {
                                    Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                                    List<String> promocodes;
                                    promocodes = (List<String>) document.get("Promocode");
                                    Log.d(TAG, "Promocodes: " + promocodes);
                                    if (promocodes.size() > 0) {
                                        mTextPromo.setText(promocodes.get(0));
                                        mDelRef.update("Promocode", FieldValue.arrayRemove(promocodes.get(0)));
                                    } else {
                                        mTextPromo.setText("Промокоды кончились");
                                        Log.d(TAG, "No Promo");
                                    }
                                } else {
                                    Log.d(TAG, "No such document");
                                }

                            }
                            else {
                                Log.d(TAG, "get failed with ", task.getException());
                            }
                    }

                });
            }
        });

标签: androiddatabase

解决方案


由于您没有发布任何代码或程序的任何片段:

我猜你正在向用户发送代码,他必须在应用程序中介绍它。创建另一个数组,用于跟踪尚未在应用程序中使用的已发送代码:例如,promotionCodes[ ] 和 sentCodes[ ]。一旦你想发送另一个代码,你应该检查代码是否在 sentCodes[] 和promotionCodes[] 中,这样你就不会发送重复。

(如果你附上一些代码或更好地表达你的问题会更容易)


推荐阅读