1

I'm adding records to an Azure data using the following method on a button click.But I'm wondering is there a better way in terms of performance and scalability to submit the data to the database without creating new items for each new record?

This is how I'm currently submitting data to an Azure mobile service:

            Item itemOne = new Item { Repititions = " " + fingersSpreadScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "Ted Bundy" , Exercise = "Fingers Spread"};
            await App.MobileService.GetTable<Item>().InsertAsync(itemOne);

            Item itemTwo = new Item { Repititions = " " + fistHeldScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "Joe Bloggs", Exercise = "Fist Held" };
            await App.MobileService.GetTable<Item>().InsertAsync(itemTwo);

            Item itemThree = new Item { Repititions = " " + waveOutScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "George Bush", Exercise = "Wrist Extension" };
            await App.MobileService.GetTable<Item>().InsertAsync(itemThree);
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • What do you mean by "* to submit the data to the database without creating new items for each new record*", bulk insert? – b2zw2a Dec 02 '14 at 21:53

1 Answers1

2

As far as I'm aware there's no bulk insert functionality in Mobile Service Table. One thing which may speed up multiple inserts is to execute them asynchronously and in parallel. In your code sample, you are waiting (await) for every InsertAsync to finish before you start next one. Would be faster to invoke all inserts and then wait for them all to finish. Sample code could look like this:

        var table = App.MobileService.GetTable<Item>();
        Item itemOne = new Item { Repititions = " " + fingersSpreadScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "Ted Bundy", Exercise = "Fingers Spread" };
        Item itemTwo = new Item { Repititions = " " + fistHeldScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "Joe Bloggs", Exercise = "Fist Held" };
        Item itemThree = new Item { Repititions = " " + waveOutScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "George Bush", Exercise = "Wrist Extension" };

        await Task.WhenAll(
            table.InsertAsync(itemOne),
            table.InsertAsync(itemTwo),
            table.InsertAsync(itemThree));

This way you will also eliminate unnecessary context switching after every insert.

Why should I prefer single 'await Task.WhenAll' over multiple awaits?

Community
  • 1
  • 1
b2zw2a
  • 2,663
  • 15
  • 15