Re: Beachball on Lengthy Task
Re: Beachball on Lengthy Task
- Subject: Re: Beachball on Lengthy Task
- From: Dave Keck <email@hidden>
- Date: Sat, 4 Apr 2009 18:10:58 -1000
> Yeah, it really helped! I am trying to get the contents of the files in a
> certain directory, so I think that I could probably get away with using a
> timer.
In this case, the reason your task is taking several seconds to
complete is most likely attributed to the sheer time it takes the read
the contents of the files; that is, most of the time that it takes for
the lengthy-task to complete isn't due to lots of
computation-intensive stuff going on, but rather the age-old slow disk
read. On the list of the 3 best times to use threads, reading large
files from disk would perhaps be one of them...
I probably should have asked for more information before forming my
last post - for this case in particular, I would use NSFileHandle's
asynchronous file reading mechanisms (which create separate threads
for you, so you don't have to worry about it). Read about NSFileHandle
in the docs, specifically, check out
-readToEndOfFileInBackgroundAndNotify.
At any rate, to get the contents of every file in a directory, I would
iterate over the files in the directory, creating an NSFileHandle for
each one, and call -readToEndOfFileInBackgroundAndNotify for each.
Depending on how many files you're expecting, you may need to call
-readToEndOfFileInBackgroundAndNotify on a small number of
NSFileHandles, wait until those have finished reading, call it on the
next group of NSFileHandles, etc.
Note that while you may have escaped from using threads this time
(directly, at least) that's not always going to be the case :).
Threads are indispensable and with the number of cores chips are
sporting these days, will only become more so...
Good luck!
David
_______________________________________________
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