Database modelled with ArrayList vs SQL in terms of efficiency - android

I'm developing an android app that uses various Lists of Java Modelled entities and FileInputStream and FileOutputStream to store and load these.
The average complexity of my dataManager is 4*O(n^3) for update all entries of 4 different data sets at the same time, where every operation requires 3 nested for on an ArrayList because an Object Palace contains a List of Apartament and an Object Apartament contains a list of People
Could I have noticeable slow down in the app due to my approach?
what is the efficience If I use SQLite to perform the same operations?

In my opinion SQL would offer some advantages.
In terms of efficiency, I can't tell you which one is better (file vs database) without discussing the amount of data you're holding. 2 or 3 records is a very different story than 10,000 records (without even considering the nested levels).
If you're using a file, you have to load everything manually into memory to access your objects, and this means (most of the time) loading the file completely, and saving it completely again.
A database would allow you to fetch only the information that you currently need, and it also allows you to only save the data that has been modified.

Related

Android - SQLite Database with Object Models

I am creating an Application in Android that uses a SQLite database. So far i have 3 table, one of which is used to link the first two tables together with foreign keys.
I have been reading a lot online about CursorLoaders, Content Providers, AsyncTask, URI's.... etc. A content provider seems like over-kill for my application, I do not intend to share this SQLite data with other applications.
My question: Is it a good idea in Android to create an object model of each row of Data from the database? My application wouldn't realistically have more than 500 objects from these databases, but is this good design/approach?
More Details: If I were to go down the Object route, I would use these objects and map them to Listviews mostly. Some of these objects may contain arraylists of other objects, Say for instance mapping these kinds of objects to expandable listviews. I tried this approach and it worked really well, I am just unsure of making all of these objects is wasting resources, slowing things down, or will it become a problem later if I decide to expand the application? What if i had 5000 Objects? Just unsure if it's a valid programming approach.

Android: Load database to memory vs queries over time

I'm working on a program that requires metadata information in order to populate some arrays. Let's say I have information like "Countries", "Districts" and a bunch of other metadata. That information is stored in a sqlite database. The program at some time need to load all the countries and iterate them in order to search for one. My question is: What is the best way to proceed: Keep the metadata in an array after query them, or every time I need them I should query the database?
Here's some more information so you can evaluate the performance:
Metadata tables (like countries): ~10
Estimated times I need to iterate the metadata: several (~100)
the arrays contains aprox. 5 fields (primitive types.)
If the amount of data is so large that if affects the amount of data available for your other data or for other app, you should keep it in the database and access it dynamically.
If the amount of data is rather small, and it's queried rather often, keeping it in memory is more efficient.
If the amount of data is rather small, and it's queried not very often, it will not make any noticeable difference what you do.
Your particular case is one of these three, but the only way to find out is to measure the performance yourself.

Using XML to store data

