NSStreams Schedule on a secondary topic

In the iPad application I'm developing, I need to put the network processing in a separate stream, as it sometimes blocks the user interface of the application. At the moment, I created a Connection object in which all the network logic goes ( NSStreams and its delegate methods).

The main obstacle is creating a secondary thread and scheduling NSStreams in the run loop of that thread. Explicitly creating NSThread , which then belongs to the Connection object?

I experimented with NSOperation , but this did not seem to be the best solution, since I feel the need for a thread dedicated to processing network events.

Pointers and tips are welcome. Any sample code could also be helpful.

Bart

+4
source share
2 answers

I like the detachNewThreadSelector ... approach, but FYI you can use NSOperation and NSOperationQueue . It will throw noncompetitive operations on separate threads.

To get streams, you look at things like this:

 [self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:mode]; 

Definitely look at Apple's “PictureSharing” sample at http://developer.apple.com/library/mac/#samplecode/PictureSharing .

In particular, copy the FileSendOperation and FileReceiveOperation and QRunLoopOperation . I also use the LinkedImageFetcher sample QWatchedOperationQueue , which works well with the PictureSharing classes. I took the classes * SendOperation and * ReceiveOperation and turned them into classes sending / receiving what I need (some NSData).

Then it is as simple as:

  FileSendOperation *op; op = [[[FileSendOperation alloc] initWithFilePath:somePath outputStream:outStream ] autorelease]; [self.queue addOperation:op finishedAction:@selector(networkingDone:)]; 
+1
source

I just came up with a search engine and I came up with this:

http://kdl.nobugware.com/post/2008/12/22/nsthread-iphone-template/

I think this is what you need;)

EDIT: http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/ Perhaps this is useful ...

If you read the code, you will see performSelectorOnMainThread (or something else) so that you can send back information from the stream to the stream.

0
source

All Articles