Lazy UIScrollView swirl loading strategy

I have a horizontal scroll UIScrollview with paging enabled. Each page is a data feed. Each data channel consumes a fairly large amount of memory, including text and images displayed in a UITableView. Since a user can have an unlimited number of data feeds, I need to be too lazy to load them to prevent my memory usage from being exceeded. My idea is to store up to 5 channels of data in memory at any time and release something outside this range. My down payment is to keep the page in the viewport in memory, and 2 pages on either side of it. Thus, when the user scrolls the page, the next sequential page will always be in memory and will be displayed quickly.

Here is my problem: We also need to support a scenario where a user can skip a specific data feed, possibly 10 or more pages, left or right, which throws my entire lazy loading scheme out of the window.

Could there be a better strategy to support this scenario?

+4
source share
3 answers

Yes, what you can do is create a scroll view that contains individual cells that look like a table view. In this regard, the cells have a representation of the content that you can place in your data, let's assume that you know how to do this, since it seems like you are doing it.

Once you have such an architecture, you become clear enough: you can, knowing the width of your cells and screen size, some simple mathematical data can tell you how much you have on the screen, and you can add one left or right so that you pre-loaded some data when the user scrolls.

This will say it will give you the opportunity to have no more than 5 channels in your memory, if 3 are visible, at the beginning or end of the content view in scrollview, 4 feeds, regardless of whether you have a billion feeds.

One of the most important components of this is the reuse of cells. You support a pair of NSSet s, one for recycled cells and one for visible cells. Add items that have left the screen to the recycled cells, remove items from the recycled cells when setting up the cell to save additional memory allocations, which can be expensive. Just remember, using this strategy, you are still subject to the same warnings as UITableView regarding cell reuse.

I am connecting some kind of software that I wrote here, so forgive me for this, but I do it as an example of what I say, if you are stuck in the implementation of what I discussed here, it is available here for your reading .

Last remark. To support cell skips, it's just simple math to set a point on the scroll list. The action for this can be done using gestures, although now we are talking about the recognizer gestures indicating specific properties that will be specific to your application, as an example. Or you can use the buttons if you really should. Just make sure you know how to calculate your offsets in your cells, and scroll down to that point. Everything will be fine.

+7
source

It is difficult to give accurate advice with the information provided in the question. Therefore, I will simply present a few considerations that I got when reading the question.

Do I need to upload a feed? Is it possible to simply load several elements (on-screen) to immediately see the first few elements? Perhaps lazy loading images, if possible.

Have you considered caching feed data on disk? This way, you can immediately submit some data, and then update the feed when loading new data.

Since each page is a feed view, will the user simply scroll past the feed without looking at it? You really need to load 2 views on each side or you can walk away with 1 look on each side.

I don’t think your lazy loading scheme stops working just because the user can go directly to any page. You will need to start loading the data for this page when it is selected, but using the disk cache or loading a small sample, the application can be accelerated. I also think that using some kind of animation to animate on the selected page may buy you some time.

+1
source

Here is my problem. We also need to support a scenario where a user can skip a specific data feed, possibly 10 or more pages, left or right, which throws my entire lazy loading scheme out of the window.

Then you need to fix your data structure. I wrote quite accurately (this is a UITableView-like page scroll view) and it works fine (there are some overload quirks, but hey ...).

  • Keep track of which pages you have downloaded. I used ring-buffer-like modulo 8 and spent some time on the correct code; it's probably much easier to use NSDictionary with NSNumber keys and UIView values. This means that when you go from page 1 to 10, you can still say that the downloaded views are for pages 1, 2, and 3, and leave them alone.
  • Load the necessary pages in -layoutSubviews. I would upload 1 or 3; 5 is probably too much (but you need to load at least 2 if you are between pages).
  • Load 3 (one on each side) into -scrollViewDidScroll: This means that when you go from page 1 to 2, it does not try to load page 4 (which is not loaded yet).
  • Load 5 (two on both sides) into –scrollViewDidEndDragging:willDecelerate: if the value is NO, –scrollViewDidEndDecelerating: –scrollViewDidScrollToTop: and –scrollViewDidEndScrollingAnimation: The idea is that the animation is complete, so you can load additional views without the user noticing the delay.
  • Perhaps load 5 in –scrollViewWillBeginDragging: to make sure page 3 is loaded if you are currently on page 1.

I decided to make “two pages on each side,” because when you go from page 1 to page 2, it often shows the pixel width on page 3 when it bounces. This can be avoided by using the scroll insert, but we did not think about it at that time (oops). Otherwise, it’s not so important to choose 5 over 3.

A bit of work to get right, but otherwise, it seems to work flawlessly.

+1
source

All Articles