I am looking to use an XML file to store the data my Android app generates. With that in mind, I have two questions for the community:
Is XML the best way to store data on Android and most efficient in cases where data may be added or altered every second or less then a second.
If XML is indeed the best for the scenario described in #1, how do I go about setting it up?
1.) Is XML the best way to database data on android and most efficient in cases where data may be added or altered every second or less then a second.
Definitely not.
2.) If XML is indeed the best for the scenario described in #1, how do I go about setting it up?
If you plan to store data just locally, the best way would be SQLite which works as a local database on every device.
If you later plan to synchronize this data with a central database, you may do this asynchronously within an AsyncTask or a Thread which would run periodically, but writing each second into a XML file is a bad idea as far as performance goes.
It's probably also a bad idea synchronizing a remote database at each insert/modification/deletion operation as if you had many users you could collapse the remote database.
I think the best approach is (as previously said) having a local database where you would store that data, and implement a webservice in the remote side if needed and use it to periodically synchronize both databases.
I would use JSON over XML and I would highly consider using GSON from Google. You maybe want to consider writing directly to a database with it's own structure and use transactions and sets. Is there are reason you want to go through JSON/XML?
XML is one of the worst ideas to keep local data in Android.
Most common used is SQLite available on the Android platform, but it all depends on what data and how you want to use.
In many mobile applications you don't need the relational database for one of the following reasons:
You have no relational data (i.e. settings) => no point in making relational tables with 1 record each
You have small, and dynamically changed data (like cache for downloaded content)
You don't need to search for data (using indexes etc.)
What alternatives can be used?
Shared preferences - simple key/value storage of primitive objects
Data serialization - for your consideration - binary (native java), JSON, parcelable (can be combined with the shared preferences)
For most of my app I'm currently using the binary serialization for "local storage".
- It's fast enough (usually much faster than starting the local SQLite engine)
- It's extremely easy and quick to implement, especially when you are using it for json/xml downloaded data parsed to POJO objects. All you need to do is just put "extends serializable" and put few lines of code to serialize/deserialize whole structure
- You can use those same classes for keeping data locally and communication with backend
Of course - it all depends from the situation - if you want to keep locally log of data from some sensor, or allow others apps to use this data, have to quick filter 1k+ records, or you really like to write few hundreds lines of code SQLite will be the best option for you. But most of mobile applications has no clear reason to use the relational (and trust me - not perfect one) engine.

Choosing Best Storage Type in Android

There are many similar questions about this issue but I have clear points about my question to ask you.
I am new at Android development and before only I developed small applications which store small sized data. For example country List, calendar, birthday reminder etc. I stored my small data in single XML file and I parsed it with easy methods. This was enough for me. But for my Mobile Application Development Course I took a project which will store huge static data.
Specifications of my project will like these:
There are about 200 entities.
Each entity has about 20 sub categories which they stored in text format.
Each sub category has about 30-sub categories which they stored again in text format.
Also for each parent entity I will have 2-3 image
If I calculate simply, I have to store 200 X 20 X 30 = 120.000 static data for my application and data does not change. This is only install and use application. Online data interaction is not necessary. (If there are some changes for data I will relase major updates in long periods of time.)
My question is about storing method.
Which way should I choose? SQLite or XML parsing? For your answer can you explain advantages / disadvantages for your choice?
Interesting project, although not necessarily realistic.
To manage a large amount of "static" data, you'll want a database. XML parsing forces you to store the data in memory, which means that you have to read it into memory on a regular basis. Remember that you can't count the in-memory data being around when the user goes to your app; Android may have destroyed your app previously.
On the other hand, you can use an SQLite database on disk directly from your app. It's persistent, even if your app goes away. You'll have to load the database once, when you install the app.
Consider wrapping your SQLite database in a content provider. This will, among other things, allow you to do asynchronous queries using a CursorLoader.

SQLite or Serialization

I'm writing an application which needs to store data. A single pack of data is about 4 classes with many dependencies between them. For example, class A has a list of objects B and B has a list of objects C and few more dependencies...
And I wonder what would be better. Keep them in SQLite db or serialize each pack separately and store them in serialized files?
For me the only right solution would be to save the data inside a database especially if there are any dependencies. For beginners it might be hard at the beginning to get into database creation. but after you have created a database in the right form you just have to insert the data and you won't have any problems in the future if you want to change something or expand your app. With simple serialisation the logic has to be solved inside the app and might cause more problems especially if you have any dependencies.
If you need a good tutorial for saving data you should look at this tutorial
http://thenewboston.org/watch.php?cat=6&number=111
For other different solutions for saving data there are also some tutorials on the website, Nr. 108 - 110 of Android programming
IT depends on the usage of the data. You may do well choosing JSON/GSON serialization and avoid the overhead of doing ORM over SQLite. (Overhead meaning additional coding to marshall to/from the db) However, if your data is subject to growth or something that would be better managed by a db (a lot of non-sequential or random access across a larger data set) then go for SQLite and ORM. In the end it comes down to what type of data you are trying to manage. Again if your data set is something that could grow and involves a lot of random access it may be worth using SQLite.

Categories

Resources