开发者

passing variables when calling methon in new thread (iphone)

i need to pass variables to the thread method when creating a new thread

my code is the follwing //generating thread

[NSThread detachNewThreadSelector:@selector(startThread) toTarget:self withObject:nil];

thread job

- (void)startThread:(NSInteger *)var img:(UIImageView *) Img{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [NSThread sleepForTimeInterval:var];
    [self performSelectorOnMainThread:@selector(threadMethod) withObject:nil waitUntilDone:NO];

//i need to pass Img to threadMethod: [pool release]; 开发者_Go百科 } thread Method

 - (void)threadMethod:(UIImageView *) Img {
        //do some coding.
    }

so how i can do this (pass parameter to both of methods


The code you provided as I see it is only using the thread to implement a delay. You can do this easily without introducing a thread like this:

[myImageView performSelector:@selector(setImage:)
                  withObject:image
                  afterDelay:5.0];

For more complex needs I have written a category on NSInvocation that allow you to easily call any method, independent of the arguments, on any thread.

You have for example this method as I see it:

-(void)doStuffWithImage:(UIImage*)image callbackAfterDelay:(NSTimeInterval)to {
  NSAutoreleasePool* pool = [[UIAutoreleasePool alloc] init];
  // ... do stuff
  [NSThread sleepForTimeInterval:ti];
  [self performSelectorOnMainThread:@selector(callbackWithImage:)
                         withObject:image
                      waitUntilDone:NO];
  [pool release];
}

This is easy enough, but spawning this method on a secondary thread is not that easy. My category allow you to do it with this simple code:

[[NSInvocation invocationWithTarget:self
                           selector:@selector(doStuffWithImage:callbackAfterDelay:)
                    retainArguments:YES, image, 5.0] invokeInBackground];

This is where you can find the code and a blog post elaborating on why and how it was implemented: http://blog.jayway.com/2010/03/30/performing-any-selector-on-the-main-thread/


You can pass only one argument by using withObject:, change your code as follows

[self performSelectorOnMainThread:@selector(threadMethod) 
                       withObject:image 
                    waitUntilDone:NO];

If you need to pass more than one value make it as a array, and then pass it.

And UIComponents are not thread safe, so be careful while passing UIcomponents to threads.


I'm reasonably certain UIImage isn't thread safe, so you may be out of luck there. In general though, any of these:

  • Make the object an instance variable

  • Make the object a global

  • Capture the variable in a block and use dispatch_async to do your threaded work instead of NSThread

  • Send the object to the thread using NSConnection

    etc...

Remember though, just because you have a reference to the object doesn't mean it's safe to use. Consider thread-safety guarantees (main thread only vs one thread only vs one writer only vs thread safe), and consider where you need to use locks or queues to guard resources shared between threads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