0

I have created a method that is running in new thread.

[NSThread detachNewThreadSelector:@selector(setmostpopularReq:) toTarget:self withObject:mostPopulerstring]; 

After completed this method i send all data to main thread.

[self performSelectorOnMainThread:@selector(getmostpopularResponse:) withObject:self waitUntilDone:YES];

But some time my main thread method not calling.

i used

dispatch_sync(dispatch_get_main_queue(),^{[self getmostpopularResponse:mostPopularList];});

But this is also have the same problem some time its calling method or some time not calling.

Please help me in this.

Varun Mulloli
  • 658
  • 13
  • 28
  • 1
    Well I don't think GCD is broken, so how are you validating that the method is being called? – trojanfoe Dec 10 '12 at 11:33
  • I trace with breakpoint.its not calling these "getmostpopularResponse" my event is not coming on front after completed all function its must be goes to from but its not working. –  Dec 10 '12 at 12:04

2 Answers2

0

I would advise you to create a delegate with which you could notify the main thread after the completion of the detached thread

Also another solution would be to create an NSOperation and NSOperationQueue instead of a new thread. There you can schedule what you want. For me looks easier, though it depends on you.

Here is a link to help you more with NSOperation https://developer.apple.com/library/mac/#featuredarticles/ManagingConcurrency/_index.html

ipinak
  • 5,739
  • 3
  • 23
  • 41
  • Thanks for respond me would you please give me a simple example. My requirement is I have to send a webservice call in background so that i can do some event on font and after getting response I will display response data on front. –  Dec 10 '12 at 12:39
0

I will write this really quickly.

@protocol RespondDelegate
- (void)notifyWithRespond:(NSData *)data;
@end

@interface ContactWebServiceOperation:NSOperation
@property (nonatomic, assign) id delegate;
@end

@implementation ContactWebServiceOperation
@synthesize delegate;

// initialize here.
- (id)initWithDelegate:(id)delegate;
{
   if ([self = [super init]) { 
      self.delegate = delegate;
   }

   return self;
}

- (void)main 
{
    if (self.isCancelled) return;
    if (nil != delegate) {
        // Do your work here...
        work();

        // When finished notify the delegate with the new data.
        [delegate notifyWithRespond:your_data_here];

        // Or
        [delegate performSelectorOnMainThread:@selector(processImageForDownloadOperation:)
            withObject:self waitUntilDone:YES];
    }
}
@end



// Now on the view that you want to present the received results 
// you have to do one thing.
// Let's say that your view is called View1


@interface View1 : UIViewController<RespondDelegate>
// Here put whatever you like.
@end

@implementation View1

// Put here all your code.


- (void)notifyWithRespond:(NSData *)data
{
    // Here you will handle your new data and you will update your view.
}

@end

If I understand correct this should work. Also, you can change the NSData to whatever you like, as long as you perform the appropriate conversions later.

If it doesn't work take a look on the link from Apple, maybe I have some typo or something. But in general it looks solid.

ipinak
  • 5,739
  • 3
  • 23
  • 41