how to use block in [nsthread detachNewThreadSelector]

687 views Asked by At

I am using a block to download images in asynchronus call . I know how to use block in this way

 [self getimage:url block:^(UIImage *img) 
 {
    passblock(img);
 }];

but i dont know how to use block in this

 [NSThread detachNewThreadSelector:@selector(getimage:block:) toTarget:self withObject:string];

When this block is going to get callback in nsthread and how to handle it.

While doing this error that i am getting is bad access

 -(void)getimage:(id)strimage block:(imageblock)block
 {
  NSData *data=[NSData *data=[NSData dataWithContentsOfURL:[NSURL      URLWithString:strimage]];
UIImage* image = [UIImage imageWithData:strurl];
 block(image);
 }

Any help will be appreciated.

3

There are 3 answers

0
Fabio Felici On

The selector passed to detachNewThreadSelector must take only one argument.

Source

0
Droppy On

Use GCD instead:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self getimage:url block:^(UIImage *img) {
        // You might want to use dispatch_async(dispatch_get_main_queue()) here
        passblock(img);
    }];
});
0
Pavankumar On
dispatch_async(dispatch_get_main_queue(), ^{
    //call your method here 
});

//in your method taking two arguments so detachNewThreadSelector passing only one argument .So better use above code and call your method with more number of arguments.