Reputation: 1
I have problem with WorkManager from Android Architecture Components.
version of workmanager is alpha-06
Maybe I didn't understand this process, but I expect that OneTimeWorkRequest will work only once, and it work correctly if time for this work not very long (not more than 7+- minutes), but if more, workmanager start the same work again (with same UUID) and not stop first work, and workmanager execute two works in parallel
Here I start work
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();
}
It is my work
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;
}
}
}
but I need only one work for download these files and save it to database
Upvotes: 0
Views: 964
Reputation: 21104
I think you are mixing a few concepts. OneTimeWorkRequest
's are not unique by themselves. If you only want one instance of the continuation run, then you should be using unique work. Look athe documentation for beginUniqueWork
.
Upvotes: 1