2012-12-10 6 views
0

새 스레드에서 실행중인 메서드를 만들었습니다.performSelectorOnMainThread 메서드가 호출되지 않습니다.

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

이 방법을 완료 한 후에 모든 데이터를 주 스레드로 보냅니다.

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

그러나 내 메인 스레드 메서드는 호출하지 않습니다.

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

을 사용하지만이 또한 같은 문제를의 호출 방법 또는 약간의 시간이 호출하지 약간의 시간이있다.

도와주세요. 난 당신이 분리 된 스레드

의 완료 후 메인 스레드를 통보 할 수있는 대리자를 만들 수 권합니다

+1

이 잘 나는 GCD가 깨진 생각하지 않는다, 그래서 당신은 어떻게 메서드가 호출되고 있음을 확인하는거야? – trojanfoe

+0

나는이 "getmostpopularResponse"를 호출하지 않고 breakpoint.its를 추적합니다. 내 이벤트는 모든 기능을 완료 한 후에 프런트에 오지 않지만 그 기능은 작동하지 않습니다. – jay

답변

0

또한 다른 해결책은 새로운 스레드 대신 NSOperation과 NSOperationQueue를 작성하는 것입니다. 원하는 일정을 예약 할 수 있습니다. 내게는 당신에게 달려 있지만 더 쉽게 보입니다. 여기

은 링크가 정말 신속을 작성합니다 NSOperation https://developer.apple.com/library/mac/#featuredarticles/ManagingConcurrency/_index.html

+0

답장을 보내 주셔서 감사합니다. 간단한 예제를 제공해 주시겠습니까? 내 요구 사항은 폰트의 일부 이벤트를 수행 할 수 있도록 백그라운드에서 webservice 호출을 보내야하고 응답을받은 후에 응답 데이터를 앞에 표시합니다. – jay

0

더 당신을 도울 것입니다.

@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 

나는 이것이 올바르게 작동한다는 것을 이해합니다. 또한 나중에 적절한 변환을 수행하는 동안 NSData를 원하는대로 변경할 수 있습니다. 애플에서 링크를 살펴 작동하지 않는 경우

, 어쩌면 약간의 오타 또는 뭔가가있다. 하지만 일반적으로 견고합니다.

관련 문제