简体   繁体   English

如何使 spring controller json 响应等到 firebase 查询结束?

[英]How to make spring controller json response wait until firebase query ends?

I'm using an ajax call to spring controller to start a firebase query, save the result to local database and return items saved in the local database (including the results saved from the current and previous firebase queries too).我正在使用 ajax 调用 spring controller 来启动 firebase 查询,将结果保存到本地数据库并返回保存在本地数据库中的项目(包括从当前和以前的 firebase 查询保存的结果)。 The problem is that since the firebase query runs async, the results from the local database query are returned before the firebase query finishes.问题在于,由于 firebase 查询运行异步,本地数据库查询的结果在 firebase 查询完成之前返回。 How can I make the returned local database query wait until the firebase query finished?如何让返回的本地数据库查询等到 firebase 查询完成?

    @PostMapping("/firebase-download")
    @ResponseBody
    public List<FirebaseTransactionRecord> firebaseDownload() {
        
        firebaseService.downloadAndSaveFirebaseTransactions();
        // wait for the query to end
        return firebaseTransactionRecordRepository.findBySentFalse();
    }   
    @Override
    @Transactional
    public void downloadAndSaveFirebaseTransactions() {

            final List<FirebaseTransactionRecord> firebaseTransactions = new ArrayList<>();

            firebaseDb.child(StringValueConstant.DB_CHILD_TEMPORARY_TRANSACTIONS)
                    .addValueEventListener(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot snapshot) {

                            for (DataSnapshot userSnapshot : snapshot.getChildren()) {
                                userSnapshot.getChildren().forEach(transactionSnapshot -> {
                                    FirebaseTransactionRecord record = firebaseTransactionMapper
                                            .toFirebaseTransactionRecord(transactionSnapshot);
                                    firebaseTransactions.add(record);
                                });
                            }

                            saveFirebaseTransactionRecords(firebaseTransactions);
                        }

                        @Override
                        public void onCancelled(DatabaseError error) {
                            log.error("Error reading Firebase data:", error);
                        }
                    });
    }
    @Override
    @Transactional
    public void saveFirebaseTransactionRecords(List<FirebaseTransactionRecord> firebaseTransactions) {
        firebaseTransactions.forEach(firebaseTransaction -> {
            if (!firebaseTransactionRecordRepository.existsByReferralNumber(firebaseTransaction.getReferralNumber())) {
                firebaseTransactionRecordRepository.save(firebaseTransaction);
            }
        });     
    }   

Something with a CountdownLatch should do the trick.带有CountdownLatch的东西应该可以解决问题。

Set the initial latch counter to 1 (for the main listener), then set it to the number of child nodes in onDataChange .将初始闩锁计数器设置为 1(对于主侦听器),然后将其设置为onDataChange中的子节点数。 Next pass the latch to saveFirebaseTransactionRecords and decrease it when each transaction completes.接下来将闩锁传递给saveFirebaseTransactionRecords并在每个事务完成时减少它。 Once the latch reaches 0, you can exit out of downloadAndSaveFirebaseTransactions一旦闩锁达到 0,您就可以退出downloadAndSaveFirebaseTransactions

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

相关问题 我如何等待 FireBase Kotlin Android Studio 的回复 - how do I wait for a response from FireBase Kotlin Android Studio 如何使依赖于先前功能数据的 function 等到设置该数据 - How to make a function which relies on a previous functions data wait until that data is set GetX Controller - 等待 firebase RTDB stream 被填充 - GetX Controller - Wait for firebase RTDB stream to be populated Firebase 响应无效 JSON object - Firebase response is not valid JSON object 如何等到所有 Promise 都完成? - How to wait until all of Promise are fulfilled? Terraform 等到执行 user_data 然后制作图像以自动缩放? - Terraform wait until execution of user_data then make an image to autoscale? 如何让我的程序在执行 function 之前等待 Firebase 的数据? - How can make my Program wait for Firebase's data before executing a function? 在发送响应之前等待 firebase email 成功/失败 - Wait for firebase email success/failure before sending response 如何在 iOS 应用程序上使 Firebase 数据库查询更快? - How to make Firebase Database query faster on iOS Application? 如何等待firebase存储上传图片后再使用? - How to wait for firebase storage to upload image before using it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM