3

I am working on a simple iPhone app that is loading data from the web service (RSS feed) inside UITableView. The first time app is loading, i am using a counter with initial value of 1 to load the first page of the feed:

- (void)viewDidLoad
{
    [super viewDidLoad];
    counter = 1;    
    [self fetchEntriesNew:counter];
} 

When the user scrolls down to the bottom of tableview, i am using the following method to load more data in the tableview by fetching the next page of rss feed by incrementing the counter:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (endScrolling >= scrollView.contentSize.height)
    {       
        NSLog(@"Scroll End Called");
        counter++;
        [self fetchEntries:counter];
    }
}

The counter is incrementing each time user scrolls down to the bottom of tableview, hence calling the next page of the rss feed. To this point, everything is working fine.

BUT, if the user after reaching at the bottom, scrolls up multiple times, the counter also gets incremented multiple times, hence a different page of the rss feed is reached and added to the bottom of the tableview.

To be more precise, if the user has reached to the bottom and scrolled up 3 times, instead of loading the second page, it will load the fourth page (3 times scrolled plus 1 already added in the start)

How can i restrict to not add another page entries if the one before it was not loaded?

Kindly do not suggest alternative strategies for handling more rows at the bottom of tableview or any third party libraries like NMPaginator and PartialTable etc. Thanks in advance!

UPDATE:

I have closely observed the app by running it again and again. It seems like if the user scrolls to the bottom (that increments the counter) before tableview is finished loading the page data (including images) that is already requested, it will just ignore loading the one that is currently loading and start loading the next one based on the incremented counter.

Jessica
  • 1,508
  • 15
  • 25
  • You can check this answer http://stackoverflow.com/questions/15898215/how-to-configure-uiscrollview-to-load-data-when-it-reaches-bottom-point/15898558#15898558 – Exploring Apr 12 '13 at 09:48
  • I don't see how the answer you suggested is helping to resolve the question i asked – Jessica Apr 12 '13 at 09:53
  • Could you keep a list of the counters that have been loaded? So everytime [self fetchEntries:counter] is called, the counter object gets added to a list and you only load counters that don't appear in that list. But an easier fix would be to just decrease counter when the user scrolls up? – JDx Apr 12 '13 at 10:03
  • @JDx 1) I have used counter for other purposes and counter serves me well between the runs of application but in this case, say if the user scrolls two times, it calls the second page and then third page and then loads the third page. So counters cannot fix that. 2) Decreasing the counter will just display whats already been loaded. – Jessica Apr 12 '13 at 10:09

2 Answers2

2

Can you please check the following code and let me know:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (endScrolling >= scrollView.contentSize.height)
    {   
        static int counterCheck = 0;
        ++counter;

        if (counter - counterCheck == 1)
        {
            [self fetchEntries:counter];
            ++counterCheck;
        }
        else
            --counter;
    }
}
viral
  • 4,168
  • 5
  • 43
  • 68
  • I have updated the question with the initial value of the counter inside my viewdidload. The code you provided is not working. Its not loading anymore cells anymore with this code. – Jessica Apr 12 '13 at 10:54
  • Your answer is solving half of the problem. Now, if the user after loading first page reaches the bottom and say scrolls up twice, it still loads the third page next although it should load the second. What it is doing right is that if a stop and run the app again, it will load the second if scrolled once and the third is already there cached. – Jessica Apr 12 '13 at 21:11
1

You could probably solve the issue by keeping record of what page you need to load, and what page was loaded last. Generally, you just need to make sure that you ignore all of the requests that end up asking you to load any page that is higher for more than 1 than the last page loaded. E.g:

if (pageThatNeedsToBeLoaded != lastPageLoaded + 1) {
    // ignore the request
}

Additionally, you could make a flag that keeps the state of your controller and fetching status, something like isFetchingData which will be set to YES when you send the request, and then setting it to NO once the data is loaded. If the flag is set to YES, you wouldn't call any other requests for more data. This is, assuming that your data is loaded incrementally, and that in no case you will load more than one page at once.

Adis
  • 4,512
  • 2
  • 33
  • 40