0

我想轉換我的一個Obj-C類的一部分,以便能夠在iWatch上運行,爲此,我必須使用NSURLSession dataTaskWithRequest:而不是NSURLConnection sendAsynchronousRequest : 這似乎沒有任何問題(意思是正確加載瞭解釋的XML),但在我的原始代碼中,我爲主程序觸發了幾個NSNotification以實現一些可見的修改。NSNotification沒有在NSURLSession dataTaskWithRequest內部觸發:

看來,在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]; 

在這裏再次,它的工作原理使用NSURLConnection的而不是內部NSURLSessionDataTask ...

你能幫忙我在這?

非常感謝。

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