Re: Detaching a thread to save a file?
Re: Detaching a thread to save a file?
- Subject: Re: Detaching a thread to save a file?
- From: Keith Blount <email@hidden>
- Date: Fri, 11 Dec 2009 17:37:35 -0800 (PST)
Hi Ken,
Many thanks for your reply, much appreciated. And thanks for pointing out the potential pitfalls. I most likely would have passed it the NSMutableDictionary ivar had you not pointed this out.
So, if I understand correctly, I could do something as simple as this:
// Presumably as the objects within the dictionary might
// change this should be a total copy:
NSDictionary *searchIndexesDeepCopy = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:searchIndexes]];
[NSThread detachNewThreadSelector:@selector(saveSearchIndexes:) toTarget:self withObject:searchIndexesDeepCopy];
// The save method:
- (void)saveSearchIndexes:(NSDictionary *)searchInfo
{
[myXMLGeneratedData writeToURL:myUrl atomically:YES];
}
I guess I expected to have to quit the thread on project close or something in the same way as you have to stop observing objects or notifications, or unbind programmatic bindings. And from looking at the docs I thought I would need to worry about locks etc.
Thanks again. I'll go ahead and try implementing this tomorrow. I really thought I must be making it too easy. :)
All the best,
Keith
--- On Fri, 12/11/09, Ken Thomases <email@hidden> wrote:
> From: Ken Thomases <email@hidden>
> Subject: Re: Detaching a thread to save a file?
> To: "Keith Blount" <email@hidden>
> Cc: email@hidden
> Date: Friday, December 11, 2009, 11:15 PM
> On Dec 11, 2009, at 4:43 PM, Keith
> Blount wrote:
>
> > 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.
>
> It could very well be that simple.
>
> The danger with secondary threads is if they may access
> data structures that are also accessible from other threads,
> especially if one of the threads may be altering the data
> structure.
>
> So, the easiest way to be safe is to have the thread deal
> only with data to which it has exclusive access. For
> example, if you were going to write an array to file, you
> might be tempted to pass to the background thread a
> reference to the NSMutableArray ivar that your document
> object holds. The problem is that while the background
> thread is invoking -writeToFile: on the array, the main
> thread is mutating it. One solution is to make a copy
> of the array in the main thread and pass the copy to the
> background thread.
>
> With respect to the user closing the document while the
> other thread is saving, that depends. From your
> description, it seems as though the thread would be writing
> a non-essential adjunct file, not the document itself.
> In that case, you should be fine. The thread may
> continue with its (independent) write operation.
>
> Cheers,
> Ken
>
>
_______________________________________________
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