为什么WorkManager无法与OneTimeWorkRequest一起正常使用?

时间:2018-08-07 08:49:38

标签: android android-architecture-components android-workmanager

我在Android Architecture Components中使用WorkManager遇到问题。

workmanager的版本为alpha-06

也许我听不懂这个过程,但是我希望OneTimeWorkRequest只能工作一次,并且如果工作时间不是很长(不超过7分钟),它将正常工作,但是如果更多,工作经理将开始工作再次执行相同的工作(具有相同的UUID),并且不停止第一个工作,而workmanager并行执行两个工作

我在这里开始工作

Data workDownloadBookData = new Data.Builder().putInt(BookDownloadWork.BOOK_ID_KEY, id).putString(BookDownloadWork.COUNTRY_CODE_KEY, countryCode).build();

    OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(BookDownloadWork.class)
                    .setInputData(workDownloadBookData)
                    .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 2L, TimeUnit.SECONDS)
                    .build();
WorkManager workManager = WorkManager.getInstance();
if (workManager != null) {
                workManager.beginUniqueWork(countryCode + id,ExistingWorkPolicy.KEEP, request).enqueue();
}

这是我的工作

public class BookDownloadWork extends Worker {

    private static final String TAG = BookDownloadWork.class.getSimpleName();

    public static final String COUNTRY_CODE_KEY = "COUNTRY_CODE_KEY";
    public static final String LAST_UPDATE_KEY = "LAST_UPDATE_KEY";
    public static final String BOOK_ID_KEY = "BOOK_ID_KEY";
    public static final String PHOTO_URI_KEY = "PHOTO_URI_KEY";

    private BookRepository bookRepository;
    private BookLoadProgressDao bookLoadProgressDao;
    private BookLoadWorkerDao bookLoadWorkerDao;
    private NotificationController notificationController;

    @NonNull
    @Override
    public Result doWork() {
        bookRepository = App.appComponent.getBookRepository();
        bookLoadProgressDao = App.appComponent.getBookLoadProgressDao();
        bookLoadWorkerDao = App.appComponent.getBookLoadWorkerDao();
        notificationController = App.appComponent.getNotificationController();

        String countryCode = getInputData().getString(COUNTRY_CODE_KEY);
//        String countryCode = getInputData().getString(COUNTRY_CODE_KEY, "");
        int serverBookId = getInputData().getInt(BOOK_ID_KEY, 0);
        if (!TextUtils.isEmpty(countryCode) && serverBookId != 0) {
            String localBookId = serverBookId + countryCode;
            BookLoadProgress bookLoadProgress = new BookLoadProgress();
            bookLoadProgress.setId(localBookId);
            try {
                LocalBookDetail localBookDetail = bookRepository.selectLocalBookDetailSynch(serverBookId, countryCode);
                bookRepository.saveBookToLocalStorageSynch(serverBookId, localBookDetail.getLastUpdate(), countryCode, null);
                BookLoadWorker bookLoadWorker = new BookLoadWorker();
                bookLoadWorker.setBookId(localBookId);
                bookLoadWorker.setWorkId(getId());
                bookLoadWorkerDao.insertBookLoadWorker(bookLoadWorker);
                RemoteBookChapter[] remoteBookChapters = bookRepository.loadBookFromServerSynch(countryCode, serverBookId);
                if (remoteBookChapters == null) return Result.FAILURE;
                //count max progress
                for (int i = 0; i < remoteBookChapters.length; i++) {
                    RemoteBookChapter remoteBookChapter = remoteBookChapters[i];
                    if (remoteBookChapter.getType().equals("image")) {                     bookLoadProgress.setMaxProgress(bookLoadProgress.getMaxProgress() + 1);
                        for (int j = 0; j < remoteBookChapter.getContent().length; j++) {
                            RemoteBookContent remoteBookContent = remoteBookChapter.getContent()[j];
                            if (remoteBookContent.getType().equals("image")) {
                                bookLoadProgress.setMaxProgress(bookLoadProgress.getMaxProgress() + 1);
                            }

                        }

                    }

                }

                bookLoadProgressDao.insertBookLoadProgress(bookLoadProgress);
                for (int i = 0; i < remoteBookChapters.length; i++) {
                    RemoteBookChapter remoteBookChapter = remoteBookChapters[i];
                    if (remoteBookChapter.getType().equals("image")) {         remoteBookChapter.setUrl(bookRepository.loadAndSaveImageSynch(remoteBookChapter.getUrl()));                   bookLoadProgress.setCurrentProgress(bookLoadProgress.getCurrentProgress() + 1);
         bookLoadProgressDao.insertBookLoadProgress(bookLoadProgress);

                        for (int j = 0; j < remoteBookChapter.getContent().length; j++) {

                            RemoteBookContent remoteBookContent = remoteBookChapter.getContent()[j];

                            if (remoteBookContent.getType().equals("image")) {

                                remoteBookContent.setUrl(bookRepository.loadAndSaveImageSynch(remoteBookContent.getUrl()));                              bookLoadProgress.setCurrentProgress(bookLoadProgress.getCurrentProgress() + 1);                                bookLoadProgressDao.insertBookLoadProgress(bookLoadProgress);

                            }

                        }

                    }

                }
                bookRepository.saveBookToLocalStorageSynch(serverBookId, localBookDetail.getLastUpdate(), countryCode, remoteBookChapters);
                bookLoadProgressDao.deleteBookLoadProgress(bookLoadProgress.getId());

                notificationController.sendNotificationAboutBookDownloadFinished(serverBookId, countryCode);
                return Result.SUCCESS;

            } catch (Exception e) {
                Log.e(TAG, "doWork: ",e );
//                bookLoadProgressDao.deleteBookLoadProgress(bookLoadProgress.getId());
                bookRepository.deleteBookSycnh(localBookId);
                return Result.FAILURE;
            }
        } else {
            return Result.FAILURE;
        }
    }

}

但是我只需要一项工作就可以下载这些文件并将其保存到数据库中

1 个答案:

答案 0 :(得分:2)

我认为您正在混合一些概念。 OneTimeWorkRequest本身并不是唯一的。如果只想运行一个延续实例,那么您应该使用唯一的作品。查看beginUniqueWork的文档。