简体   繁体   English

异步加载数据以在 .net core 2.2 中查看

[英]Load data asynchronously to view in .net core 2.2

I am consuming wego api to list out the flight.我正在消费 wego api 列出航班。 but i will get small amount of data in each request so i want to load all data asynchronously to the view.Is there any solution in .net core但我会在每个请求中获得少量数据,所以我想将所有数据异步加载到视图中。.net 核心中是否有任何解决方案

For data pulling I am using the below code.对于数据提取,我使用以下代码。 so after reach to view i don't want to stop the data getting.because I got only 100 records i need to get the remaining data in view and here there is no pagination所以在查看后我不想停止数据获取。因为我只有 100 条记录,我需要查看剩余的数据,这里没有分页

const int numberOfResultsToGet = 100;
var results = new List<SearchResultMv>();
while (results.Count < numberOfResultsToGet)
{
    var response = await GetFlights(flightParam, auth);
    results.AddRange(response.Results);

    // update offset
    flightParam.Offset += response.Results.Count;

    // sleep for 1 second before sending another request
    Thread.Sleep(TimeSpan.FromSeconds(1));
}
    int Parallelism => 5;
    int NumberOfResultsToGet = 100;
           [HttpGet("get")]
    public async Task<IActionResult> GetFlightsAsync()
    {
        var flights = await 
        GetFlightsByPartitionAsync(GetPages(NumberOfResultsToGet));
        return Ok(flights);
    }

    private async Task<List<SearchResult>> GetFlightsByPartitionAsync(IList<int> 
   pages)
    {
        var result = new List<SearchResult>();

        var output = System.Collections.Concurrent.Partitioner
               .Create(pages)
               .GetPartitions(Parallelism)
               .Select(partition => Task.Run(async () =>
               {
                   using (partition)
                   {
                       while (partition.MoveNext())
                       {
                 // Perform your logic here
                           lock (result)
                               result.Add(new SearchResult() { FlightNo = partition.Current });
                       }
                   }
               }));

        await Task.WhenAll(output);
        return result;
    }
    private IList<int> GetPages(int results)
    {
        IList<int> pages = new List<int>();
        for (int iteration = 1; iteration <= results; iteration++)
            pages.Add(iteration);

        return pages;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM