所以我有一些像这样的代码:
@interface RequestHandler()
@property (nonatomic) NSInteger statusCode;
@end
@implementation RequestHandler
- (bool)sendRequest:(NSString *)surveyorId withData:(NSData *)requestData
{
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
if (self.statusCode == 200)
{
return YES;
}
return NO;
}
显然,例程将在请求完成之前继续进入if-else
语句。因此,在检查之前,self.statusCode
未在委托didReceiveResponse
中正确设置。这样做的最佳方式是什么?
我只想添加另一个bool
属性,该属性将在connectionDidFinishLoading
中设置,然后循环直到设置此属性。完成后,它会检查self.statusCode
。但是我在想这会阻塞线程吗?它与sendSynchronousRequest
权利没有什么不同?如果不将它放入后台线程,有没有办法做到这一点?
答案 0 :(得分:1)
而不是您的sendRequest:withData:
方法返回指示成功/失败的BOOL,您的RequestHandler
最好有一个委托。然后,它可以让其代理知道异步请求完成时的成功/失败/其他任何事情,而不是尝试从sendRequest:withData:
方法返回此信息(正如您已经发现的那样, #39;工作得很好。)
所以,您可以定义委托协议这样的东西(仅作为示例 - 您可能希望在这些中包含更多信息):
@protocol RequestHandlerDelegate <NSObject>
- (void)requestHandlerSuccessfullyCompletedRequest:(RequestHandler *)sender;
- (void)requestHandlerFailedToCompletedRequest:(RequestHandler *)sender;
@end
然后,为RequestHandler提供符合此协议的委托属性:
@property (nonatomic, weak) id<RequestHandlerDelegate> delegate;
(确保将某些内容设置为代理人!)
然后,当您的异步请求完成时,您可以向您的代理发送相应的消息,例如:
[self.delegate requestHandlerSuccessfullyCompletedRequest:self];
您需要在NSURLConnection
中实施RequestHandler
委托方法(来自您的代码,我假设您已经完成了此操作),或者,如果您的目标是iOS 7 +,您可以改为查看NSURLSession
。
答案 1 :(得分:0)
您必须实现2个委托方法:
状态代码:- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
收到的数据:- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
示例用法:
<强>声明强>
@interface RequestHandler : NSObject <NSURLConnectionDelegate>
{
NSMutableData *receivedData;
}
请求强>
- (void)sendRequest:(NSString *)surveyorId withData:(NSData *)requestData
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// Apply params in http body
if (requestData) {
[request setHTTPBody:requestData];
}
[request setURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
<强>代表强>
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *responseCode = (NSHTTPURLResponse *)response;
if ([self.delegate respondsToSelector:@selector(didReceiveResponseCode:)]) {
[self.delegate didReceiveResponseCode:responseCode];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
receivedData = [[NSMutableData alloc] initWithData:data];
if ([self.delegate respondsToSelector:@selector(connectionSucceedWithData:)]) {
[self.delegate connectionSucceedWithData:receivedData];
}
}
答案 2 :(得分:0)
您可以在代码中使用NSURLConnection sendAsynchronousRequest
块,而不是将NSURLConnection与委托方法一起使用。在该示例中,您可以检查连接错误并比较状态代码。
NSURL *URL = [NSURL URLWithString:@"http://yourURLHere.com"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *rspreportStatus, NSData *datareportStatus, NSError *e)
{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)rspreportStatus;
int code = [httpResponse statusCode];
if (e == nil && code == 200)
{
// SUCCESS
} else {
// NOT SUCCESS
}
}];
答案 3 :(得分:-1)
您也可以通过记录此returnString来检查。
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSArray *arrpicResult = [returnString JSONValue];