Re: How to set a TextField
Re: How to set a TextField
- Subject: Re: How to set a TextField
- From: Alex Zavatone <email@hidden>
- Date: Mon, 14 Mar 2016 04:27:21 -0400
Just do the calculation within a dispatch_async within computeSimething.
On Mar 14, 2016, at 4:17 AM, Gerriet M. Denkmann wrote:
>
>> On 14 Mar 2016, at 14:17, Quincey Morris <email@hidden> wrote:
>>
>> On Mar 13, 2016, at 23:50 , Gerriet M. Denkmann <email@hidden> 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.)
>
> You are absolutely right that a background would be the right thing.
> But this is just a small tool for testing, and it will not take more than a few seconds, so I am trying to avoid this.
>
> But I have found a solution (you mentioning setNeedsDisplay was a great help):
>
> - (void)computeSomething
> {
> self.textField1.stringValue = @“Start computing”;
> [self.textField1 display];
> // some seconds of computations
> self.textField1.stringValue = @“Result = 42”;
> }
>
> Gerriet.
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> 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:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden