Reputation: 728
I am making a UIScrollView where the content is infinite, for example a calendar with events in it. I am fetching the events from a backend using a REST call.
I am wondering if anyone have a pattern/chunk of code where the limits of:
When to fetch more data and
How much more to fetch
is easily adjustable.
For example i'd like to experiment with fetching events for 30 days (15 forward and 15 backwards), and when the user scrolls 10 days forward (5 days from limit) i'd like to fetch 30 more days forward, meaning i now have 60 days.
Thanks
Upvotes: 0
Views: 87
Reputation: 728
I made i simple method myself.
I have a void method in my model that takes a date, a numberOfDaysToFetch and a limit. numberOfDaysToFetch could be 90 and limit could be 30.
As the scrollview scrolls through the views (each representing a day) i call the method for each day that is visible.
If data for the day is not present locally or the day is closer than limit (30) to data not present locally, the method will call a service to fetch data (asyncrounously) for example from the newest date and numberOfDaysToFetch forward.
This means the method will first get 6 months of data. 3 forwards and 3 backwards. When scrolling 2 months forward, 3 more months of data is fetched.
Whenever a response is received from the service the data received is stored locally and a notification is thrown and a method will be called to update the views in the scrollview.
I can now adjust limit and numberOfDaysTo to what fits the performance of the webservice best.
Upvotes: 0
Reputation: 119031
Using a scroll view (whether UIScrollView
directly, or UITableView
, which could help you with memory management - unloading of non-visible content) you can use the scrollViewDidScroll:
delegate method and the contentOffset
property to check where the user is / has scrolled to and whether it's getting close to the end of the data you currently have. The algorithm you use is dependent upon the height of each item / day in your scroll view. In combination with the contentOffset
you can tell what day the user is scrolled to.
Upvotes: 1