0

I am developing an app in MonoTouch for iOS.

A big part of the functionality is that the user will need to swipe between pages (screens) of data continuously and go back and forth.

I need to keep in mind memory consumption, so my thought is I could only have a few screens in memory at a time. For example, lazy load the next 1 screen ahead of time only, so it's available when the user swipes to it. (Or lazy load on swipe).

When the user swipes back to a previous page they were on, I need to make sure that that page is still in memory and hasn't left memory. Pages that are a few behind, I can lazy re-load once the user swipes back. Therefore, I will need a mechanism to send screens to GC if they have not been viewed by the user in a while and have them automatically loaded when the user swipes to that page.

I was thinking of creating a List of objects where each object has information about what to display on the view along with a reference to the actual view itself. If this reference is null, I know to create the view. If the reference exists and is not disposed I can just show the user that existing reference. As the user swipes left and right, I can go to the next (or prev) item in the list and do the same thing. Check if it has a view reference and load that view, or create it.

Performance is key here, as the user may be swiping through potentially hundreds (or more) of screens.

Any thoughts on the best architecture for speed and memory management?

user1060500
  • 1,505
  • 1
  • 21
  • 40

1 Answers1

0

Key would be to reuse views if possible. In my app I have swiping left/right similar to what I think you want, and I just have to create up to three views: one for "on screen" and then right/left off-screen (although left is not created until needed).

You can get pretty creative by doing as much as possible related to your page build-out in a thread and just update the UI on main thread. Also I definitely focus first on the to-be visible page when loading new dataset before moving to the right not visible page and, if it makes sense, left not visible page. When user swipes to new page, the views get reused and moved around the larger UIScrollView content area.

t9mike
  • 1,546
  • 2
  • 19
  • 31
  • I am going to work on implementing something like this. My PoC implementation has me creating a new view for each screen as user swipes left and right and I keep a List of the views that are created, so if it already has been created it will reuse it. It's a bad approach and and going to refactor it. I may comment with additional questions. – user1060500 Nov 19 '12 at 00:02
  • Suggest accepting my answer or add your own answer and accept it. – t9mike Nov 24 '12 at 13:52
  • Don't worry about this. I will once I've implemented it and I am satisfied with the results. – user1060500 Nov 24 '12 at 16:11
  • I have a question about your approach. How do you handle the ViewControllers collection. When you navigate back, you are swiping right, at this time you will pop to the previous VC, thus the existing ViewController you are leaving will be gone - removed from ViewControllers collection, and you will have to create a new instance of a view controller regardless. Right? – user1060500 Nov 25 '12 at 15:59
  • When I said view reuse above I was referring to within the UIScrollView. I.e. it might contain 100 different virtual pages, but only three real views representing a subset of those pages: the current page UIView, the UIView for the left page, and the UIView for the right UIView. – t9mike Nov 25 '12 at 23:10
  • Regarding view reuse with different controllers, Apple has depreciated ways to find out when a view is unloaded from a controller in iOS6, making it more difficult to do what you want. But [this SO thread](http://stackoverflow.com/questions/1816614/viewwilldisappear-determine-whether-view-controller-is-being-popped-or-is-showi) has info on how to determine if your view controller is being popped. – t9mike Nov 25 '12 at 23:12
  • Thanks! If Apple is deprecating that approach then I will try something different. I will try it with a UIScrollView. I would actually like to take an Apple best practice approach to this. For example: Look at how Photos app works. Swipe left and right to navigate between photos and videos. I want to do the same thing, just with different content (a ViewController). Do you think Apple is using ScrollView here, or how are they doing it? – user1060500 Nov 26 '12 at 02:53