2014-03-03 248 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. 
    }); 

}); 

}