Re: alternative to NSURLConnection sendSynchronousRequest
Re: alternative to NSURLConnection sendSynchronousRequest
- Subject: Re: alternative to NSURLConnection sendSynchronousRequest
- From: JB <email@hidden>
- Date: Tue, 13 Jan 2009 17:40:05 -0800
Great suggestion! However, the post method itself sits inside a
try-catch-finally statement:
@implementation Controller
- (void)sync{
@try {
NSLog(@"sync has begun");
... lots of setup, with potential to throw custom errors ...
[batch post]; // can throw custom errors too
}
@catch (NSException* exception) {
NSString* reason = [NSString stringWithFormat:@"%@", [exception
reason]];
[self setSyncStatus:[NSString stringWithFormat:@"Sync failed: %@",
reason]];
[self setSyncing:FALSE];
}
@finally {
if ([self isSyncing] == TRUE) {
// if still syncing, sync wasn't interrupted by error; set sync status
to success
[self setSyncStatus:[NSString stringWithFormat:@"Last sync: %@", [self
timestamp]]];
[self setSyncing:FALSE];
}
[self postSyncFinishedNotification];
}
}
@end
@implementation Batch
...
- (void)post{
NSMutableURLRequest* request = [self requestWithMethod:POST];
// this method wraps NSURLConnection initWithRequest:request delegate:self
[self sendAsynchronousRequest:request];
// should not be evaluated until sendAsynchronousRequest is complete
NSLog(@"post finished");
}
- (NSData*)sendAsynchronousRequest:(NSMutableURLRequest*)request{
NSURLConnection* connection = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
if (connection) {
// do nothing, handled by delegate methods
} else {
@throw [self connectionException];
}
return receivedData;
}
@end
I did this to have one place to catch and process errors for the entire sync
process, which includes errors that I throw upon receipt of responses
containing non-2xx status codes (not shown here, but appears in the delegate
method connection:didReceiveResponse:)
This works with the synchronous approach, but not with the async approach
because the rest of the try-catch-finally statement evaluates before the
request completes. Hence the request for suggestions on forcing the
sendAsynchronousRequest method to finish before moving on.
I've looked into locking the thread with NSConditionLock, but that feels
like overkill here. Might there be a more direct solution that I'm not
seeing?
Many thanks!
JB
On Tue, Jan 13, 2009 at 2:24 PM, Kevin Gessner <email@hidden>wrote:
> On Jan 13, 2009, at 4:27 PM, JB wrote:
>
> Hi all,
>>
>> I'm building a client for a server with (apparently) weird redirect
>> issues,
>> I cannot get a proper HTTP response using NSURLConnection
>> sendSynchronousRequest
>>
>> However, I can print out the response code using the asynchronous method:
>> [[NSURLConnection alloc] initWithRequest:request delegate:self];
>>
>> When I swap this method in, I can print out the status code just fine.
>> However, because this approach is asynchronous my code calls this method
>> and
>> moves on without waiting for a return value. I need the request to
>> complete
>> before moving on.
>>
>> How can I force my code to wait for the asynchronous request to finish,
>> without using sendSynchronousRequest?
>>
>
> You can split your post-connection stuff into another method, and call it
> from the delegate's implementation of connectionDidFinishLoading:. Then it
> will be called only when the connection is complete.
>
> HTH
> -- Kevin
>
> Kevin Gessner
> http://kevingessner.com
> email@hidden
>
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden