android - 在 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());
}
}
});
}
});
解决方案
由于您没有发布任何代码或程序的任何片段:
我猜你正在向用户发送代码,他必须在应用程序中介绍它。创建另一个数组,用于跟踪尚未在应用程序中使用的已发送代码:例如,promotionCodes[ ] 和 sentCodes[ ]。一旦你想发送另一个代码,你应该检查代码是否在 sentCodes[] 和promotionCodes[] 中,这样你就不会发送重复。
(如果你附上一些代码或更好地表达你的问题会更容易)
推荐阅读
- javascript - 从节点红色对象获取单个数据?
- wpf - 对于带有数据库的 WPF 客户端-服务器应用程序,我可以使用哪些工具?
- visual-studio - SSRS - 文本框属性 - 操作 - 转到报告 - 如何匹配表格特征
- php - 配置服务器以在 '/' Laravel 项目上打开 public/index.php
- javascript - mongodb 如果存在则增量,否则插入
- html - 删除图像周围的白边
- css - 边框和背景位置 CSS
- c# - 通用实体基类
- kotlin - kotlin 中数据类构造函数中的 if 条件
- sql-server - 对 OLAP 多维数据集中的所有度量使用范围,一个或多个选定的计算度量除外