wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
335 stars 131 forks source link

The query result for Media is missing and duplicating media items at the edge of page limit #258

Closed navjot50 closed 3 years ago

navjot50 commented 3 years ago

I have the following code which gets all the media items for a given author:

public async Task<IEnumerable<MediaItem>> GetAllMediaForAuthorAsync(int authorId) {
      int pageNum = 1;
      var mediaQueryBuilder = new MediaQueryBuilder() {
        Page = pageNum,
        PerPage = Constants.WordpressConstants.PerPage,    //PerPage = 100
        Authors = new int[] { authorId }
      };

      var mediaByAuthor = new List<MediaItem>();
      List<MediaItem> pageMedia;
      do {
        try {
          logger.Log(logOrigin, $"Fetching page number {pageNum} media by author with id {authorId}.", LogLevel.DEBUG);
          var queryResult = await wordpressRestClient.Media.Query(mediaQueryBuilder);
          pageMedia = queryResult.ToList();
          mediaByAuthor.AddRange(pageMedia);
          mediaQueryBuilder.Page = ++pageNum;
        } catch (WPException) {
          break;
        }
      } while (pageMedia.Count != 0);

      return mediaByAuthor;
}

I have around 3000 media items and I am getting 100 items per request. The issue is that the 100th item of one page response is being duplicated as the 1st item of subsequent page response. For e.g. the 100th media item for 1st page response has id 7244, then the 1st media item of 2nd page response also has id 7244. And as a result of this duplication, one media item goes missing in the 2nd page response. So the media item with id 7245 never makes to the list of media items. Interestingly it is happening randomly and always at the edge of the page limit. Any help will be appreciated. I am working with Wordpress version 5.7.2 and the WordpressPCL version is 1.7.1