tags:

views:

41

answers:

1

Hi all,

I am trying to find out how method calls works in objective-c.

I have a simple line of code in an iPhone app, wich is changing the appearance of a view, and then a view controller is called and attached to the main view.

switch(pressed) {

   case overall: {
      IPViewController *ipv = [[IPViewController alloc] initWithParam:(IPParam*)p]; 
      [self.view addSubview: ipv.view];
      [ipCustomTabbar switchOn:YES]; // <-- here at first attempt
   }
}

It happens that the allocation of ipv is quite time consuming, so the appearance of the ipCustomTabbar get changed after a bit. So I decided to move the ipCustomTabbar at first. But with my surprise I found that that the behaviour is not changed at all. The ipCustomTabbar is not changed until the ipv is initialized and attached to view. I was expecting that the first thing was a call/message on switchOn then execute the rest of the flow.

switch(pressed) {

   case overall: {
      [ipCustomTabbar switchOn:YES]; // <-- moved here 
      IPViewController *ipv = [[IPViewController alloc] initWithParam:(IPParam*)p]; 
      [self.view addSubview: ipv.view];
   }
}

EDIT: if I comment out the "time-consuming" lines everything is quite fast.

switch(pressed) {

   case overall: {
      [ipCustomTabbar switchOn:YES]; // <-- only this is fast 
      //IPViewController *ipv = [[IPViewController alloc] initWithParam:(IPParam*)p]; 
      //[self.view addSubview: ipv.view];
   }
}

What am I missing here ?

thanks

+1  A: 

Am I correct in assuming that [ipCustomTabbar switchOn:YES] changes the appearance of the view and you want it to happen straight away instead of waiting for the processing in -initWithParam: to finish.

The problem is in the way drawing is handled. Changing the state of a view object does not necessarily cause the drawing to be done straight away. Usually what happens is that a change of state of a view invalidates the view and puts it on Cocoa's list of things that need to be redrawn. The actual; drawing only occurs later and since it happens on the main thread, that will be after the processing of your event finishes including initialisation of your view controller.

What you need to do is avoid doing lengthy processing on the main thread as much as possible. You should consider moving the lengthy processing in the -initWithParam: method onto a different thread. It sounds like it's an ideal candidate for NSOperation.

JeremyP
the assumption is correct. I would have never imagined this cause. Could you please suggest some links or resources where this argument is explained ?
Leonardo
It's the standard way views are drawn. See "Drawing your view's content" in the View Programming Guide: http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html%23//apple_ref/doc/uid/TP40009503-CH2-SW25
JeremyP
the NSOperation definitively solve the problem, however seems that the whole process takes little bit time. Nevermind because I am displaying a "wait I am loading data" message. I used to program for the web, and I had forgotten how useful thread programming is :-)
Leonardo