Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid how to avoid i/o problems when periodically writing to a file on internal memory
    primarykey
    data
    text
    <p>I'm writing an app which records various sensor (location for one) and device specific meta data to a file and then transmits the file to a server. It is not a background service - the app only needs to write and transmit files while the app is active (no need to set alarms to wake up a service). I'd like to write a row to the file every time <code>onLocationChanged()</code> is called (though location data is not the only data being written) - or at least at a similar rate at which <code>onLocationChanged()</code> is called. <code>onLocationChanged()</code> is currently being called once/second, but we may end up recording data at a higher rate (possibly 2-3x/second). That seems like a fair amount of i/o to the internal memory. </p> <p>I currently have everything working (proof of concept), but I need to improve on the methods I'm using to write to the file. I'm writing a row to the file each time <code>onLocationChanged()</code> is called, which is probably not wise and it seems is causing i/o to stack up. I've read other similar questions which touch on various methods (new threads, alarms, timer tasks, handlers, etc.), but I couldn't locate answers that were specific to what I'm trying to do. I've also considered other methods like caching/buffering data and only writing to internal storage on a less frequent basis (every 10 seconds?), or possibly writing to a SQLite db and exporting to a file later. What can I do to best uncouple (assuming that's what I need to do) the file code from the sensor code and ensure timely updates to the file? I also need to ensure that all data gets written to the file.</p> <p>UPDATE: The solution I ended up using involves appending to a StringBuilder for a set number of rows (one row per call to onLocationChanged()). After appending 10 rows worth of data, effectively buffering, I'm handing off the StringBuilder to an AsyncTask, where the data is written to file.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload