NSURLConnection 的 didReceiveData 在 dispatch_async 块中不工作
NSURLConnection's didReceiveData Not Working in dispatch_async Block
我试图在通过 NSURLConnection 加载 URL 之前验证用户的令牌。
我的问题是,如果我在 dispatch_async
块中启动 NSURLConnection(请参阅下面的 1),则永远不会调用 (NSURLConnection *)connection didReceiveData
方法。
1.在新线程上调用令牌检查的方法:
- (void)awaitTokenCheckCompletion:(void (^) (BOOL success))completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, kNilOptions), ^{
[self checkAccessTokenExpiration];
dispatch_async(dispatch_get_main_queue(), ^{
});
});
}
上面的代码调用了 checkAccessTokenExpiration
方法,这将触发下面的 createClientCredentials
方法。
2。创建 NSURLConnection 的方法:
-(void) createClientCredentials {
DLog("Getting Access Token");
NSString *post = [NSString stringWithFormat:@"type=get&id=%@&key=%@",@"id",@"key"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@authorization/keys", serverURL]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
__attribute__((unused))
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
3。 (NSURLConnection *)connection didReceiveData 方法永远不会被调用:
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {
DLog("gotData");
}
根据文档
,您在块内设置委托
The delegate object for the connection. The connection calls methods on this delegate as the load progresses. Delegate methods are called on the same thread that called this method. For the connection to work correctly, the calling thread’s run loop must be operating in the default run loop mode.
所以,尝试在异步块之外设置委托
NSURL 连接将不会使用 dispatch_async 块调用。
如果您通过编写 conn start 在主线程中调用相同的代码,它将调用 NSURLConnection 的委托。
初始化并启动 NSURL 连接:-
NSURLConnection* conn = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
[conn start];
您可以选择 3 个不同的选项来调用 api 并获取数据:-
- 发送同步请求
- 发送异步请求
- 正在初始化 NSUrlConnection 并告诉它开始
参考这个 link :-
http://iosdevelopmentjournal.com/blog/2013/01/27/running-network-requests-in-the-background/
希望这个回答对您有所帮助。谢谢
我试图在通过 NSURLConnection 加载 URL 之前验证用户的令牌。
我的问题是,如果我在 dispatch_async
块中启动 NSURLConnection(请参阅下面的 1),则永远不会调用 (NSURLConnection *)connection didReceiveData
方法。
1.在新线程上调用令牌检查的方法:
- (void)awaitTokenCheckCompletion:(void (^) (BOOL success))completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, kNilOptions), ^{
[self checkAccessTokenExpiration];
dispatch_async(dispatch_get_main_queue(), ^{
});
});
}
上面的代码调用了 checkAccessTokenExpiration
方法,这将触发下面的 createClientCredentials
方法。
2。创建 NSURLConnection 的方法:
-(void) createClientCredentials {
DLog("Getting Access Token");
NSString *post = [NSString stringWithFormat:@"type=get&id=%@&key=%@",@"id",@"key"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@authorization/keys", serverURL]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
__attribute__((unused))
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
3。 (NSURLConnection *)connection didReceiveData 方法永远不会被调用:
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {
DLog("gotData");
}
根据文档
,您在块内设置委托The delegate object for the connection. The connection calls methods on this delegate as the load progresses. Delegate methods are called on the same thread that called this method. For the connection to work correctly, the calling thread’s run loop must be operating in the default run loop mode.
所以,尝试在异步块之外设置委托
NSURL 连接将不会使用 dispatch_async 块调用。 如果您通过编写 conn start 在主线程中调用相同的代码,它将调用 NSURLConnection 的委托。
初始化并启动 NSURL 连接:-
NSURLConnection* conn = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
[conn start];
您可以选择 3 个不同的选项来调用 api 并获取数据:-
- 发送同步请求
- 发送异步请求
- 正在初始化 NSUrlConnection 并告诉它开始
参考这个 link :-
http://iosdevelopmentjournal.com/blog/2013/01/27/running-network-requests-in-the-background/
希望这个回答对您有所帮助。谢谢