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: Sat, 12 Dec 2009 02:00:47 -0800 (PST)
Hi, many thanks for the replies Ken and Graham, much appreciated.
Graham - sorry, that method in the thread was just supposed to be a dummy method and, being late, I completely forgot to mention that. In reality it would probably go something like this:
- (void)saveSearchIndexes:(NSDictionary *)searchInfo
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *myURL = // get the URL...
MyXMLGenerator *xmlGenerator = [MyXMLGenerator sharedInstance];
NSData *xmlData = [xmlGenerator XMLDataFromSearchInfo:searchInfo];
[myXMLGeneratedData writeToURL:myUrl atomically:YES];
[pool release];
}
That said, from your reply I’m now thinking that the shared instance of “MyXMLGenerator” (it has a more meaningful name in the actual program of course) would also be a problem as that object could be used on the main thread, too, so should become:
MyXMLGenerator *xmlGenerator = [[MyXMLGenerator alloc] init];
...
[xmlGenerator release];
“myUrl” would have been generated like this, as self would be an NSDocument subclass:
NSString *path = [[[self fileURL] path] stringByAppendingPathComponent:@“searchInfo.xml”];
NSURL *myURL = [NSURL fileURLWithPath:path];
However, as this is information again from the main thread, am I to assume this would be a bad thing and that I should instead get this in a different manner? Perhaps by passing in both the URL and the searchIndexes objects as as a dictionary? E.g:
NSDictionary *searchInfo = [NSDictionary dictionaryWithObjectsAndKeys:searchStrings, @“SearchStrings”, myURL, @“URL”, nil];
[NSThread detachNewThreadSelector:@selector(saveSearchIndexes:) toTarget:self withObject:searchInfo];
Ken - thanks for all the info. That’s a good point about not detaching multiple threads. Really, I suppose I only want to spawn one, and if the user hits save again, he or she will have to wait for the first save to finish before the second takes place.
So, if I understand correctly, I could do something like this:
- (BOOL)writeSafelyToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError
{
// Do all the regular save stuff, accounting for save operation type etc and writing out
// all the document data (which isn’t as time consuming but is more critical, so will
// remain in the main thread.
// (Yes, I know this is an odd method to override, but for saving individual files
// in a file package without overwriting the lot each time it is apparently the best way).
if (isSavingSearchIndexes)
{
// Alert the user that a save is occurring by putting up a modal
// panel with an indeterminate progress bar, make a note that we
// will need to save the search indexes again, and return.
return YES;
}
NSDictionary *searchInfo = ...
[NSThread detachNewThreadSelector:@selector(saveSearchIndexes:) toTarget:self withObject:searchInfo];
isSavingSearchIndexes = YES;
}
- (void)searchIndexSaveCompleted
{
isSavingSearchIndexes = NO;
// Take down the modal panel if there is one and instigate another save if necessary.
}
- (void)saveSearchIndexes:(NSDictionary *)searchInfo
{
// Do the save stuff and then:
[self performSelectorOnMainThread:@selector(searchIndexSaveCompleted) withObject:nil waitUntilDone:NO];
}
Obviously all of the above could do with some refining as the modal panel probably isn’t a very nice solution, but am I on the right track?
Many thanks again and all the best,
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