Obj-Cクラスの一部をiWatchでも実行できるように変換しようとしています。そのために、NSURLSession dataTaskWithRequestをNSURLConnection sendAsynchronousRequestの代わりに使用します。 : これは何の問題もないようです(XMLが正しく読み込まれたことを意味します)。元のコードの中には、いくつかの目に見える変更を実現するためのメインプログラムのNSNotificationがいくつかトリガーされました。NSURLSession dataTaskWithRequest内でNSNotificationが起動しない:
NSURLSession completionHandlerの内部ではもう動作しないようです。ここで
は元のコードです:
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *answer = [NSDictionary dictionaryWithXMLString:result];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_START_UPDATING object:nil userInfo:nil];
/* DO SOME STUFF THERE */
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_UPDATING_COMPLETE object:nil userInfo:answer];
}];
今すぐ修正版は次のようになります。
NSURLSessionDataTask *subDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *answer = [NSDictionary dictionaryWithXMLString:result];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_START_UPDATING object:nil userInfo:nil];
/* DO SOME STUFF THERE */
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_UPDATING_COMPLETE object:nil userInfo:answer];
}];
[subDataTask resume];
何狂気であることは、両方の通知のいずれも送信されないこと/第二の方法を使用して受信されます最初のバージョンでは正常に動作します。
同様に、私はこのように呼ばれる別の同じような状況でタイマーコール、持っている:
if (autoRefresh)
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(refreshInformationTimer:) userInfo:nil repeats:NO];
そして、ここでは再び、それはNSURLSessionDataTask内NSURLConnectionを使用して動作しますが、ない...
あなたは助けることができます私はこれで?
ありがとうございます。
** **のケースでは、辞書を作成する前に 'error'パラメータを処理する必要があります。 – vadian
それを指摘してくれてありがとう、私もそれを世話しますが、実際には私の主な問題の解決策ではありません:なぜNSNotificationとNSTimerはcompletionHandler内で起動しないのですか? –