0

I'm not an app developer so this may be very simple to answer.

I have a webview that displays a simple HTML page. On top of this I have a simple bride from objective-c to javascript so I can send messages between the HTML page and the app.

In the HTML page there's a button, when you click it a message is sent to objective-c that starts an NSTimer. The timer calls a function every 300ms. The function sends a random number to the HTML page through the objective-c to javascript bridge.

This function is supposed to do some heavy processing and then send the result back to the HTML page. This function needs as much "power" as possible.

My question is simply, do I need to create a separate thread to handle the working and posting or is this not an issue since the communication is to a HTML page in a webview?

If I need a separate thread, please point me in the right direction where I can learn about it.

tbleckert
  • 3,763
  • 4
  • 32
  • 39

1 Answers1

1

Short answer: Yes, you should put your heavy processing outside the main thread.
You can use:

[self performSelectorOnMainThread:@selector(heavyProcessing:) withObject:@"Argument" waitUntilDone:NO];

or use GCD. Check this How do I create a NSTimer on a background thread?

If you really want to understand what to use and why, read this Concurrency Programming Guide

Long answer: It depends. If your HTML has animations, or it allows interactivity (like the user can press buttons and stuff happens), the WebView will freeze while the heavy processing is being done. But, if the content of the WebView is always the same and is updated only after the heavy process is done, then, the difference won't be noticeable. (Maybe the mouse icon keeps spinning).

Community
  • 1
  • 1
subzero
  • 3,420
  • 5
  • 31
  • 40