scheduling a Thread after a Thread in iphone application
Asked Answered
L

2

6

I want to schedule a thread after a thread completion.

Is it possible ? How?

For example ( to specify my need )

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // 1. response - schedule myThread 
    // 2. response - schedule a new thread which will be executed after myThread
    // 3. response - schedule a new thread which will be executed after second thread
    // .....
}
Leuco answered 28/10, 2009 at 19:45 Comment(2)
Dare I ask why these all need to be separate threads, if they're executing a set of strictly serial tasks?Anorthite
Do you have fill example represent this case , i need it nowCachinnate
C
11

If you use NSOperations you can use the addDependency: method to specify an operation's dependencies.

NSInvocationOperation will probably be useful to you if you go this route.

Edit: I just re-read the subject and you're on iPhone so you don't have blocks, but for reference, if you have blocks available, NSBlockOperation is even better.

Note that when using these methods to perform asynchronous operations (and assuming the code in them uses autorelease) you'll be responsible for instantiating an NSAutoreleasePool when your method starts running and releasing/draining it upon exit.

Coarctate answered 28/10, 2009 at 20:3 Comment(2)
Blocks, and hence NSBlockOperation are not available on iPhone (yet). (Also, your links point to the Mac OS X documentation; since this was an iPhone question, the iPhone OS documentation would be more appropriate.)Transcontinental
Update: As of iOS 4.0 we now have blocks.Islington
L
2

(1) You can poll the threads isFinished property.

(2) You can register your dispatching/controlling object to receive the threads NSThreadWillExitNotification notification. For more precise control, you can subclass NSThread and to send a notification as the last act before the thread stops. Then in your controlling objects notification handler you can poll isFinished to make sure its done before launching the next thread.

See Thread Programming Guide and NSThread Class Guide.

League answered 28/10, 2009 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.