android marshmallow app crash large arrayList - android

Have an application that generates an arrayList of data collected from a rest call. The arrayList is used in an adapter supporting a listView. Had a case where the arrayList reach a size of 999 entries.
The application passes the arrayList via a putExtra on an Intent. The arrayList is used to populate a listView. When startActivity is called, the application crashes without generating a stack trace recorded in stacktrace.
Is there a limit to the size an arrayList can be? Have set a debug break point in the activity started and the crash is occurring right after calling the super onCreate method.
Ideas on how to debug this? Have been able to pass a 390 entry arrayList without crashing.

AS #CommonsWare said, there's a limit of < 1MB but in real practice is way less than that, as soon as you need to pass more data than a couple hundred KBs you need to use other procedures to pass the data around.
The most common one would be to save it into a Local database or other type of storage and then on the other activity query for the data (on the intent you only pass the data needed to query for the list later on) and instead of loading all of them at once, you use a CursorLoader so that you only load the data as you need.
Other quick and dirty way is by adding an static field to pass the data to the other activity and then on load read the value and remove it, the problem that you will face is that this is not Thread-safe nor it will solve the issue of what to do with the data on the other activity when onSavedInstanceState gets exectued, because you are going to run into the same 1MB issue. [WARNING: This is the worst solution you could think of, so, please try anything else before you try this one, that's why i gave you first a very good approach and leaved this one at the end]
[UPDATE:]
BTW the first approach is how we fixed this problem on the Microsoft Band app, because we had so much data coming as part of a very long run (GPS points, and other tracked data). Just make sure that the data is not big enough that you will generate a bottleneck somewhere else, we used to only cache it in memory and never send it to disk to avoid the serialization process or the time it took to save and retrieve from storage.

Related

Saving Firebase Data to Device

I want to save whole Firebase data to device.
ValueListeners and ChildListeners have to be called a lot of times in the app. This becomes a little cumbersome. Sometimes it makes the app a little slow.
I know we can use setPersistenceEnabled, but in that case Listeners have to be called.
So, is there any way, in which we can save data (database) in device in a way, from which accessing the data can be fast and easy?
The only way to get data from the server onto your device is by attaching a listener. But a convenient way to attach a listener without coding it yourself, is by calling keepSynced(true) on a reference or query. The SDK in that case attaches an empty listener to the node(s), so that the data in the cache stays up to date.

How to set some Data properly into Database

I have a task object that I would like to track time spent on. I am running a timer counting up for a variable in the task, but whenever I go to update the task inside the database it seems like it somehow gets set back to null even after I confirm that it updates. I was thinking that maybe I am not understanding the database class completely, and making the context that I am calling the database on somehow affects the data inside. Is there any reason that my data is being set to null?
Okay, so I figured this out. The problem was that I was creating multiple instances of the database in one activity. Also, I was using a service that was meant to share data across activities that both affect the data within the database in specific ways. When I passed between these activities, the application was essentially swapping between different instances of the same database, and therefore never really saved the data correctly and led to some weird behavior.
The moral of the story is that you should create one global database for your activity, and try to share the same database between two activities attempting to manipulate the same data.

Save gps-location to sqlite continuously

I'm trying to make my own gps-tracker, mainly for bike-rides. I managed to make a usable app, at least for personal use, but I wanted to improve it, and have added ContentProviders, Fragments, CursorAdapters and a Service to receive the onLocationChanges from GPS.
However, I have really no idea how to handle the stream of Locations I'm receiving. My old app just saved them to an ArrayList, so right now my Service is sending a Broadcast to my Activity, that saves them to the ArrayList.
Problem is, that when the ride is over, it takes from 5-15 seconds to save the locations to sqlite (yes, I'm using compiledstatement and transaction), and I would like to avoid that, by saving the locations when received. When I tried to do that, my app became unresponsive (as expected), as I was doing the insert in the UI thread, and I do receive location updates often.
This is of course the most important aspect of the app, so what is the solution?
I could do the insert in a thread, but since inserting a single record is expensive, I'm not sure it could keep up.
I could write 25 records (or more) at a time in a transaction, but there will be some logic to keep track of what is saved and what is not.
Is there other options for a better user-experience ?
Use an IntentService to delegate the saving to another thread, then use applyBatch to do inserts.
Personaly I think the gps track would be better in a file than embeded in the database tracks are lickly to be just big. It's the same senario as pictures where embeding in the database is not the recomended solution. The databases would just hold some summary information and a reference to the file. You wouild write out the file as you go so no big pauses at the end. You keep the local ArrayList as well while you are recording if you are using it for displaing a path on a map or a graphical plot etc. That's the way I am doing things in my biking app IpBike.

efficiency of application variable?

I have a database from where i need to extract quite a lot of data.
Now i get that data when required, i.e. I have made a class that handles database interactions and whenever an activity requires data it will call that class for the data. So at a time an Activity only has the bare minimum amount of data in memory (i.e. the data that it is using). But everytime i change an activity i have to perform database access to fetch data for the new activity.
Method 2
As opposed to this i have this other alternative, in which i make an application object and then perform database access in the beginning and then store all the data that i would require (in all the activities) in the application object. Whenever i need the data, i refer to the application object. The downside of this that i will be holding too much extra data that i am not using at a given instant.
Which of the above 2 approaches is better?
Thank you in advance.
It depends on your requirements and their priorities. If the time required for solution 2 is too long for you to accept then optimize (e.g. by using method 2, but in general I would advise against storing potentially all of your database in memory ... assuming the amount of memory will suffice).
Did you try solution 1. If the problem is only to read the data from database, it should not take too long to load the data for one activity. If complex calculations are involved you might be pressed to optimize. But don't optimize just in case!
I prefer the first approach because making a call to database is not costly until and unless it is being accessed by multiple applications.

Passing data between intents

Lectori salutem,
I'd like to implement a search in my application, my main data stucture, a ListOf(custom class) is in my main activity. Which is a json-ed object with over 2500 objects. The json file gets loaded/parsed on the startup of my application, this takes about 30 seconds. So passing the data in a .putextra is really not an option. The search dialog would take 30 seconds to even start searching, not to mention you'd have twice the memory usage.
So question is, what are my options here? Is there a way of passing the search query to my mail intent, and handle it there, so I can address the existing data structure?
It sounds like you would be best off using a SQLite database. You can even distribute a pre-populated database with your application to minimize load time.
You can get started here: http://developer.android.com/guide/topics/data/data-storage.html#db
Why not try to keep the information in a singleton object? Or in your App class for example? Any of these should give you access to the object anywhere in the app.
You should check if the data being copied into new object, or the reference of it being past. I'm pretty sure only the reference will be sent, and you don't have to wait for the object copied into new one. Therefore, stick with the putExtra, if you don't wan't to create static reference, who is the evil demon that summons memory leaks :)

Categories

Resources