You can make a stream of posts instead of loading everything in memory with the AsyncEnumerable library:
using System.Collections.Async;
public IAsyncEnumerable<Post> GetAllPostsFromLikesThread()
=> new AsyncEnumerable<Post>(
async yield =>
{
for (var page = 0; ; page++)
{
var posts = await GetPageFromLikesThreadAsync(page);
foreach (var post in posts)
await yield.ReturnAsync(post);
if (!posts.Any())
break;
}
});
The GitHub page has examples on how to consume an IAsyncEnumerable<T>
. You can install the library with the NuGet package.