2014-03-03 246 views
-2

我想使用異步請求獲取json數據,我正在通過syncrouns來完成此操作,但現在需要更改,但我無法將此代碼修改爲異步,因爲我必須返回NSdataJSON異步請求

+ (NSString *)stringWithUrl:(NSURL *)url 
{ 

// if(kShowLog) 
     NSLog(@"%@", url); 

    NSURL *newURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]]; 

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:newURL 
               cachePolicy:NSURLRequestReloadIgnoringCacheData 
              timeoutInterval:1]; 
    // Fetch the JSON response 
    NSData *urlData; 
    NSURLResponse *response; 
    NSError *error; 
// NSOperationQueue *opQueue; 

    // Make synchronous request 
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; 
    return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 
} 
+0

你爲什麼不嘗試使用[AFNetworking](http://afnetworking.com)? – cojoj

+0

檢查我的回答 – codercat

+1

你有兩個相互矛盾的要求:你必須返回的NSString對象,它是操作的結果,你的方法應是異步的。這是不可能的。調用方無法獲取異步方法_immediately_的結果。修復你的需求。 – CouchDeveloper

回答

-1

有什麼不對的異步然後,在2螺紋

 NSString *responseString; 
    NSOperationQueue *operation = [[NSOperationQueue alloc]init]; 
     [NSURLConnection sendAsynchronousRequest:request queue:operation completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
      NSLog(@"data %@", data); 
     responseString = [[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding]; 
     }]; 
    return responseString; 
+0

不工作,它說的NSData incompetible作廢 – user3355452

+0

試試我的解決辦法@ user3355452 – codercat

+0

爲什麼排隊呢? –

0

通話stringWithUrl如果你想保持返回數據。 (我用異步數據

dispatch_async(dispatch_get_global_queue(), ^{ 
    NSData *d = [self stringWithUrl:myURL]; 
    ... 

    //update ui 
    dispatch_sync(dispatch_get_main_queue(), ^{ 
     ... 
    }); 
}); 
+0

你必須要知道,thread0並不在於此同步給出的NSData ......那會異步點;) –

0

的方式,將工作,但也是相當的黑客(蘋果公司在Mac中老年的API使用所有的時間),將運行runloop去,同時等待:

hackisch ::但是在同步異步包裝給出相同的線程,則runloop上完成塊運行

__block NSString *responseString = nil; 
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:myurl] 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
    responseString = [[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding]; 
    if(!responseString) { 
     responseString = @""; 
    } 
    }]; 

    while(!responseString) { 
     [NSRunloop currentRunloop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]; 
    } 

    return responseString; 
+0

寫內聯和hackisch但會做請求的異步和等待(不阻塞) –

+0

它顯示了我什麼 – user3355452

+0

dateWithTimeIntervalFromNow找不到類方法 – user3355452

0

你必須創建一個單獨的線程這一過程,讓JSON數據會擋住你的主要的同步調用,線程。

我改變了你的代碼,以用於從Web服務獲取JSON數據做異步操作。

+ (void)stringWithUrl:(NSURL *)url 
{ 

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
dispatch_async(queue, ^{ 

    // if(kShowLog) 
    NSLog(@"%@", url); 

    NSURL *newURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]]; 

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:newURL 
               cachePolicy:NSURLRequestReloadIgnoringCacheData 
              timeoutInterval:1]; 
    // Fetch the JSON response 
    NSData *urlData; 
    NSURLResponse *response; 
    NSError *error; 
    // NSOperationQueue *opQueue; 

    // Make synchronous request 
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; 

    dispatch_sync(dispatch_get_main_queue(), ^{ 

     //Here you have to put your code, which you wanted to get executed after your data successfully retrived. 
     //This block will get executed after your request data load to urlData. 
    }); 

}); 

}