Monotouch: How to Optimize Performance for Manual NIB Download

I use scrollview to display a multi-page view that contains more than 10 pages. (Scrollview.PageEnabled = true)

And each individual page in scrollview has about 6 subtasks (named: ABCUI), each of which is loaded from nib:

this.scrollview.DecelerationEnded+= scrollView_DecelerationEnded(...); public void LoadSubView(int nPageNo) { if (this.PageLoaded(nPageNo)) return; for (int i=0;i<6;i++) { ABCViewController abcUI=MonoTouch.Foundation.NSBundle.MainBundle.LoadNib ("ABCUI", this, null); //XIB file size: 20K abcui.SetTitle(...); abcui.SetXXXX(...); abcui.frame = .. pageFrame.X += this.ScrollView.Frame.Width+nPage*...; this.scrollview.addsubview(abcUI.view,...); } } public void scrollView_DecelerationEnded (object sender, EventArgs e) { int nPageNo=(int)Math.Ceiling((this.ScrollView.ContentOffset.X+1)/this.ScrollView.Frame.Width); this.LoadSubView(nPageNo +1); this.LoadSubView(nPageNo - 1); } public void Button1Clicked(object sender, EventArgs e) { this.ClearViewsInScrollView(); this.LoadSubView(1); } 

When the user starts pressing button 1, he loads the first page in scrollview (only one page at a time, but 1 page has 6 subtasks), and when the user scrolls the scroll, it loads the next page.

But it will take a lot of time when the first page or the switch page in scrollview loads, so the user must wait:

  • ipad1: about 1000 ms
  • iPad2: about 600 ms
  • in the simulator: 100 ms;

how to optimize performance (reduce to less than 300 ms / ipad1)?

+4
source share
1 answer

A very good question and a great time, since I have been working on something similar in the last few days.

Now I'm not sure if this solution will help you, 300 ms, but theoretically it is faster.

(You will see both “XIB” and “NIB.” I mean the same thing. After all, NIB is a “compiled” XIB.)

The key to this is to prevent each XIB file from being downloaded multiple times. There is no reason for this, since what you (we) mainly need from it are instances from objects in the XIB, and not the XIB itself, which takes up memory.

Fortunately, the iOS SDK provides a UINib class that can do what we want. With this class, we can create multiple instances of XIB content without having to load each XIB every time, only once at the "beginning".

Here's how to do it:

First create a UINib object for each XIB file you want.

 // Loads a NIB file without instantiating its contents. // Simplified here, but to have access to a NIB for the whole lifecycle of the app, // create a singleton somewhere. static UINib abcuiNib = UINib.FromName("ABCUI", NSBundle.MainBundle); 

Secondly, after loading NIB into memory, now you can get objects from it.

 abcuiNib.InstantiateWithOwneroptions(this, new NSDictionary()); 

Pay attention to the "this" parameter. Since this is the view controller that you want to load, the above line should be somewhere at the beginning of the object's life cycle, for example, in the constructor:

 public partial class ABCViewController : UIViewController { public ABCViewController() { // The first parameter is needed to set an owner (File Owner) for the objects // that will be instantiated. // The second parameter is for various options. It does not accept null in MonoTouch, // but you can just pass an empty NSDictionary. // If you have all your outlets correctly set up, the following line is all // that is needed. abcuiNib.InstantiateWithOwneroptions(this, new NSDictionary()); } // We don't need the following, as it will load the XIB every time //public ABCViewController() : base("ABCUI", null) {} // view controller implementation } 

Remember, I have not tested the above, since so far I have tested it using separate objects in XIB. If (and when) I use it with UIViewControllers in XIB, this is the direction I will go. I will also prepare an article with more detailed results and information in due time.

Also note that the same “rules” apply, for example. you still have to release all outputs in the ViewDidUnload override.

If after that you do not find a performance improvement, I think you will need to redesign your XIB. Apple suggests better to have multiple XIBs with multiple objects in each, rather than multiple XIBs packed with objects.

Helpful Reading: Apple's NIB Management Docs

+5
source

Source: https://habr.com/ru/post/1401020/


All Articles