Good day, Sorry for the rather long post. this is more like a design decision than anything else. I have an activity which contains 4 fragments. Now i switch through the fragments through a slideMenu.
Each Fragment makes a call to a webservices and gets a JSONObject result vis an AsyncTask. I am looking for a structure where i would have to cache each JSONObject result
for each fragment . when the user switches between the fragments, it checks if a certain amount of time has expired and acts accordingly
-if cached time has expired, reload the network request again.
-if not, use the cached JSONObject result and display to the user.
this means if i have an expiration time for like 6 hrs or rather the data from the backend updates every 6hrs, I should only request from the webservice via the asynctask
once every 6 hours and other times just used the cached values instead.
My current implementation
1. To have a set of global boolean variables via extending the Application class to load for each fragments the first time.
public class MyApp extends Application {
private boolean First_load_frag1 = true;
private boolean First_load_frag2= true;
public void setLoadFrag1(boolean value){
First_load_frag1 = value;
}
public boolean getLoadFrag1(){
return First_load_frag1;
}
}
in my fragments, i set them to true in onCreate(), use it to determine whether to request the first time and then set it to false so i don't have to anymore.
then in my fragments OnResume() i check to see if the cached_time has expired or not. i do something like this
#Override
public void onResume(){
super.onResume();
public final static long MINUTE_MILLIS = 60000;
Calendar cal = Calendar.getInstance();
int mins = (int) ((cal.getTimeInMillis()/MINUTE_MILLIS) - (cached_timer/MINUTE_MILLIS);
if(mins > 180){
Toast.makeText(getActivity(), "on resume refresh data", Toast.LENGTH_LONG).show();
requestService(); //this sends a request to the webservice
} else {
cached_result = manager.getCachedJSONResult(CACHED_RESULT );
if(cached_result != null){
Toast.makeText(getActivity(), "on resume cached data", Toast.LENGTH_LONG).show();
loadCachedResult(cached_result);
}
}
}
This feels like a messy way of handling this situations to me. Taken into consideration application crashes and Activity lifecycles, is this a good solution of doing something like this oris there a more elegant way i can achieve this? Every input will be much appreciated. Thanks
1.first you need to create data structure for holding data for fragment
check in data structure that data is available for selected fragement and it is time for refresh data
if yes then call load data(web service to load data) into fragment
else
load data from data structure and show in fragment.
Related
I used the lifecycle callback onCreate to fetch data like below
mWeOutViewModel.getPlaceListLiveData()
.observe(this, weOutItemViewModels -> {
AppLogger.i(getCustomTag() + "adding items " + weOutItemViewModels.size());
if (weOutItemViewModels != null && weOutItemViewModels.size() > 0)
mWeOutListAdapter.addToExisting(weOutItemViewModels);
});
As you can see the AppLogger output the initial size which is 0 when the fragment is displayed, then I fetch the data and call postValue (setValue crashes the app and it expected because I fetch data from the internet using a background thread). So I call post value like below :
private void updatePlaces(List<WeOutGroupedViewModels> weOutGroupedViewModels) {
List<WeOutGroupedViewModels> oldList = placeMutableLiveData.getValue();
oldList.addAll(weOutGroupedViewModels);
AppLogger.i(TAG +" updating places "+oldList.size());
placeMutableLiveData.postValue(oldList);
}
As you can see the other AppLogger before postValue, the size of the list is displayed(not empty), but nothing happens until the app crashes and nothing is shown in the logs. I have no ways of debugging since even on debug mode nothing happens. The post value doesn't trigger the observer.
I initialize the mutableLivedata like this :
private final MutableLiveData<List<WeOutGroupedViewModels>> placeMutableLiveData = new MutableLiveData<>();
and access like this :
public LiveData<List<WeOutGroupedViewModels>> getPlaceListLiveData() {
return placeMutableLiveData;
}
Event when I make the livedata public to access directly the livedata, there is no change (just in case someone thinks that's is where the issue comes from)
Instead of placeMutableLiveData.postValue(oldList);
I recommend using
placeMutableLiveData.postValue(Collections.unmodifiableList(new ArrayList<>(newList));
That way, the next time you access this list, you won't be able to mutate it in place, which is a good thing. You're not supposed to mutate the list inside a reactive state holder (MutableLiveData).
So theoretically it should look like this:
private void updatePlaces(List<WeOutGroupedViewModels> weOutGroupedViewModels) {
List<WeOutGroupedViewModels> newList = new ArrayList<>(placeMutableLiveData.getValue());
newList.addAll(weOutGroupedViewModels);
AppLogger.i(TAG +" updating places "+newList.size());
placeMutableLiveData.postValue(Collections.unmodifiableList(newList));
}
I'm implementing a chat client.
Everytime user clicks Send message button - I perform a Realm insert of this message.
I have a service 'waiting' for this change to send this message via socket.
Like this:
Observable<RealmResults<RealmMessage>> observable = getUnsentMessages();
subscribeUnsendMessages = observable
...
.subscribe(message -> {
launchMessageSending(message);
});
and method for getting this observable looks like this:
public Observable<RealmResults<RealmMessage>> getUnsentMessages() {
final Realm instance = getRealmInstance();
return instance.where(RealmMessage.class)
...
.findAllAsync()
.asObservable()
.filter(o -> ... )
.doOnUnsubscribe(instance::close);
}
And here is the problem - there is a corner case, when I perform two operations roughly at the same time.
The second one looks like this:
public boolean shouldTrack(#NonNull RealmChat chat) {
getRealmInstance().executeTransactionAsync(realm ->{
RealmTrackedState trackedStates = realm.where(RealmTrackedState.class).findFirst();
trackedStates.getRealmChats().add(chat);
});
return true;
}
The above method is a tracking cache for remembering if I tracked given chat or not.
(This is a bit more complicated than this, bear with me).
This PROBABLY causes the observer to react on the second database change.
Because these two operations happen almost at the same time - the second notification is still "valid" because there is an unsent message.
So, questions:
Am I right?
If so - how should I handle this?
I don't really want to 'delay' the shouldTrack(); method because I'm afraid that the same problem will occur in a different way.
Maybe create a flag for this message being 'handled', so the second will be ignored - but this is a bit nasty I guess. The second 'send' shouldn't happen.
EDIT:
Here is my TrackedState object
public class RealmATrackedState extends RealmObject {
#PrimaryKey
private int id = 1;
private RealmList<RealmChat> realmChats;
private boolean isSomething;
}
Is updating such object (as posted above) causing RealmMessage table be notified?
I'm looking for the best implementation pattern in Android to update a list when one of the elements change in a different activity.
Imagine this user journey:
An async process fetches ten (10) contact profiles from a web server. These are placed in an array and an adapter is notified. The ten (10) contact profiles are now displayed in a list.
The user clicks on contact profile five (5). It opens up an activity with details of this contact profile. The user decides they like it and clicks 'add to favourite'. This triggers an async request to the web server that the user has favourited contact profile five (5).
The user clicks back. They are now presented again with the list. The problem is the list is outdated now and doesn't show that profile five (5) is favourited.
Do you:
Async call the web server for the updated data and notify the adapter to refresh the entire list. This seems inefficient as the call for the list can take a couple of seconds.
On favouriting the profile store the object somewhere (perhaps in a singleton service) marked for 'refresh'. OnResume in the List activity do you sniff the variable and update just that element in the list.
Ensure the list array is static available. Update the array from the detail activity. OnResume in the activity always notify the adapter for a refresh.
Ensure the list array and adapter is static available. Update the array and notify the adapter from the detail activity.
Any other options? What is the best design principle for this?
Async call the web server for the updated data and notify the adapter
to refresh the entire list. This seems inefficient as the call for the
list can take a couple of seconds.
As you say, it's very inefficient. Creating an Object is expensive in Android. Creating a List of many object is much more expensive.
On favouriting the profile store the object somewhere (perhaps in a
singleton service) marked for 'refresh'. OnResume in the List activity
do you sniff the variable and update just that element in the list.
This is not a good solution because there is a probability that the app crashes before we refresh the object or the app get killed by the device.
Ensure the list array is static available. Update the array from the
detail activity. OnResume in the activity always notify the adapter
for a refresh.
Updating the array via a static method or variable is not a good solution because it makes your detail Activity get coupled with the list. Also, you can't make sure that only the detail activity that change the list if your project get bigger.
Ensure the list array and adapter is static available. Update the
array and notify the adapter from the detail activity.
Same as the above, static variable or object is a no go.
You better use an Event Bus system like EventBus.
Whenever you clicks 'add to favourite' in detail activity, send the async request to update favourite to the web server and also send Event to the list activity to update the specific profile object. For example, if your profile has id "777" and the profile is favourited in detail activity then you need to send the Event something like this in your :
btnFavourite.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Send event when click favourite.
EventBus.getDefault.post(new RefreshProfileEvent(id, true);
}
});
RefreshProfileEvent is a simple pojo:
public class RefreshProfileEvent {
private String id;
private boolean isFavourited;
public RefreshProfileEvent(String id, boolean isFavourited) {
this.id = id;
this.isFavourited = isFavourited;
}
//getter and setter
}
Then you can receive the Event in your list activity to update the selected profile:
public class YourListActivity {
...
#Override
protected onCreate() {
...
EventBus.getDefault().register(this);
}
#Override
protected onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(RefreshProfileEvent event) {
// Refresh specific profile
// For example, your profile is saved in List<Profile> mProfiles
// Search for profile by its id.
for(int i = 0; i < mProfiles.size(); i++) {
if(mProfiles.getId().equals(event.getId()) {
// Refresh the profile in the adapter.
// I assume the adapter is RecyclerView adapter named mAdapter
mProfiles.get(i).isFavourited(true);
mAdapter.notifyItemChanged(i);
// Stop searching.
break;
}
}
}
You don't need to wait for AsyncTask request result returned by the server. Just make the profile favourited first and silently waiting for the result. If your request success, don't do anything. But if the request error, make the profile unfavourited and send unobstructive message like SnackBar to inform the user.
Third option is the best when a user changes the data in detail activity the array should be changed and then when the use returns to main activity call Adapter.notifyDataSetChanged(); will do the trick
For an ArrayAdapter , notifyDataSetChanged only works if you use the add() , insert() , remove() , and clear() on the Adapter.
You can do something like this:
#Override
protected void onResume() {
super.onResume();
Refresh();
}
public void Refresh(){
items = //response....
CustomAdapter adapter = new CustomAdapter(MainActivity.this,items);
list.setAdapter(adapter);
}
On every onResume activity it will refresh the list. Hope it helps you.
I create an app that like dictionary app. When the user types in an Edittext, I call an AsyncTask to compute and update the result to the screen (I put the UI update code in onPostExecute() method ). However, when you type little fast, the eddittext become not responesive (a little latency). I think this promblem occurs because many AsyncTasks are running (each AsynTask for an input letter). So, I think I need to stop the first task before calling new task. Am I right? What should I do in this situation?
You don't need to implement the filter method in an async task. I call filter method on data when first letter has been written in editbox and save the result in an temporary array, then when another letter has been written, I call filter method on the temporary data which technically has less information than the original data. By doing this, the dimmension of data set decreases as you type in editbox. Also, you can use this method to store previous data set so when you press backspace, you don't have to call filter method again, you just go to previous saved temporary data set. For me, it works fine and I don't have to use async task because it is efficient
I suggest you another approach: use only one thread. The searching thread should wait for searching data > do search > and sleep until new data. E.g.:
private static class SearchThread extends Thread{
private Object monitor = new Object();
private String value;
public void search(String value){
this.value = value;
synchronized (monitor){monitor.notify();}
}
#Override
public void run() {
while(true){
try {System.out.println("Wait for search data."); synchronized (monitor){monitor.wait(); }
} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("Searching for " + value);
}
}
}
My application works this way:
ListView---->onListItemClick---->detailspage---->backpressed---->goes
back to the list---->click the same item again---->same detailspage
loads again.
The details page gets a lot of data from a server and populate its views.
So, it takes 2-4 secs every time an item is clicked in the listview. I have seen apps where they wont load any data if the same page is called 2nd time.
How can I do that?
Currently in my app its like this:
onCreate call AsyncTask to get data and populate the view
nothing in onResume, onPause, onStart, onStop, onDestroyed
You can keep the data for that domain object in a singleton and then when you enter your details page there are two ways to go.
If for instance you had a list of Person class.
public class Person {
private String name;
private Image img;
...
}
Then you could have a PersonCache that was a singleton caching the data for the last Person selected in your list:
public class PersonCache {
private Person cachedPerson;
private static PersonCache instance;
private PersonCache(){
...
}
public PersonCache getInstance(){
if(instance == null){
instance = new PersonCache();
}
return instance;
}
public Person getCachedPerson(){
return cachedPerson;
}
public void setCachedPerson(Person p){
cachedPerson = p;
}
}
So in onCreate when you finish fetching your JSON data you create a Person object and call setCachedPerson.
If you know that the data in the details page won't have been updated:
In onCreate in details page you check if the object that has been selected is the same as the one cached in your singleton (if the objects have unique ids in your database you can look at those to check if it's the same).
If you don't know whether there's new data:
You can use the If-Modified-Since technique when making your GET request in your AsyncTask.
Basically what you do is add a header parameter
key: If-Modified-Since
value: Sat, 29 Oct 1994 19:43:31 GMT
If the server has no new data it can respond with 304 and send no response body but if it has new data it will respond with 200 and send the data just like normal.
Implementing this would require some implementation on the server side as well.
Here's some more info on the technique:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html (section 14.25)