Ios 如何将NSOperationQueue内的NSData分配给NSOperationQueue外的NSData
我需要从JSON下载数据,并将数据分配到Ios 如何将NSOperationQueue内的NSData分配给NSOperationQueue外的NSData,ios,json,xcode,nsoperationqueue,Ios,Json,Xcode,Nsoperationqueue,我需要从JSON下载数据,并将数据分配到NSData之外的NSOperationQueue中。这是我的密码: -(void)parsingInfo { NSURL *url = [NSURL URLWithString:@"http://someJSON.json"]; NSData *data; [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:downlo
NSData
之外的NSOperationQueue
中。这是我的密码:
-(void)parsingInfo {
NSURL *url = [NSURL URLWithString:@"http://someJSON.json"];
NSData *data;
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:downloadQueue completionHandler:^(NSURLResponse* response, NSData* jsonData, NSError* error){
if(error)
{
// Error Downloading data
NSLog(@"Error");
}
else
{
data = jsonData;
}
}];
if (data) {
NSError *error;
NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
application = [JSONDic objectForKey:@"Applications"];
NSArray *featured = [JSONDic objectForKey:@"Featured"];
NSDictionary *dict2;
dict2 = [featured objectAtIndex:0];
} else {
NSLog(@"Error, no data!");
}
}
将一个块传递到NSOperation中,该操作可以使用NSData对象作为参数调用。将一个块传递到NSOperation中,该操作可以使用NSData对象作为参数调用。如果要在到达“If(data)”语句之前等待“data”被填充,则。。 a) 进行同步请求呼叫
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
或者
b) 用户信号量阻止/中断此线程,直到您收到数据
-(void)parsingInfo
{
NSURL *url = [NSURL URLWithString:@http://someJSON.json];
__block NSData *data;
// Create a semaphore to block this thread ( or run loop) until we get response back from server.
dispatch_semaphore_t waitSemaphore = dispatch_semaphore_create(0);
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:downloadQueue completionHandler:^(NSURLResponse* response, NSData* jsonData, NSError* error){
if(error)
{
// Error Downloading data
NSLog(@"Error");
}
else
{
data = jsonData;
}
// Signal to release the semaphore (i.e. unblock the current thread).
dispatch_semaphore_signal(waitSemaphore);
}];
// A dispatch semaphore is an efficient implementation of a traditional counting semaphore. Dispatch semaphores call down
// to the kernel only when the calling thread needs to be blocked. If the calling semaphore does not need to block, no kernel call is made.
while (dispatch_semaphore_wait(waitSemaphore, DISPATCH_TIME_NOW))
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
// Release the semaphore.
dispatch_release(waitSemaphore);
if (data) {
NSError *error;
NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
application = [JSONDic objectForKey:@"Applications"];
NSArray *featured = [JSONDic objectForKey:@"Featured"];
NSDictionary *dict2;
dict2 = [featured objectAtIndex:0];
} else {
NSLog(@"Error, no data!");
}
}
请随时提问。如果您想在到达“If(data)”语句之前等待“data”被填充,那么。。 a) 进行同步请求呼叫
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
或者
b) 用户信号量阻止/中断此线程,直到您收到数据
-(void)parsingInfo
{
NSURL *url = [NSURL URLWithString:@http://someJSON.json];
__block NSData *data;
// Create a semaphore to block this thread ( or run loop) until we get response back from server.
dispatch_semaphore_t waitSemaphore = dispatch_semaphore_create(0);
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:downloadQueue completionHandler:^(NSURLResponse* response, NSData* jsonData, NSError* error){
if(error)
{
// Error Downloading data
NSLog(@"Error");
}
else
{
data = jsonData;
}
// Signal to release the semaphore (i.e. unblock the current thread).
dispatch_semaphore_signal(waitSemaphore);
}];
// A dispatch semaphore is an efficient implementation of a traditional counting semaphore. Dispatch semaphores call down
// to the kernel only when the calling thread needs to be blocked. If the calling semaphore does not need to block, no kernel call is made.
while (dispatch_semaphore_wait(waitSemaphore, DISPATCH_TIME_NOW))
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
// Release the semaphore.
dispatch_release(waitSemaphore);
if (data) {
NSError *error;
NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
application = [JSONDic objectForKey:@"Applications"];
NSArray *featured = [JSONDic objectForKey:@"Featured"];
NSDictionary *dict2;
dict2 = [featured objectAtIndex:0];
} else {
NSLog(@"Error, no data!");
}
}
请随意提问。将
if(数据)
部分代码移到NSOperation队列中。基本上,您的函数将启动一个新线程来运行下载,然后将立即继续检查数据是否存在(可能在下载开始之前)。好的,但是阵列应用程序将为nilOk,然后当异步请求完成时,使用知道数组应用程序是什么的新函数激发主线程。将if(data)
部分代码移动到NSOperation队列内部。基本上,您的函数将启动一个新线程来运行下载,然后将立即继续检查数据是否存在(可能在下载开始之前)。好的,但是阵列应用程序将为nilOk,然后当异步请求完成时,使用一个新函数触发主线程,该函数知道数组应用程序
是什么。你的意思是u block NSData*data吗?不,只有在要修改块中的数据时才需要它。您只希望操作将数据传递回块。您的意思是u block NSData*data吗?不,只有在您要修改块中的数据时才需要它。您只希望操作将数据传递回块。谢谢,现在速度非常快,但您应该替换NSData*数据;使用_块NSData*数据;为什么投反对票?这是一个公认的答案,我在我的一个项目中使用过这个解决方案。不要在iOS中使用同步网络/阻塞线程。没有必要,设计也很糟糕。您可以用更为cpu/UI友好的方式使用块或委托来完成任何事情。更不用说更简单了。@jsd-我同意您的说法:-不阻塞UI(主线程)。但是,如果您已经在另一个线程上发出数据获取请求(比如,用于某些数据获取目的),那么基于当前线程的设计/目的,信号量方法是合适的。最初的问题没有提到“主”线程,所以我的回答/建议对于用户面临的问题是有效的。希望我说服了你:)好的,我现在测试了它,它没有那么快,你有其他方法吗?谢谢,它现在非常快,但你应该替换NSData*数据;使用_块NSData*数据;为什么投反对票?这是一个公认的答案,我在我的一个项目中使用过这个解决方案。不要在iOS中使用同步网络/阻塞线程。没有必要,设计也很糟糕。您可以用更为cpu/UI友好的方式使用块或委托来完成任何事情。更不用说更简单了。@jsd-我同意您的说法:-不阻塞UI(主线程)。但是,如果您已经在另一个线程上发出数据获取请求(比如,用于某些数据获取目的),那么基于当前线程的设计/目的,信号量方法是合适的。最初的问题没有提到“主”线程,所以我的回答/建议对于用户面临的问题是有效的。希望我能说服你:)好的,我现在就测试了,而且没有那么快,你还有其他方法吗?