I put a NSMutableURLRequest
request in dispatch_async
but it doesn't work. However, NSURLRequest
works. Code:
我在dispatch_async中放入了一个NSMutableURLRequest请求,但是它不起作用。然而,NSURLRequest作品。代码:
dispatch_queue_t queue1 = dispatch_get_global_queue(0, 0);
dispatch_async(queue1, ^{
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
[request setHTTPMethod:@"GET"];
NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:nil];
[connection start];//It doesn't work!
//***
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
NSURLResponse* theResponse = nil;
NSError* theError = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:theRequest
returningResponse:&theResponse
error:&theError];
//This works great!
});
Is there any difference between NSMutableURLRequest
and NSURLRequest
? Or, I use the NSURLConnection
in a wrong way?
NSMutableURLRequest和NSURLRequest有什么区别吗?或者,我用错了NSURLConnection ?
Thanks!
谢谢!
1 个解决方案
#1
2
It's not that you're using a mutable connection in one place and not the other, it's that you're calling the synchronous request method which runs immediately on the current thread versus an asynchronous method which needs a run loop to operate. From the documentation for -[NSURLConnection start]:
这并不是说您在一个地方而不是在另一个地方使用可变连接,而是说您正在调用同步请求方法,该方法立即在当前线程上运行,而不是需要运行循环才能操作的异步方法。[NSURLConnection start]:
If you don’t schedule the connection in a run loop or an operation queue before calling this method, the connection is scheduled in the current run loop in the default mode.
如果在调用此方法之前没有在运行循环或操作队列中调度连接,那么连接将在默认模式下的当前运行循环中调度。
Calling the asynchronous method from a block on a background thread is redundant. You should either call the async method on the main thread (it returns immediately and schedules its work in the background) or call the synchronous method in the async dispatched block. Doing it the second way, you don't have to deal with all the delegate callbacks, but you give up some control over the load operation.
从后台线程上的一个块调用异步方法是多余的。您应该在主线程上调用async方法(它会立即返回并安排它在后台的工作),或者在异步发送块中调用同步方法。采用第二种方法,您不必处理所有委托回调,但您放弃了对加载操作的一些控制。
#1
2
It's not that you're using a mutable connection in one place and not the other, it's that you're calling the synchronous request method which runs immediately on the current thread versus an asynchronous method which needs a run loop to operate. From the documentation for -[NSURLConnection start]:
这并不是说您在一个地方而不是在另一个地方使用可变连接,而是说您正在调用同步请求方法,该方法立即在当前线程上运行,而不是需要运行循环才能操作的异步方法。[NSURLConnection start]:
If you don’t schedule the connection in a run loop or an operation queue before calling this method, the connection is scheduled in the current run loop in the default mode.
如果在调用此方法之前没有在运行循环或操作队列中调度连接,那么连接将在默认模式下的当前运行循环中调度。
Calling the asynchronous method from a block on a background thread is redundant. You should either call the async method on the main thread (it returns immediately and schedules its work in the background) or call the synchronous method in the async dispatched block. Doing it the second way, you don't have to deal with all the delegate callbacks, but you give up some control over the load operation.
从后台线程上的一个块调用异步方法是多余的。您应该在主线程上调用async方法(它会立即返回并安排它在后台的工作),或者在异步发送块中调用同步方法。采用第二种方法,您不必处理所有委托回调,但您放弃了对加载操作的一些控制。