One-time download ListView data - Service or Bundle? - android

I'm developing an app that downloads ParseObject's data from server and populate a ListView in Fragment with it.
I read about downloading data by Service and after it's done (some kind of listener?) it would update Fragment and be accessible until user leaves the app (which is fine by me).
On the other hand - I can just store it in Bundle and retrieve it every time I get back to that Fragment, but then I'd need implement Serializable which in this case can be cumbersome: like here
Fragments are held by DrawerLayout so it's really irritating now to see loading bar everytime You change to that Fragment and I'm looking for a solution to change that to improve UX.
What do You suggest? Which approach would be better in that situation? Maybe there are things that I should be aware of before attempting to use any of these?

I think it depends on how often the data is changed on the server.
If only daily/weekly the solution must differ compared to if the data is changed every mins/hours.
If the data is kind of static then you can download it only once, and save it to SharedPreferences, or to a seperate local file or DB.
If it changes kinda often, I'd suggest to use bundles or in memory objects, so when the user reenters the app the data should be downloaded again.

The solution i'd use would be to simply convert the entire list of data to JSON, and then save it in SharedPreferences. This allows for easy reuse of the data when the user goes back to that fragment.
As you aren't saving the data across app close/reopen, a local database is not needed.

Related

Android Concept: Does Datasource belong in Activity or Fragment

Question: Assuming I have a web service which returns a JSON which I deserialize into a (list of) POJO(s), should each Fragment get a copy of the datasource or should I keep the datasource only in the activity and let the fragment(s) get the data via a callback interface (which I set in onAttach())? Or is there even another, better way?
WHY
I work with Android for quiet a while now but I think I still have not fully understood some very basic principles and I hope you can help me clarify things. In one of my applications, I use Volley to call a web service which returns JSON formatted data.
I use GSON to deserialize the JSON into POJOs. I then use greenrobots EventBus to send the new data around so I can update the UI. Currently all my fragments are registered as subscribers and each fragment stores a reference to the data.
I think I am doing this wrong because what if a fragment is currently not being displayed (they unregister from EventBus in onStop()). They could miss an update of the model and when a Backstack is popped, they could display an outdated model, right?
So what would be the best way to store the model so that all my UI components and controllers always display the latest version of the model? I'm fearing that one or the other component (Fragment and/or Activity) might miss an update and then displays outdated data.
What is Androids way to store models retrieved from web services and make them accessible for activities and fragments?
I think my main problem is that I can refresh the data in multiple activities and fragments. For example I could reload the data after a pull-to-refresh of a list of entries but renaming an entry would also cause an update - but only of this particular entry.
Have you thought about using sqlite to store the data and then displaying it?
What I would do is as follows:
Fetch data from service as a json
Parse the json and store it in a sqlite table
Read from the sqlite table as and when required
The advantage in doing so is that you are able to store new data as well as update the old one and as you said you can use pull down to refresh functionality, this proves helpful to store the latest data as well as the old one and always display the latest data as you are fetching from a datasource that is being updated.

Temporary and static realm objects used together?

I am currently running into some issues regarding the realm database and I don't know what the best practices are to tackle this problem.
So I have setup an app which communicates to a server and gets Post Objects through Retrofit stored into my Realm. This post feed is a core part of my app and I want to keep things locally to stay attractive while being offline. The thing is that I cannot store the entire feed list locally as this would be a massive chunk of memory. But I want to make sure that the User gets this content while scrolling through a recycler-view.
The recycler-view currently only shows local Posts and updates them if refresh is forced. I want to implement a load on scroll mecanism that loads Post objects into it while scrolling but this scroll objects should not be stored locally when the app closes.
I thought of creating an additionally in-memory Realm but there is another problem: A Post Object contains a ForeignKey to a UserObject. When I download this dynamic PostObjects and a UserObject changed over this time on the server I want to be able to reflect this changes to every other User on the local persisten Realm too. (To avoid having 2 different UserObjects for the same User)
My best idea i came up so far is to have a
Boolean field on the PostObjects set to true or false to indicate temporary state or not. After the applications closes I would drop all the temporary entries. Is this a viable solution or do I miss anything? I hope you understand my problem and can help.
Edit: My Realm Database contains two relevant objects:
User
Has a name (primarykey), id (global from server), imageUrls etc..
Post
Has a User who created it, again an id and its data (text, date, images...)
Edit2 : What I really need is a type of in-memory Realm that allows me on start up to clone another realm that is used to have data in case of no connectivity. Before the in-memory realm gets deleted I would then override the offline realm with the 5-10 last post entries of the temporary realm. Is such a thing possible?

Saving and loading ArrayLists in Android

I am working on an android application that will randomly choose a activity for people to do from an array list. I want this list to be edited by the user of the application by adding or removing activities from the list. I would like the list to be loaded when the application is opened and saved when paused, closed, or destroyed.
I cannot figure out how to store the array list an load it. I have tried to understand posts in the past about this topic but cannot make sense of how to implement them.
I you can help, it would be greatly appreciated.
You have some alternatives:
1) Use a local database (I suggest greendao for the great performance). Here's the oficial page
That way like in a conventional database you store (in the OnPause method) the data and query it the next time the user opens the app.
2) Use serialization (its convenient for small amount of data), as the previous one in the onPause method you serialize the object and deserialize it when the users open the app. Here's a tutorial about it

Store data in Android using Volley

In application there is menu in menu user can choose one of them. All required data from server. Where should I make an request and store data?
So far I made request inside fragment, but I do not want to make request per activity/fragment.
Is the good idea to make request inside main activity (of course using other thread <>). And then keep data in static list in this activity? Or is there better way to store this data?
This is a very vague question, the answer can vary by a lot based on the precise requirements.
For example if its just a boolean returned by server, sure you can save as static boolean, if the list returned is small, sure save it as static, as long as you are comfortable with loosing the data when the app is killed.
If not you need to save the data somewhere, depending on the size, type there are multiple options you can use please read http://developer.android.com/training/basics/data-storage/index.html
If you ment using Volley as a Singleton class read more in the following link.

ArrayList or SQLite

In terms of storing data in Android, would it be more efficient to use a large ArrayList or setup an SQLite database? I have ~9000 bus stops and stop names (both in String) that I need to store and I was wondering which method would be the quickest or most efficient.
An ArrayList is probably a very bad idea. I assume you want the data to be persistent, so if your user kills your app your data will be lost if you use an ArrayList. A database is persistent(unless the user clears the cache of the app). So I would highly recommend using a database over an ArrayList.
If your data does not change then you could probably have it read on startup and kept in memory while the App runs. For example, having a .json file with all the stops, read it on startup, put the data in a HashMap or something that is easy to use since you will probably lookup stuff. It would probably work fine with ~9000 entries.
If you are going to add or update information about the stops during usage then the SQLite is the better choice though.
1.) Store and retrieve your data from a SQLite DB since you want persistance. And since you say you have 9k+ rows a simple select will give you everything at once and you can easily filter the data as well if you need to
2.) At startup, put all your data into efficient memory structures like HashMaps(or in your case arraylists) and reference them throughout the app. This way you'll only do one time access.
3.) When in doubt build a DB. No harm, more efficient and easier to handle than files

Categories

Resources