0

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を使用して動作しますが、ない...

あなたは助けることができます私はこれで?

ありがとうございます。

+0

** **のケースでは、辞書を作成する前に 'error'パラメータを処理する必要があります。 – vadian

+0

それを指摘してくれてありがとう、私もそれを世話しますが、実際には私の主な問題の解決策ではありません:なぜNSNotificationとNSTimerはcompletionHandler内で起動しないのですか? –

答えて

0

私は再現できません、このテストは正常に動作します。あなたはそれを試すことができますか?

- (void)test07 
{ 
    static NSString * NotificationName = @"zekgnzerlkbnz"; 

    // Set an expectation that will be fullfill when we receive the notification 
    XCTestExpectation * ex = [self expectationWithDescription:@"download ok"]; 
    [[NSNotificationCenter defaultCenter] addObserverForName:NotificationName 
                 object:nil 
                 queue:[NSOperationQueue mainQueue] 
                usingBlock:^(NSNotification * _Nonnull note) { 
                 [ex fulfill]; 
                }]; 
    // Prep a download request 
    NSURL * url = [NSURL URLWithString:@"http://www.google.com"]; 
    NSURLRequest * request = [NSURLRequest requestWithURL:url]; 

    // Fire download. Send notif upon completion 
    NSURLSessionDataTask *subDataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

     [[NSNotificationCenter defaultCenter] postNotificationName:NotificationName object:nil userInfo:nil]; 

    }]; 
    [subDataTask resume]; 

    // Wait for 10 second or for exp to be fullfilled 
    [self waitForExpectationsWithTimeout:10 
           handler:^(NSError * _Nullable error) { 
            XCTAssert(error == nil); 
           }]; 
} 
関連する問題