0

我是新来的,所以我不确定我这样做是否正确。

我正在创建一个应用程序,我可以在其中将主题(基本上是定时事件)上传到 firestore 数据库。我遇到的问题是如何正确管理这些事件。目前我正在使用WorkManager来安排每小时从数据库读取一次以检查是否添加了任何新主题,但是问题在于该服务可能尚未启动,因此当打开应用程序时,我创建了空状态显示,我无法想出通过滑动刷新或类似的方式手动启动服务的方法。

它目前还设置为将我的离线房间数据库与新主题同步,一旦它实际从 Firestore 中获取它们,效果很好。但是我想让它更流畅、更自动化,这就是为什么我选择使用WorkManager

如果值得注意,我尝试使用SharedPreferences来跟踪主题的状态并根据该状态发送通知(无论是开始、已开始还是结束)但问题是我每次都必须清除 App Data我安装了该应用程序以使其工作。

你会怎么做呢?

最好朝正确的方向轻推,但也欢迎提供代码示例(可能是伪代码)。

工人阶级:

@Override
@NonNull
public Result doWork() {
        try{

            Query query = firestore.collection("themes").orderBy("start_time");

            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful()){
                        for(DocumentSnapshot snapshot : task.getResult()){
                            if(snapshot.exists()){
                                Themes themes = snapshot.toObject(Themes.class);

                                themeKey = snapshot.getId();

                                DateTime today = new DateTime();
                                DateTime start_time = new DateTime(themes.start_time);
                                DateTime end_time = start_time.plus(INTERVAL);

                                if (today.isBefore(start_time)) {
                                    CurrentTheme currentTheme = new CurrentTheme(themes.theme_name, themes.theme_description,
                                            String.valueOf(start_time.getMillis()), "Starts in: ", themeKey, themes.sponsor_url, themes.theme_img);

                                    mFirebaseMethods.insert(currentTheme);

                                    if(!FireApp.getPreferences().getBoolean("starting", false)){
                                        notifyThemeStatus(currentTheme.getTheme_name() + " starting at " +
                                                start_time.toString(), FireApp.getApp()
                                                .getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0).getString("regId", null));

                                        editor = FireApp.getPreferences().edit();
                                        editor.putBoolean("starting", true);
                                        editor.putBoolean("started", false);
                                        editor.putBoolean("ended", false);
                                        editor.apply();
                                    }

                                } else if(today.isAfter(start_time) && today.isBefore(end_time)){
                                    mFirebaseMethods.deleteByTime(String.valueOf(start_time.getMillis())); 

                                    CurrentTheme currentTheme = new CurrentTheme(themes.theme_name, themes.theme_description,
                                            String.valueOf(end_time.getMillis()), "Ends in: ", themeKey, themes.sponsor_url, themes.theme_img);

                                    mFirebaseMethods.update(currentTheme);

                                    if(!FireApp.getPreferences().getBoolean("started", false)) {
                                        notifyThemeStatus(themes.theme_name + " ends " +
                                                getRelativeTimeSpanString(themes.start_time + INTERVAL).toString(), FireApp.getApp()
                                                .getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0).getString("regId", null));

                                        editor = FireApp.getPreferences().edit();
                                        editor.putBoolean("starting", false);
                                        editor.putBoolean("started", true);
                                        editor.apply();
                                    }
                                } else if (today.isAfter(end_time)){

                                    CurrentTheme currentTheme = new CurrentTheme(themes.theme_name, themes.theme_description,
                                            String.valueOf(0), "Ended", themeKey, themes.sponsor_url, themes.theme_img);

                                    mFirebaseMethods.update(currentTheme);

                                    //checkWinner(themeKey, themes.start_time, themes.theme_name);

                                    if(!FireApp.getPreferences().getBoolean("ended", false)) {

                                        notifyThemeStatus(themes.theme_name + " has ended.", FireApp.getApp()
                                                .getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0).getString("regId", null));

                                        editor = FireApp.getPreferences().edit();
                                        editor.putBoolean("ended", true);
                                        editor.putBoolean("started", false);
                                        editor.apply();
                                    }

                                }
                            }
                        }
                    }
                }
            });
            return Result.SUCCESS;
        }catch (Throwable throwable){
            Log.e("PopulateThemeCache", "Error caching", throwable);
            return Result.FAILURE;
        }
    }

我在我的应用程序类中启动工作人员的位置:

Constraints constraints = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build();

PeriodicWorkRequest populateCache = new PeriodicWorkRequest.Builder(PopulateThemeCache.class, 1, TimeUnit.HOURS)
            .setConstraints(constraints)
            .addTag("Theme_Update")
            .build();

WorkManager.getInstance().enqueue(populateCache);

mFirebaseMethods是一个存储库类,它具有执行房间数据库操作的所有 asynctask 代码

4

0 回答 0