Windows Phone, multiple HTTP request requests, how many?

In my Windows Phone 8 application, I am compiling a list of items from a web api. After that, I loop all the elements and get the details for each element.

My code now looks something like this:

List<plane> planes = await planeService.getPlanes(); // Get all planes from web api

foreach(Plane plane in planes)
{
    var details = await planeService.getDetails(plane.id); // Get one plane details from web api

    drawDetails(details);
}

How can I improve this to make multiple queries in parallel and what is the resonant number of queries running in parallel? The list of planes can be from 0 to 100 objects, usually no more than 20.

+4
source share
3 answers

How can I improve this to make multiple queries in parallel?

, (). SemaphoreSlim getDetails .

async Task ProcessPlanes()
{
    const int MAX_REQUESTS = 50;

    List<plane> planes = await planeService.getPlanes(); // Get all planes from web api

    var semaphore = new SemaphoreSlim(MAX_REQUESTS);

    Func<string, Task<Details>> getDetailsAsync = async (id) =>
    {
        await semaphore.WaitAsync();
        try 
        {           
            var details = await planeService.getDetails(id);
            drawDetails(details);
            return details;
        }
        finally
        {
            semaphore.Release();
        }
    };

    var tasks = planes.Select((plane) => 
        getDetailsAsync(plane.id));

    await Task.WhenAll(tasks.ToArray());
}

, ? 0 100 , 20.

, , . , :

ajax

WP8, , 100 .

+4

, , .

, , .

, , :

List<plane> planes = await planeService.getPlanes();

var allDetails = Task.WhenAll(from plane in plains
                              select planeService.getDetails(plane.id));

foreach(var details in allDetails)
{
    drawDetails(details);
}

.. , . Async, * # CamelCase.

+1

ServicePoint, HTTP-. , ServicePoint, 2. , , ServicePointManager.DefaultConnectionLimit. MSDN , . . .

-1
source

Source: https://habr.com/ru/post/1525013/


All Articles