Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting from ASIHTTPRequest to AFNetworking
    primarykey
    data
    text
    <p>I am converting my app routines from ASIHTTP to AFNetworking due to the unfortunate discontinuation of work on that project ... and what I found out later to be the much better and smaller codebase of AFNetworking. </p> <p>I am finding several issues. My code for ASIHTTPRequest is built as a method. This method takes a few parameters and posts the parameters to a url ... returning the resulting data. This data is always text, but in the interests of making a generic method, may sometimes be json, sometimes XML or sometimes HTML. Thus I built this method as a standalone generic URL downloader.</p> <p>My issue is that when the routine is called I <em>have</em> to wait for a response. I know all the "synchronous is bad" arguments out there...and I don't do it a lot... but for some methods I want synchronous.</p> <p>So, here is my question. My simplified ASIHTTP code is below, followed by the only way i could think of coding this in AFNetworking. The issue I have is that the AFNetworking sometimes does not for the response before returning from the method. The hint that @mattt gave of <code>[operation waitUntilFinished]</code> totally fails to hold the thread until the completion block is called... and my other method of <code>[queue waitUntilAllOperationsAreFinished]</code> does not necessarily always work either (and does NOT result in triggering the error portion of the [operation hasAcceptableStatusCode] clause). So, if anyone can help, WITHOUT The ever-present 'design it asynchronously', please do.</p> <p><strong>ASIHTTP version:</strong></p> <pre><code>- (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; } </code></pre> <p><strong>AFNetworking version</strong></p> <pre><code>- (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; } </code></pre> <p>Thanks very much for any ideas. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload