Random programming things I'd want to remember

Wednesday, September 18, 2013

Efficient way to save lists to IsolatedStorage

I am working on a Windows 8 project and I needed a long list of strings in the isolated storage. I used this code:
for(int i=0;i<myList.Count;i++)
  await FileIO.AppendTextAsync(myFileName, myList[i]);

The method is of course marked as async, so when my debugger skipped over that line and nothing happened, I though something was wrong with Visual Studio. Then I set the debugger at my AppendTextAsync line and when it stopped, I realized that it was taking forever for the computer to append the items from my list, one-by-one (there were more than ten thousand items, and my computer is not the slowest one out there.

I recently read the book by Marijn Haverbeke called Eloquent JavaScript, which has a pretty heavy (well, in my mind) emphasis on functional programming. I remembered that there is a Join method that I could use to convert my list into a string, and I did it like so:

string result = string.Join(Environment.NewLine, myList.ToArray());
and voila! It took a split second.


No comments: