0

I yet again have a super weird problem. I get different outputs whether I'm running the application on the simulator or on the iPhone.

See these console logs:

iPhone First run, first career cell clicked

2011-06-23 14:47:28.974 Acando[2033:307] 1 - loadView method running
2011-06-23 14:47:29.156 Acando[2033:307] 2 - viewDidLoad method running
2011-06-23 14:47:29.159 Acando[2033:307] 3 - viewWillAppear method running
[Switching to process 12803 thread 0x0]
[Switching to process 11523 thread 0x0]
2011-06-23 14:47:29.689 Acando[2033:307] 4 - viewDidAppear method running
2011-06-23 14:47:29.692 Acando[2033:307] View: UILabel, height: 21.000000
2011-06-23 14:47:29.695 Acando[2033:307] View: UIWebView, height: 400.000000
2011-06-23 14:47:29.851 Acando[2033:307] webViewDidFinishLoad method running
2011-06-23 14:47:29.855 Acando[2033:307] UIWebView dynamic height: 1150.000000
[Switching to process 12547 thread 0x0]

Simulator First run, first career cell clicked

2011-06-23 14:46:26.405 Acando[92098:207] 1 - loadView method running
2011-06-23 14:46:26.421 Acando[92098:207] 2 - viewDidLoad method running
2011-06-23 14:46:26.422 Acando[92098:207] 3 - viewWillAppear method running
[Switching to process 92098 thread 0x2003]
[Switching to process 92098 thread 0x207]
2011-06-23 14:46:26.484 Acando[92098:207] webViewDidFinishLoad method running
2011-06-23 14:46:26.485 Acando[92098:207] UIWebView dynamic height: 1150.000000
[Switching to process 92098 thread 0x6703]
2011-06-23 14:46:26.781 Acando[92098:207] 4 - viewDidAppear method running
2011-06-23 14:46:26.782 Acando[92098:207] View: UILabel, height: 21.000000
2011-06-23 14:46:26.783 Acando[92098:207] View: UIWebView, height: 1150.000000

It seems that on the iPhone, the method webViewDidFinishLoad is loaded after the viewDidAppear method.

But on the simulator the webViewDidFinishLoad is loaded before the viewDidAppear method.

What gives?

EDIT: What is even more weird is that this only happens when I click on the first cell that loads the viewcontroller which has my UIScrollView and UIWebView. All the subsequent cells that I click on the webViewDidFinishLoad method is loaded before the viewDidAppear method.

There has got be some way I can correct this?

Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
  • I have 4 MutableArray each array have 40 objects. One array contains 40 images. I shuffled these algoriathm and display Objects from the array.It displaying in order well. Some times I getting these message in console window [Switching to process 2297 thread 0x10703] After that displaying order will be changed. – Vineesh TP Aug 23 '12 at 07:46

3 Answers3

2

You should take into account data Internet connexion on the Simulator is far better than the one on the iPhone device. Be careful when working with synchronous and multithreading.

Stefan Ticu
  • 2,093
  • 1
  • 12
  • 21
  • To add to this, perhaps you can better simulate the device by forcing the load to wait for a little while, then run it. – Moshe Jun 23 '11 at 13:06
1

Your threads are executing in different orders on the phone, vs the simulator. This is not so much a bug as a feature of multi threaded programming. Are you getting crashes in your program or just curious on the execution order?

Perception
  • 79,279
  • 19
  • 185
  • 195
  • No crashes. I'm resizing a `UIScrollView` to the size of a dynamically height changing `UIWebView`. That's why I need to have the behaviour of the simulator where you can see UIWebView dynamich height matches the height of the view UIWebView. Which otherwise you can see on the phone do not match eachother so the scrolling is erroneous. – Peter Warbo Jun 23 '11 at 13:13
  • 2
    You could move your height adjustment logic to the - (void)webViewDidFinishLoad:(UIWebView *)webView method on the UIWebView. – Perception Jun 23 '11 at 13:34
  • Wow, Why didn't I think of that? Thank you! Works perfectly! – Peter Warbo Jun 23 '11 at 14:00
0

The simulator is running x86 compiled code in a simulated environment. There are bound to be some differences.

This is why you should always test on an actual device and not rely entirely on the simulator for your testing results.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • As a follow-up question then, how do I achieve the simulator behaviour on the device? That is having `webViewDidFinishLoad` run before `viewDidAppear`? – Peter Warbo Jun 23 '11 at 13:06
  • Perhaps you could synchronize the exit of viewWillAppear with the finish of webViewDidFinishLoad? i.e. don't exit viewWillAppear until webViewDidFinishLoad has completed – crashmstr Jun 23 '11 at 13:22
  • How would you do that programatically? – Peter Warbo Jun 23 '11 at 13:29
  • Best way would be to use a [mutex](http://en.wikipedia.org/wiki/Mutual_exclusion). Wait for the mutex in the viewWillAppear, signal it at the end of webViewDidFinishLoad. The bad way would be to sleep at the end of viewWillAppear until a bool is set in webViewDidFinishLoad. – crashmstr Jun 23 '11 at 13:36
  • Thanks for your input, I went with Perceptions advice. – Peter Warbo Jun 23 '11 at 14:01