由於不幸中斷了該項目的工作,我正在將我的應用程序例程從ASIHTTP轉換爲AFNetworking ...以及後來發現的AFNetworking的更好更小的代碼庫。從ASIHTTPRequest轉換到AFNetworking
我在找幾個問題。我的ASIHTTPRequest代碼是作爲一種方法構建的。此方法需要幾個參數並將參數發佈到url ...返回結果數據。這些數據總是文本,但爲了製作一個通用的方法,有時可能是json,有時是XML或有時是HTML。因此,我將此方法構建爲獨立的通用URL下載器。
我的問題是,當例程被稱爲我有等待響應。我知道所有的「同步是壞的」論點......我不這麼做......但是對於一些我想同步的方法。
所以,這是我的問題。我的簡化ASIHTTP代碼如下,其次是我可以考慮在AFNetworking中編碼的唯一方法。我遇到的問題是AFNetworking在返回該方法之前有時不會響應該響應。 @mattt給出的提示[operation waitUntilFinished]
完全無法保持線程,直到調用完成塊...並且我的其他方法[queue waitUntilAllOperationsAreFinished]
不一定總是能夠工作(並且不會導致觸發[operation hasAcceptableStatusCode ]條款)。所以,如果任何人都可以提供幫助,那麼請不要猶豫不決地「異步設計」。
ASIHTTP版本:
- (NSString *) queryChatSystem:(NSMutableDictionary *) theDict
{
NSString *response = [NSString stringWithString:@""];
NSString *theUrlString = [NSString stringWithFormat:@"%@%@",kDataDomain,kPathToChatScript];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:theUrlString]];
for (id key in theDict)
{
[request setPostValue:[theDict objectForKey:key] forKey:key];
}
[request setNumberOfTimesToRetryOnTimeout:3];
[request setAllowCompressedResponse:YES];
[request startSynchronous];
NSError *error = [request error];
if (! error)
{
response = [request responseString];
}
return response;
}
AFNetworking版本
- (NSString *) af_queryChatSystem:(NSMutableDictionary *) theDict
{
NSMutableDictionary *theParams = [NSMutableDictionary dictionaryWithCapacity:1];
for (id key in theDict)
{
[theParams setObject:[theDict objectForKey:key] forKey:key];
}
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:kDataDomain]];
NSMutableURLRequest *theRequest = [httpClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"/%@",kPathToChatScript] parameters:theParams];
__block NSString *responseString = [NSString stringWithString:@""];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:theRequest] autorelease];
operation.completionBlock =^{
if ([operation hasAcceptableStatusCode]) {
responseString = [operation responseString];
NSLog(@"hasAcceptableStatusCode: %@",responseString);
}
else
{
NSLog(@"[Error]: (%@ %@) %@", [operation.request HTTPMethod], [[operation.request URL] relativePath], operation.error);
}
};
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
[queue waitUntilAllOperationsAreFinished];
[httpClient release];
return responseString;
}
非常感謝您的任何想法。
是的,它的確如此。非常感謝你。我將不得不更多地瞭解塊以開始更多地使用它們。 – Jann