Detaching a thread to save a file?
Detaching a thread to save a file?
- Subject: Detaching a thread to save a file?
- From: Keith Blount <email@hidden>
- Date: Fri, 11 Dec 2009 14:43:35 -0800 (PST)
Hi,
Apologies for a rather basic question, but I’ve searched my Cocoa books and the web and despite lots of information on threads I’m still a little confused about how best to do what I want to do ensuring that I’m not opening myself up to crashes (possibly just nervousness owing to not using NSThread before).
My app allows the user to open multiple text files within a project. To allow in-project searching of this text, and also for Spotlight purposes, my app keeps a dictionary of plain text representations of all of these texts. In the current version, it writes this dictionary of plain text representations to disk whenever the user does a manual save. (This information needs to be saved to disk, but it’s not critical - if the app crashes and it’s not saved, it can be rebuilt from the rich text documents easily enough.)
The problem is that if the project grows in size, saving this dictionary to disk can take two or three seconds. For this reason, I would like to save it in the background, so it doesn’t hold up the interface. According to Scott Anguish’s Cocoa Programming book, NSThread is ideal for this sort of thing, and that is what I would like to do (I’m stuck supporting Tiger, so I need to use -detachNewThreadSelector:). But I can’t find any obvious examples of detaching a thread specifically to save a file.
So, my question is - and I already apologised for its basicness :) - what is the best way to detach a thread for saving a file, and what issues do I need to be aware of? For instance, what if the user tries to close the project (document) while the other thread is saving the file?
Presumably it’s not as simple as using -detachNewThreadSelector:... to spawn a thread that just does a simple -writeToFile: method.
If I’ve missed a really simple example somewhere, please feel free to tell me to RTFM, but I’d be grateful for a link. :)
Many thanks and all the best, have a great weekend,
Keith
_______________________________________________
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