Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I'm making an ecommerce application on Android and, as it is my first serious project, I'm trying to figure out beforehand the best approach to do this.
The application talks to a web service (magento api, meaning soap or xml rpc unfortunately) and gets all the content on the phone (product categories, product details, user credentials etc.). I think it should do lazy loading or something like that.
So, I was thinking to keep the user credentials in a custom Object which will be kept in a SharedPreferences so that every Activity can easily access it. I'll use a couple of ListViews to show the content and AsyncTask to fetch the data needed. Should I keep all the data in memory in objects or should I use some sort of cache or a local database? Also, I'm planning to use a HashMap with SoftReferences to hold the bitmaps I am downloading. But wouldn't this eat a lot of memory?
How can all the activities have access to all these objects (ecommerce basket etc.)? I'm thinking of passing them using Intents but this doesn't seem right to me. Can SharedPreferences be used for a lot of objects and are there any concurrency issues?
Any pointers would be really appreciated. What are some good guidelines? What classes should I look into? Do you know of any resources on the Internet for me to check out?
A very detailed question I will do my best to answer it.
I used the following approach in my application:
I save the user credentials in the shared preferences. The preferences can only hold custom objects if they are serializable and writing and reading from flash memory takes a lot of time. Therefore I load the preferences on startup and store them in memory.
I try to keep all the data in memory that is needed in many places and in a consistent state, all the other memory is passed via serializing to json and passing through an intent or i pass only ids and I re fetch it from the net. (There is definitively a possibility for cashing in a local database but the effort to keep it up to date is to much work at the moment.) To store objects that take to long to reload from internal memory or network and re parse it I use a custom application that holds the reference to some controller objects which manage the caching. The application will stay in memory until your app is closed. This is convenient but can result in needing way to much memory if you are not careful.
The bitmaps that are downloaded by my program are cached on two layers. At the first time I want to access an image I create a image manager object inside my activity scope. That object will try to find the bitmap in an internal map. If it is not there it will try to load it from phone memory if it is not in the phone memory it will download it from the net, store it in the cache folder of my app and put it in the map. In this way the bitmap is accessible as long as the activity runs and is cleaned up at the moment the user changes to another screen. Until now this is sufficient for me.
At the end just begin programming and come back if you encounter other questions or errors and post some more specific questions.
Many useful techniques that you will need to use: ContentProviders, AsyncTasks, Bitmap Caching, ... are used in Romain Guys 'Shelvs' (http://www.curious-creature.org/2009/01/19/shelves-an-open-source-android-application/) app. It's a great start point to get the into a recommend Android flow.
Related
I am currently learning android programming and creating an app that will store some integers representing user choices (values inserted several times a day, must be displayed in the results activity) and steps data collected Google Fit HISTORY Android APIs, also displayed in the results activity. I am looking for the most efficient way to store this data. I know that it might be possible to insert the custom data types in the GOOGLE fit database. However, I am not sure if it is a good idea if the app mostly works offline, and it needs to immediately represent only a small set of results, for example, the values inserted in the last 2 weeks, with step counts. On the other hand, I am not sure if it is ok to have two databases storing the data.
My apologies if the question sounds a bit too amateur, I am doing my best to find an optimal solution in terms of performance.
Thank you for your answers.
So, to give you my opinion and answer (mainly opinion)
Android has 3 ways (mainly) for storing data:
Files
Online database/API
Local database
for this specific scenario you have listed, wanting the data to be available offline, you should probably be looking at using Room: https://developer.android.com/training/data-storage/room, as it supports storing primitive types without having to write any type converters, you can store models and custom data as well, it uses very basic SQL (because it's a wrapper for the older Sqlite database methods) and is part of android (not an external 3rd party library). Room also requires most operations to be done off of threads, instead of main threads and this will improve your performance as well (also has support for livedata/rxjava to observe straight onto any changes as they happen)
However, as I told this user here:
Should i store one arrayList per file or should i store all my arrayList in the same file?
When starting out, don't worry about the best way for doing something, instead, try something out and learn from it, worrying about the best solution now is rather pointless, either way, happy learning and coding :P
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
A little background: The main page contains about 25 thumbnail images displayed in a recycle view. I receive a JSON response from an API containing the urls for those images alongside other data.
I save the JSON string in an objectBox database instance and use GSON.fromJSON to create the actual objects when I need to display them. Btw I use the MVVM pattern.
Now there are three scenarios I would like to consider:
1) when changing activities any currently not displayed activity can be deleted at any point by the os without warning and the images with it. Should I cache the images (where? in memory (don't want to waste it) or storage?) or should I just download them again when user returns to the main activity? Seems to me like downloading the images every time user switches the activities would be quite demanding.
2) when user switches off the application entirely and launches it again everything not saved on disk will be wiped off. I would be ok to download the images in this case since it's just one time process and the database will probably need bringing up to date anyway.
3) finally when the phone is not connected to internet the app should display some images even though they might not be current.
So my question is, how should images be cached in scenario like this one and what's the right way to do it?
Can I cache images in objectBox directly or would i be better off with SQLite? Or should i use some other library like Picasso for caching?
Generally yes, images should be cached in at least memory, and depending on your app (how likely is it to be reused,etc) in memory and storage. If you want to support your 3rd point (displaying when offline), you need to do storage caching, and memory caching is optional but probably a good idea.
What library (or if you use a library, I've had good reasons to roll my own on a few times) to use is way off topic for this site, as its mainly opinion. And we'd need lots of specific knowledge of your usecase to advise one over the other.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm seeking assistance on a APP I need to create. I don't have no experience with android app development but, I'm studying and practicing.
I'm trying to build an app to record payments from a list of customers and print a receipt when paid. I download a list of customers with pending balance, using json array into android. Now, I'm confused, don't know if I should use arrays or a database to store the list, since I need to update (upload) later to the server. SQLite seems like an option but, I have to download the list every time user is connected to WiFi.
I guess I have to store the payments on an array and flush it once uploaded.
Can you please tell me what would be the best option for the tasks I'm trying to accomplish.
It depends on your use case. Arrays and sqlite can work independently or together.
If you are going to upload the data instantly after downloading within the same session, then you can keep it in the Array and upload it after what ever you need to do with it. Keep in mind that Arrays will be kept in memory and depending on where you are keeping the array object, they might not persist through the life cycle of an activity or the life cycle of your application.
SQLite on the hand writes data to disk, so you will be able to persist it even after user has backed out of your application.
...since I need to update (upload) later to the server
Based on the above, it seems like you should persist your data on disk. SQLite is one of the options for persisting data. Have a look here to see what else is available for persisting data.
You can load up any persisted data into your array and upload it after you have processed it.
Hope this helps.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am currently creating an app that is a guide for a game which involves the user navigating through categories and a large amount of information being displayed. For instance, if I wanted to review information regarding a specific weapon, I may navigate through Items > Weapons > Ranged > Bow. What would be the best to to set this up so the app would run smoothly? I may have up to 100 of these different items the user can view.
The two current approaches I am considering is either making a local SQL database of all of these items, and then having one activity that pulls from the database based on what the user selects. The other being to just make string resources and then load those resources depending on what item the user selects. Any opinions or insight would be much appreciated.
I am not sure about string resources but if you are doing "read only" operations on the data and not saving any user input I would suggest NOT using SQLite. The problem with SQLite for this use is that you will have to create the tables and populate the database programmatically the first time that the app is run. (someone correct me if there is another way to do this) Basically you will end up writing all of your information as strings or reading it from a file on the device anyway, it will just get inserted into the database and be accessed from there every time after. Another drawback is that every time you want to update the information in the app you will have to add rules in the "onUpgrade" method of the database handler. Accessing the data from SQLite will be faster than reading files but IMHO it is a way bigger pain than it is worth for just read operations (unless you have massive amounts of data.. "massive" in the computer world happens to be pretty darn big).
I would probably store the data in your own files (XML, JSON, or just plain text) and put it in the assets folder. Using a nice folder structure and good file naming in there would allow you to add new information without having to change your program.
Another option is to build a web database, pull the information from there, and save it to the local SQLite database when there is an update to the information that you saved online. This makes it easy for you to make revisions to the content without having to send out an app update but takes quite a bit more work
The Content Provider is the prefered way to store big data on the android platform and fortunately many tools are available to do the hard work for you. With that said I would hardly consider 100 a large amount of data even on a mobile device. Will the data change? In other words is it static or dynamic? I recommend the Content provider but you could use shared preferences, a resource file or even hard code your values.
I'm currently building an android application, and I'm making some HttpRequests to grab images from an API. Right now I'm just storing them in an object container, and then storing them in ArrayLists, but I want to store it into a temporary cache for the application so that when I quit out of that particular activity and go back into the launcher activity, when I go back into that activity, I won't have to make another httprequest for the image.
However, I don't know where to start, what to read up on, or anything regarding temporary storage. I've only used SharedPreferences, and passing extras along intents. Can anyone point me to any good places to get started with, either documentation or sample code?
edit: I forgot to mention that I'd like the data to be deleted when I quit out of the application. I'm not too sure what caching even means, so I don't know if this happens by default when people talk about "caching"
You could refer to Android - How do I do a lazy load of images in ListView.
There are a cache example in Fedor's LazyList.zip from the answers.
The Activity class has some great information on cache directories and the Lifecycle. If your cache is more than a couple megabytes you should consider using external storage.