プロトコルを作成することをお勧めします。
@protocol MONStuffParserRecipientProtocol
@required
- (void)parsedStuffIsReady:(NSDictionary *)parsedStuff;
@end
ビューコントローラーを宣言するには:
@class MONStuffDownloadAndParserOperation;
@interface MONViewController : UIViewController < MONStuffParserRecipientProtocol >
{
MONStuffDownloadAndParserOperation * operation; // add property
}
...
- (void)parsedStuffIsReady:(NSDictionary *)parsedStuff; // implement protocol
@end
いくつかのバックエンドを追加します:View Controllerに
- (void)displayDataAtURL:(NSURL *)url
{
MONStuffDownloadAndParserOperation * op = self.operation;
if (op) {
[op cancel];
}
[self putUpLoadingIndicator];
MONStuffDownloadAndParserOperation * next = [[MONStuffDownloadAndParserOperation alloc] initWithURL:url recipient:viewController];
self.operation = next;
[next release], next = 0;
}
操作をView Controllerに保持させます:
@interface MONStuffDownloadAndParserOperation : NSOperation
{
NSObject<MONStuffParserRecipientProtocol>* recipient; // << retained
}
- (id)initWithURL:(NSURL *)url Recipient:(NSObject<MONStuffParserRecipientProtocol>*)recipient;
@end
データがダウンロードされて解析されると、操作メッセージが受信者に送信されます。
// you may want to message from the main thread, if there are ui updates
[recipient parsedStuffIsReady:parsedStuff];
実装するものは他にもいくつかあります。これは単なるフォームです。より安全で、ダイレクト メッセージ、参照カウント、キャンセルなどが含まれます。