On Mar 13, 2016, at 23:50 , Gerriet M. Denkmann <gerr...@mdenkmann.de> wrote:
> 
> - (void)computeSomething
> {
>       self.message1 = @“Start computing”;
>       //      some seconds of computations
>       self.message1 = @“Result = 42”;
> }

Assume, conceptually, that drawing only takes place asynchronously (that is to 
say, setting a new value on the string invokes setNeedsDisplay, that’s all), 
and it’s obvious why this isn’t going to work.

But the real issue is not that the text field won’t update, it’s that you’re 
blocking the main thread with computation. You should move the computation off 
the main thread — using dispatch_async to a default queue. At the end of the 
computation, go back to the main thread (dispatch_async again, specifying the 
main queue this time) and put the result in the text field.

This is, of course, going to introduce threading issues into your design. If 
the background thread keeps its results private (within the thread) until you 
switch back to the main thread, you can actually update any permanent data 
structures on the main thread, avoiding the need for locks or other 
synchronization techniques. However, you have to arrange for the main thread to 
avoid trying to use the results before the background computation is done, or 
to start any new computations, until the old ones are done, as far as such 
safety checks are necessary for your app.

(You can use NSOperationQueue instead of GCD, but I don’t see a lot of 
advantage in that. The consequences are the same.)

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to