android asynctask - How to edit SharedPreferences without blocking UI Thread? -


i want put many values in sharedpreferences doing blocking ui thread. tried in asynctask's doinbackground() , using handler, both attempts turned out of no use.

here's method:

        ....         context c = ramadanwidgetconfig.this;         saveddatasehri = c.getsharedpreferences("sehri", 0);         saveddataiftar = c.getsharedpreferences("iftar", 0);         boolean sfilled = saveddatasehri.getboolean("filled", false);         boolean ifilled = saveddataiftar.getboolean("filled", false);         if (!sfilled || !ifilled) {             fillsharedpreferences();         } ....  private void fillsharedpreferences() {      string date[] = {"6/18/2015", "6/19/2015", "6/20/2015", "6/21/2015", "6/22/2015", "6/23/2015", "6/24/2015", "6/25/2015", "6/26/2015", "6/27/2015", "6/28/2015", "6/29/2015", "6/30/2015", "7/1/2015", "7/2/2015", "7/3/2015", "7/4/2015", "7/5/2015", "7/6/2015", "7/7/2015", "7/8/2015", "7/9/2015", "7/10/2015", "7/11/2015", "7/12/2015", "7/13/2015", "7/14/2015", "7/15/2015", "7/16/2015", "7/17/2015"};     string sehri[] = {"4:13 am", "4:13 am", "4:14 am", "4:14 am", "4:14 am", "4:14 am", "4:15 am", "4:15 am", "4:15 am", "4:15 am", "4:16 am", "4:16 am", "4:17 am", "4:17 am", "4:17 am", "4:18 am", "4:18 am", "4:19 am", "4:19 am", "4:20 am", "4:20 am", "4:21 am", "4:21 am", "4:22 am", "4:22 am", "4:23 am", "4:24 am", "4:24 am", "4:25 am", "4:26 am"};     string iftar[] = {"7:24 pm", "7:25 pm", "7:25 pm", "7:25 pm", "7:25 pm", "7:25 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:26 pm", "7:25 pm", "7:25 pm", "7:25 pm", "7:25 pm", "7:24 pm"};      sharedpreferences.editor editorsehri = saveddatasehri.edit();     sharedpreferences.editor editoriftar = saveddataiftar.edit();      (int = 0; < date.length; i++) {         editorsehri.putstring(date[i], sehri[i]);         editoriftar.putstring(date[i], iftar[i]);     }     editorsehri.putboolean("filled", true);     editoriftar.putboolean("filled", true);     editorsehri.apply();     editoriftar.apply(); } 

basically, adding widget home screen , before adding checking if have data or not. method gets called if don't have data. if not called other code executes within milliseconds.

edit: found out problem lies somewhere else. clearing data of app before adding widget clear sharedpreferences, later found out clearing data clears else related first launch of app. app takes long while launching first time. perhaps setting things up, don't know what's going on. after clearing data , adding widget, app launched first time. why slowing down , thought sharedpreferences making slow.

now need figure out why app takes long in first launch , how avoid it. (splash screen comes first mind).

not clear part responsible populating data. sharedpreferences thread safe, doesn't support use across multiple processes.

with cold start (no data) how wait result? constant checking still can block ui, if use threading insert data.

if use huge amount of structured data set consider use sqlite. populate data may use service.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -