我在NSBlockOperation中使用NSURLConnection,我將此操作添加到NSOperationQueue.But它不工作。當我調用NSBlockOperation的啓動方法,然後它工作,但它不當我將NSBlockOperation添加到NSOperationQueue時工作。NSURLConnection不工作在NSBlockOperation而操作添加到NSOperationQueue
請任何人都可以幫我嗎?
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:1];
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLConnection *connection1 = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[connection1 start];
}];
NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://urls.api.twitter.com/1/urls/count.json"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLConnection *connection2 = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[connection2 start];
}];
[operation2 addDependency:operation1];
[operationQueue addOperation:operation1];
[operationQueue addOperation:operation2];
我還實施NSURLConnectionDataDelegate爲:
- (無效)連接:(NSURLConnection的*)連接didReceiveData:(NSData的*)數據
- (無效)連接:(NSURLConnection的*)美國康涅狄格州didReceiveResponse:(NSURLResponse *)響應
請幫 感謝
*爲什麼*你在使用NSOperationQueue並在NSOperationQueue上執行的塊中啓動(僅re * only * start *)NSURLConnection?你可以看看這裏:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/doc/uid/20001697-SW12 **調度委託方法調用** – CouchDeveloper