如何在循环中从Firestore查询中获取单个文档

时间:2020-02-13 12:26:48

标签: kotlin google-cloud-firestore android-notifications

在我的应用中,我正在Firestore查询中运行for循环。此查询本身仅用于返回满足品牌和位置条件(字符串值)并且文档中位于计数器("deal_number")比用户集合中的比较计数器({{1 }}。

因此,从本质上讲,我在用户集合中搜索最后一个计数器编号,并将其用作对交易ID中计数器的逻辑检查

Login.deal_number

对于上述情况,为什么Firestore在仅应有一个事件的情况下会创建多个通知,这是因为过滤器似乎无法正确应用的方式。

这似乎是基于Firestore查询运行通知的常见趋势。这有可能吗?我已经尝试过for循环中的各种中断,但是在同一文档上仍然会遇到多次点击。我尝试使用 menuref = FirebaseFirestore.getInstance() menuref.collection("Users").whereEqualTo("uid", userid) .addSnapshotListener { value, task -> if (task != null) { return@addSnapshotListener } for (document in value!!) { val seller_brand = document.getString("brand")!! val seller_location = document.getString("location")!! val deal_num = document.getLong("last_deal_num")!! //Login.deal_number is a companion object Login.deal_number = deal_num Log.d("Firestore_brand", seller_brand) Log.d("Firestore_location", seller_location) Log.d("lastdealnum", "${Login.deal_number}") menuref.collection("Car_Deals").whereEqualTo("brand", seller_brand).whereEqualTo(seller_location, "True").whereGreaterThan("deal_number",Login.deal_number) .addSnapshotListener { value, task -> if (task != null) { return@addSnapshotListener } counter_deal = 0 for (document in value!!) { val new_deal_num = document.getLong("deal_number")!! Log.d("dealnumnew", "$new_deal_num") if (new_deal_num == Login.deal_number) { counter_deal = counter_deal + 1 break } else if (new_deal_num < Login.deal_number) { counter_deal = counter_deal + 1 break } else if (new_deal_num > Login.deal_number && counter_deal < 1) { Log.d("Tag_counter_deal","${counter_deal}") Log.d("Tag_newdeal_num","${new_deal_num}") Log.d("Tag_userdeal_num","${Login.deal_number}") counter_deal = counter_deal + 1 newdealnumref = FirebaseFirestore.getInstance().collection("Users") newdealnumref.document(userid) .update("last_deal_num", new_deal_num) .addOnSuccessListener { }.addOnFailureListener { e -> Log.w( "firestore_create_error", "Error writing to document", e ) } Log.d("newdealbrand", "$seller_brand $seller_location") Log.d("newdeal", "New deal found") dealCreatedNotificationChannel() // this is the android O channel creation CodetoRunforNotification() // this is the code to run for the notification. generic, havent changed anything according to normal notification creation with(NotificationManagerCompat.from(this)) { notify(notify_counter, builder) notify_counter++ } counter_deal = 0 break } } } } } 来限制快照被触发而没有运气时发送的通知数量。

Database 1

Database 2

0 个答案:

没有答案