I have been told, “AppKit controls don’t take kindly to being updated from threads other than the main thread.”
I have 2 processes:
- update variables in the heap periodically
- display values from the heap to the display
I did do:
NSThread *thread_Client = [[NSThread alloc] initWithTarget:self selector:@selector(myTcpClient) object:nil];
NSThread *thread_Display = [[NSThread alloc] initWithTarget:self selector:@selector(displayData) object:nil];
but after awhile it blanks out or locks up.
What is a better approach?
thx
I’m a bit unclear about what you’re trying to do. It sounds like you are trying to perform a cyclical operation in a background thread and (at the end of each cycle) use the results to update the UI. Other postings suggest using an NSTimer in the main thread (which is the only thread that you should use for updating the UI). You would have the timer fire every so often (repeatedly); in the callback that is invoked when it fires, you would copy whatever data/info from the object(s) being updated by the background thread into appropriate UI views and then invoke setNeedsDisplay to cause updating of the UI. An issue is the consistency of the info you are using: if the background thread continues to update object(s), you could copy a mix of old and new info into the UI. Perhaps you need a ‘delivery’ object that the background fills in and then passes to the UI updater; such an object would contain a consistent set of info as yielded at the end of the background cycle.
In any case you do not want to be sleeping in the main thread. All it does is slow down the UI response.