Refresh data when day changes in offline app
I have an offline app that stores data in RoomDatabase. New data is inserted only after user action (when button is clicked or spinner selection changes). Data is retrieved as LiveData.
In my main activity I am only showing data for current day. But my problem appears when day changes and app is still in the background, not killed by the system. In that situation, activity still shows data for previous day.
How I can implement refresh when current date is different from date showed in activity?
When app was killed, I have a method in onCreate that tries to insert new row (with onConflict Ignore), and that works. But if there is better way, I am open for suggestions.
Dao (I am getting only row with date that matches passed date)
@Query("SELECT * from coffee_productivity WHERE date LIKE :todayDate")
fun getTodayData(todayDate: String): LiveData<CoffeeProductivityData>
Repository
private var mTodayData: LiveData<CoffeeProductivityData> = mCoffeeProductivityDao.getTodayData(mUtilities.getTodayDate())
// Wrapper for getting current day data
fun getTodayData(): LiveData<CoffeeProductivityData> {
return mTodayData
}
ViewModel
private val mTodayData: LiveData<CoffeeProductivityData> by lazy {
mRepository.getTodayData()
}
// Get LiveData for today coffees and productivity
fun getTodayData(): LiveData<CoffeeProductivityData> {
return mTodayData
}

add a comment |
I have an offline app that stores data in RoomDatabase. New data is inserted only after user action (when button is clicked or spinner selection changes). Data is retrieved as LiveData.
In my main activity I am only showing data for current day. But my problem appears when day changes and app is still in the background, not killed by the system. In that situation, activity still shows data for previous day.
How I can implement refresh when current date is different from date showed in activity?
When app was killed, I have a method in onCreate that tries to insert new row (with onConflict Ignore), and that works. But if there is better way, I am open for suggestions.
Dao (I am getting only row with date that matches passed date)
@Query("SELECT * from coffee_productivity WHERE date LIKE :todayDate")
fun getTodayData(todayDate: String): LiveData<CoffeeProductivityData>
Repository
private var mTodayData: LiveData<CoffeeProductivityData> = mCoffeeProductivityDao.getTodayData(mUtilities.getTodayDate())
// Wrapper for getting current day data
fun getTodayData(): LiveData<CoffeeProductivityData> {
return mTodayData
}
ViewModel
private val mTodayData: LiveData<CoffeeProductivityData> by lazy {
mRepository.getTodayData()
}
// Get LiveData for today coffees and productivity
fun getTodayData(): LiveData<CoffeeProductivityData> {
return mTodayData
}

Could tell more information? What is date? It is like 'dd-mm-yyyy'. And why do you need to relate to date? AlsoSystem.currentTimeMillis()
return always current date.
– Dmitro Ivanov
Dec 29 '18 at 13:47
Date is stored in database as String (dd/mm/yyyy). I am getting current date using my utility method. I am getting it same way You showed. My main activity is showing data for current day, so I am retrieving a row, where date matches my current date. I think the problem is with the fact that my LiveData is observing same row when date changes but activity is not killed, so I need some way to refresh that observed row.
– OMIsie11
Dec 29 '18 at 14:10
add a comment |
I have an offline app that stores data in RoomDatabase. New data is inserted only after user action (when button is clicked or spinner selection changes). Data is retrieved as LiveData.
In my main activity I am only showing data for current day. But my problem appears when day changes and app is still in the background, not killed by the system. In that situation, activity still shows data for previous day.
How I can implement refresh when current date is different from date showed in activity?
When app was killed, I have a method in onCreate that tries to insert new row (with onConflict Ignore), and that works. But if there is better way, I am open for suggestions.
Dao (I am getting only row with date that matches passed date)
@Query("SELECT * from coffee_productivity WHERE date LIKE :todayDate")
fun getTodayData(todayDate: String): LiveData<CoffeeProductivityData>
Repository
private var mTodayData: LiveData<CoffeeProductivityData> = mCoffeeProductivityDao.getTodayData(mUtilities.getTodayDate())
// Wrapper for getting current day data
fun getTodayData(): LiveData<CoffeeProductivityData> {
return mTodayData
}
ViewModel
private val mTodayData: LiveData<CoffeeProductivityData> by lazy {
mRepository.getTodayData()
}
// Get LiveData for today coffees and productivity
fun getTodayData(): LiveData<CoffeeProductivityData> {
return mTodayData
}

I have an offline app that stores data in RoomDatabase. New data is inserted only after user action (when button is clicked or spinner selection changes). Data is retrieved as LiveData.
In my main activity I am only showing data for current day. But my problem appears when day changes and app is still in the background, not killed by the system. In that situation, activity still shows data for previous day.
How I can implement refresh when current date is different from date showed in activity?
When app was killed, I have a method in onCreate that tries to insert new row (with onConflict Ignore), and that works. But if there is better way, I am open for suggestions.
Dao (I am getting only row with date that matches passed date)
@Query("SELECT * from coffee_productivity WHERE date LIKE :todayDate")
fun getTodayData(todayDate: String): LiveData<CoffeeProductivityData>
Repository
private var mTodayData: LiveData<CoffeeProductivityData> = mCoffeeProductivityDao.getTodayData(mUtilities.getTodayDate())
// Wrapper for getting current day data
fun getTodayData(): LiveData<CoffeeProductivityData> {
return mTodayData
}
ViewModel
private val mTodayData: LiveData<CoffeeProductivityData> by lazy {
mRepository.getTodayData()
}
// Get LiveData for today coffees and productivity
fun getTodayData(): LiveData<CoffeeProductivityData> {
return mTodayData
}


asked Dec 29 '18 at 13:34
OMIsie11OMIsie11
227
227
Could tell more information? What is date? It is like 'dd-mm-yyyy'. And why do you need to relate to date? AlsoSystem.currentTimeMillis()
return always current date.
– Dmitro Ivanov
Dec 29 '18 at 13:47
Date is stored in database as String (dd/mm/yyyy). I am getting current date using my utility method. I am getting it same way You showed. My main activity is showing data for current day, so I am retrieving a row, where date matches my current date. I think the problem is with the fact that my LiveData is observing same row when date changes but activity is not killed, so I need some way to refresh that observed row.
– OMIsie11
Dec 29 '18 at 14:10
add a comment |
Could tell more information? What is date? It is like 'dd-mm-yyyy'. And why do you need to relate to date? AlsoSystem.currentTimeMillis()
return always current date.
– Dmitro Ivanov
Dec 29 '18 at 13:47
Date is stored in database as String (dd/mm/yyyy). I am getting current date using my utility method. I am getting it same way You showed. My main activity is showing data for current day, so I am retrieving a row, where date matches my current date. I think the problem is with the fact that my LiveData is observing same row when date changes but activity is not killed, so I need some way to refresh that observed row.
– OMIsie11
Dec 29 '18 at 14:10
Could tell more information? What is date? It is like 'dd-mm-yyyy'. And why do you need to relate to date? Also
System.currentTimeMillis()
return always current date.– Dmitro Ivanov
Dec 29 '18 at 13:47
Could tell more information? What is date? It is like 'dd-mm-yyyy'. And why do you need to relate to date? Also
System.currentTimeMillis()
return always current date.– Dmitro Ivanov
Dec 29 '18 at 13:47
Date is stored in database as String (dd/mm/yyyy). I am getting current date using my utility method. I am getting it same way You showed. My main activity is showing data for current day, so I am retrieving a row, where date matches my current date. I think the problem is with the fact that my LiveData is observing same row when date changes but activity is not killed, so I need some way to refresh that observed row.
– OMIsie11
Dec 29 '18 at 14:10
Date is stored in database as String (dd/mm/yyyy). I am getting current date using my utility method. I am getting it same way You showed. My main activity is showing data for current day, so I am retrieving a row, where date matches my current date. I think the problem is with the fact that my LiveData is observing same row when date changes but activity is not killed, so I need some way to refresh that observed row.
– OMIsie11
Dec 29 '18 at 14:10
add a comment |
1 Answer
1
active
oldest
votes
You can use AlarmManager
to wake the phone at midnight and than call your method The
AlarmManager API is another option that the framework provides for
scheduling tasks. This API is useful in cases in which an app needs to
post a notification or set off an alarm at a very specific time.
Source : developer
Ok, so what would be best way to implement that LiveData refreshing? Change to MutableLiveData and use setValue?
– OMIsie11
Dec 29 '18 at 16:02
as long as your method doesn't change anything in the app UI you can use whatever you want, coz if you update the app layout while the app is closed or in the background, it will crash, but if u want to change background data or even save them in sharedpreferences than you have nothing to worry about
– Hossam Hassan
Dec 29 '18 at 16:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53970043%2frefresh-data-when-day-changes-in-offline-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use AlarmManager
to wake the phone at midnight and than call your method The
AlarmManager API is another option that the framework provides for
scheduling tasks. This API is useful in cases in which an app needs to
post a notification or set off an alarm at a very specific time.
Source : developer
Ok, so what would be best way to implement that LiveData refreshing? Change to MutableLiveData and use setValue?
– OMIsie11
Dec 29 '18 at 16:02
as long as your method doesn't change anything in the app UI you can use whatever you want, coz if you update the app layout while the app is closed or in the background, it will crash, but if u want to change background data or even save them in sharedpreferences than you have nothing to worry about
– Hossam Hassan
Dec 29 '18 at 16:08
add a comment |
You can use AlarmManager
to wake the phone at midnight and than call your method The
AlarmManager API is another option that the framework provides for
scheduling tasks. This API is useful in cases in which an app needs to
post a notification or set off an alarm at a very specific time.
Source : developer
Ok, so what would be best way to implement that LiveData refreshing? Change to MutableLiveData and use setValue?
– OMIsie11
Dec 29 '18 at 16:02
as long as your method doesn't change anything in the app UI you can use whatever you want, coz if you update the app layout while the app is closed or in the background, it will crash, but if u want to change background data or even save them in sharedpreferences than you have nothing to worry about
– Hossam Hassan
Dec 29 '18 at 16:08
add a comment |
You can use AlarmManager
to wake the phone at midnight and than call your method The
AlarmManager API is another option that the framework provides for
scheduling tasks. This API is useful in cases in which an app needs to
post a notification or set off an alarm at a very specific time.
Source : developer
You can use AlarmManager
to wake the phone at midnight and than call your method The
AlarmManager API is another option that the framework provides for
scheduling tasks. This API is useful in cases in which an app needs to
post a notification or set off an alarm at a very specific time.
Source : developer
answered Dec 29 '18 at 15:04


Hossam HassanHossam Hassan
3031217
3031217
Ok, so what would be best way to implement that LiveData refreshing? Change to MutableLiveData and use setValue?
– OMIsie11
Dec 29 '18 at 16:02
as long as your method doesn't change anything in the app UI you can use whatever you want, coz if you update the app layout while the app is closed or in the background, it will crash, but if u want to change background data or even save them in sharedpreferences than you have nothing to worry about
– Hossam Hassan
Dec 29 '18 at 16:08
add a comment |
Ok, so what would be best way to implement that LiveData refreshing? Change to MutableLiveData and use setValue?
– OMIsie11
Dec 29 '18 at 16:02
as long as your method doesn't change anything in the app UI you can use whatever you want, coz if you update the app layout while the app is closed or in the background, it will crash, but if u want to change background data or even save them in sharedpreferences than you have nothing to worry about
– Hossam Hassan
Dec 29 '18 at 16:08
Ok, so what would be best way to implement that LiveData refreshing? Change to MutableLiveData and use setValue?
– OMIsie11
Dec 29 '18 at 16:02
Ok, so what would be best way to implement that LiveData refreshing? Change to MutableLiveData and use setValue?
– OMIsie11
Dec 29 '18 at 16:02
as long as your method doesn't change anything in the app UI you can use whatever you want, coz if you update the app layout while the app is closed or in the background, it will crash, but if u want to change background data or even save them in sharedpreferences than you have nothing to worry about
– Hossam Hassan
Dec 29 '18 at 16:08
as long as your method doesn't change anything in the app UI you can use whatever you want, coz if you update the app layout while the app is closed or in the background, it will crash, but if u want to change background data or even save them in sharedpreferences than you have nothing to worry about
– Hossam Hassan
Dec 29 '18 at 16:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53970043%2frefresh-data-when-day-changes-in-offline-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could tell more information? What is date? It is like 'dd-mm-yyyy'. And why do you need to relate to date? Also
System.currentTimeMillis()
return always current date.– Dmitro Ivanov
Dec 29 '18 at 13:47
Date is stored in database as String (dd/mm/yyyy). I am getting current date using my utility method. I am getting it same way You showed. My main activity is showing data for current day, so I am retrieving a row, where date matches my current date. I think the problem is with the fact that my LiveData is observing same row when date changes but activity is not killed, so I need some way to refresh that observed row.
– OMIsie11
Dec 29 '18 at 14:10