0

新しいスレッドで実行されるメソッドを作成しました。

[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];});

しかし、これは、メソッドを呼び出すときも、呼び出さないときも同じ問題を抱えています。

これで私を助けてください。

4

2 に答える 2

0

デタッチされたスレッドの完了後にメインスレッドに通知できるデリゲートを作成することをお勧めします

また、別の解決策は、新しいスレッドの代わりに NSOperation と NSOperationQueue を作成することです。そこにあなたが望むものをスケジュールすることができます。あなた次第ですが、私にとっては簡単に見えます。

NSOperation https://developer.apple.com/library/mac/#featuredarticles/ManagingConcurrency/_index.htmlでさらに役立つリンクを次に示します。

于 2012-12-10T12:22:05.070 に答える
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 を好きなように変更できます。

うまくいかない場合は、Apple からのリンクを見てください。タイプミスか何かがある可能性があります。しかし、一般的には堅実に見えます。

于 2012-12-10T12:57:59.263 に答える