{"id":43,"date":"2018-02-27T04:59:30","date_gmt":"2018-02-27T04:59:30","guid":{"rendered":"http:\/\/niveditaupadhyay.com\/?p=43"},"modified":"2018-03-01T07:21:11","modified_gmt":"2018-03-01T07:21:11","slug":"android-sharedpreferences-readwrite-may-slow-down-the-ui-rendering","status":"publish","type":"post","link":"http:\/\/niveditaupadhyay.com\/?p=43","title":{"rendered":"Android SharedPreferences read\/write may slow down the UI rendering"},"content":{"rendered":"<p>At work recently I was facing this issue where when we tap on the bottom navigation bar icon it was taking a few seconds to show the tap animation and render the new fragment. The fragment had couple network calls to get the data from the web service. When I started debugging the issue my initial hypothesis was that probably making the network calls, parsing the API response and then rendering the UI was taking a long time. To confirm my hypothesis I updated the logic to make the network call on app launch and store the data in the memory and when fragment loads just load the data from the memory and render the UI. My Fragment was not making any network call at this point because I already had data loaded in the memory. But surprisingly after all these changes I could still see the delay in rendering the fragment UI. I quickly realized that my hypothesis was wrong. At this point I had no idea what can potentially be causing this delay in rendering the fragment UI.<\/p>\n<p>Then I decided to comment all the functions in the fragment class and run the app. It loaded instantly. Then I started uncommenting the functions one by one. There was a function in the fragment class in which we were reading\u00a0the data from the SharedPreferences and displaying it. As soon I uncommented that function, the fragment rendering again became sluggish. Then I updated the logic to read the data from SharedPreferences on app launch and keep it in the memory while app is running and read it from there. It improved the fragment UI rendering significantly and QA was happy with it.<\/p>\n<p>The conclusion of this exercise was\u00a0 not to read\/write SharedPreferences unless absolutely necessary. Every storage operation has a cost. This one did have a hidden cost and took a while to figure out. It can slowdown the UI rendering drastically so use with caution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At work recently I was facing this issue where when we tap on the bottom navigation bar icon it was taking a few seconds to show the tap animation and render the new fragment. The fragment had couple network calls to get the data from the web service. When I started debugging the issue my [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":5,"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":48,"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/48"}],"wp:attachment":[{"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/niveditaupadhyay.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}