threads vs run-loops for keeping your GUI responsive
threads vs run-loops for keeping your GUI responsive
- Subject: threads vs run-loops for keeping your GUI responsive
- From: Niko Matsakis <email@hidden>
- Date: Fri, 9 Dec 2005 15:44:48 +0100
Okay, I want to get away from the spinning beachball that inevitably
results when churning through a lot of data. I am writing a Core
Data app.
As a motivating example, I have an import function which reads in a
text file and creates corresponding objects. If the text file is
large, this can take quite a while. At the moment, I just create a
progress bar and update it: thus reassuring the user that something
is happening, but not really allowing them to do anything (such as,
say, cancel it).
Now, I know that I could create a parallel worker thread. The Cocoa
tools for doing so (performSelectOnMainThread:, detachThread:) are
quite nice. However, it seems like overkill for my task. Rather
than have to think about whether I have locked down access to my
NSManagedObjectContext, and whether or not some method is part of the
AppKit or not and therefore needs to happen on the main thread, I'd
rather just use a single-threaded approach.
It seems like what I really want to do is simply have a function
which does a discrete amount of work: processing one line from the
file, for example, and to repeatedly schedule calls of this function
onto the run loop. This way, if no other events should occur, my
function can get invoked, and I can do some work. Eventually, when
the work is done, I can exit the modal session as usual.
Transforming my code to work this way would be quite trivial.
Unfortunately, it is not clear to me how I might schedule a call to
this event. I have scoured the wiki and mailing list, but I can't
find much mention of this approach: everyone seems to want to create
worker threads.
I know I could use NSTimer, but again that doesn't feel quite right:
I want the function to be called ASAP, assuming that nothing else is
going on; scheduling it for a certain time seems overly complicated.
Surely there is a way to do this? Am I missing something? Is this
not a good approach?
Niko
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden