【问题标题】:How to update firebase realtime database periodically?如何定期更新firebase实时数据库?
【发布时间】:2019-03-14 09:11:36
【问题描述】:

我有一个解析 html 的异步任务,从其中获取一些内容,然后将它们放入 firebase 实时数据库(作为 json)如果您自己运行代码,我的代码正在工作,但由于数据正在更改,我想从服务器定期运行代码,假设每 5 分钟更新一次我的数据库。这在firebase中可能吗?如果不是,您能否解释一下其他方法(服务器定期自行执行您的代码)?

static class fetchdata extends AsyncTask<String, Void, DatabaseReference>{

    private DatabaseReference hurriyetDatabase = FirebaseDatabase.getInstance().getReference("HurriyetNews");

    @Override
    protected DatabaseReference doInBackground(String... urls) {

        String url = urls[0];

        hurriyetDatabase.removeValue();

        try {
            Document wholePage = Jsoup.connect(url).get();
            Elements links = wholePage.select(
                    "div.swiper-slide a[href][href*=/gundem/], " +
                            "div.swiper-slide a[href][href*=/dunya/], " +
                            "div.swiper-slide a[href][href*=/ekonomi/], " +
                            "div.swiper-slide a[href][href*=/teknoloji/]");

            for (int i=0; i< links.size(); i++){
                Element link = links.get(i);
                String newsUrl = link.attr("abs:href");
                Document currentnews = Jsoup.connect(newsUrl).get();

                String category = currentnews
                        .select("meta[property=article:section]")
                        .first()
                        .attr("content");

                String title = currentnews
                        .select("meta[property=og:title]")
                        .first()
                        .attr("content");

                String description = currentnews
                        .select("meta[property=og:description]")
                        .first()
                        .attr("content");

                String imageUrl = currentnews
                        .select("meta[property=og:image]")
                        .first()
                        .attr("content");

                String lastUpdateTime = currentnews
                        .select("time")
                        .first()
                        .attr("datetime");

                NewsEntry currentNews = new NewsEntry(
                        "Hürriyet", category, title, newsUrl, imageUrl, lastUpdateTime, description);


                String id = hurriyetDatabase.push().getKey();
                hurriyetDatabase.child(id).setValue(currentNews);

            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return hurriyetDatabase;
    }
}

我想使用这个 json 作为一个数据库,移动应用程序的客户端将从中查询他们的需求。

【问题讨论】:

  • 您可以考虑使用 Java 计时器系统来排队延迟的数据库异步刷新。

标签: android json firebase server


【解决方案1】:

在 Firebase 上,您可以使用 Cloud Functions 来完成此操作,它会响应 Firebase 内部或外部发生的事件而运行。

对于这种特定情况,您希望按计划运行代码,请查看Cloud Functions for Firebase trigger on time?

【讨论】:

    【解决方案2】:

    这在 Firebase 中可行吗?

    是的,使用Cloud Functions for Firebase 可以让您自动运行后端代码。这也可以响应由 Firebase 功能和 HTTPS 请求触发的事件。但请注意,单个云功能一次只能响应在一个位置发生的变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2021-10-19
      相关资源
      最近更新 更多