waitForDataInBackgroundAndNotify
waitForDataInBackgroundAndNotify
- Subject: waitForDataInBackgroundAndNotify
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Wed, 2 Oct 2002 14:37:04 +0200
I have a document-based application where each document monitors a file.
Currently I do in each document:
[ NSTimer scheduledTimerWithTimeInterval: one_or_two_seconds target:
self selector: @selector(periodicUpdate:) userInfo: nil repeats: YES ]
and my periodicUpdate: method does:
NSData *data = [ theFileHandle readDataToEndOfFile ] ;
unsigned int length = [ data length ] ;
if ( length > 0 )
{
// do something with the new data
}
But I think it would be more elegant if my app just waits until new data
is available, instead checking every few seconds.
I have seen the FileHandle method: waitForDataInBackgroundAndNotify.
But I do not fully understand it.
Is this the way to use it:
1. document registers for NSFileHandleDataAvailableNotification
2. document creates new thread, passes the file handle to it, and the
new thread does waitForDataInBackgroundAndNotify. "When the data becomes
available, the thread notifies all observers with
NSFileHandleDataAvailableNotification. After the notification has been
posted, the thread is terminated."
3. upon receiving the notification, document reads new data and
processes it.
Then back to step 2.
Is this correct? Is this really the best way to do it? Is this more
efficient than my old way (periodic check)?
How does the document know that its file has changed, and not the file
belonging to some other document?
Also: there is another method:
waitForDataInBackgroundAndNotifyForModes: - what is this modes array
good for? Yes, I can read: "modes specifies the run-loop mode (or modes)
in which NSFileHandleDataAvailableNotification can be posted" but what
does this mean?
I guess I do not want NSModalPanelRunLoopMode nor
NSEventTrackingRunLoopMode or NSConnectionReplyMode, or do I?
And probably waitForDataInBackgroundAndNotify defaults to
NSDefaultRunLoopMode, which is the mode I should want. Correct?
Gerriet.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.