<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mayur Sonawane</title>
    <description>The latest articles on Forem by Mayur Sonawane (@the_mayur23).</description>
    <link>https://forem.com/the_mayur23</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F333527%2F95d61557-deaf-43cf-97f8-0f80daa0bcd2.jpeg</url>
      <title>Forem: Mayur Sonawane</title>
      <link>https://forem.com/the_mayur23</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/the_mayur23"/>
    <language>en</language>
    <item>
      <title>Hare-Pulling Experience: Image Downloads in React Native</title>
      <dc:creator>Mayur Sonawane</dc:creator>
      <pubDate>Mon, 29 May 2023 06:44:25 +0000</pubDate>
      <link>https://forem.com/the_mayur23/hare-pulling-experience-image-downloads-in-react-native-gch</link>
      <guid>https://forem.com/the_mayur23/hare-pulling-experience-image-downloads-in-react-native-gch</guid>
      <description>&lt;p&gt;Working with React Native is a lot like adopting a pet bunny. It's cute, versatile and runs on all platforms... until it doesn’t. I'm talking about the time I decided to give my app the task of downloading a mountain of images from a server. A Herculean task? Sure. But we've got our furry friend, the hare, right?&lt;/p&gt;

&lt;p&gt;Thinking the hare would spring into action and hop through the task with a flurry of simultaneous downloads using Promise.all(), I sat back with my cup of coffee, ready to witness the spectacle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;downloadImages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* Our eager hare's concurrent downloading code */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happened instead? Our bunny had a meltdown! It could only manage to download 497 out of a whopping 1644 images, and that too while playing hide and seek with the progress report. Turns out, our bunny was more like a very confused tortoise on a sugar high.&lt;/p&gt;

&lt;p&gt;Time for a different approach. But with Expo being the diva it can be at times, the game was on!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Big Batch Bake-Off
&lt;/h3&gt;

&lt;p&gt;Expo often gets a bad rep due to its limitations, but it shines when it comes to device capabilities. Taking inspiration from a reality cooking show, I decided to break down our download task into bite-sized portions. And thus, the batch processing code was born, taking into account the device's RAM, CPU, and OS version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getBatchSize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* My version of the Great British Bake Off, but for batch sizes */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Synchronizing the Tortoise... Wait, What?
&lt;/h3&gt;

&lt;p&gt;As fun as watching a hyperactive tortoise would be, the reality was surprisingly effective. I created a function that methodically downloaded images one at a time. And here's the kicker – it was faster! A whopping 2.5 times faster. The tortoise was on a roll!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleDownload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageUri&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* Where the tortoise comes to the rescue */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Progress Update: It's Not You, It's Me
&lt;/h3&gt;

&lt;p&gt;My initial plan to report download progress based on images turned out to be as confusing as a bunny in a lettuce field. Instead, I decided to focus on the number of products processed. Clear, simple, and no more confusing the hare with complex calculations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* Progress reporting - tortoise style */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Moral of the Story
&lt;/h3&gt;

&lt;p&gt;In the end, it was our slow and steady tortoise that came out on top, nailing all the downloads and providing accurate progress updates. The hare? Well, it's taking a much-needed nap.&lt;/p&gt;

&lt;p&gt;So, whether you're wrangling a rabbit or managing a meltdown, remember - sometimes, taking it slow can help you win the race, even in the fast-paced world of programming!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>expo</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
