Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

iphone - error handling with NSURLConnection sendSynchronousRequest

how can i do better error handling with NSURLConnection sendSynchronousRequest? is there any way i can implement

- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error

i have a nsoperation queue which is getting data in background thats why i have sync request. and if i have to implement async request then how can i wait for request to get complete. because that method can't proceed without data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would not rely on the error being non-nil to indicate that an error occurred.

I have been using the error checking as described in Ben's answer, but I believe it is generating false-positive / spurious errors in some circumstances.

As per this SO answer, I am changing to use the result of the method to determine success/failure. And then (and only then) check the error pointer for details of the failure.

NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
/* Return Value
   The downloaded data for the URL request. Returns nil if a connection could not be created or if the download fails.
*/
if (response == nil) {
    // Check for problems
    if (requestError != nil) {
        ...
    }
}
else {
    // Data was received.. continue processing
}

This approach will avoid reacting to an error that occurred in the downloading process that did not cause it to fail. I think it's possible that as the downloading occurs it may encounter non-critical errors that get handled within the framework, and they have the side effect of setting the error pointer with an error object.

For example, I have been getting POSIX errors reported from users that have downloaded the app that seem to have occurred deep in the processing of the URL connection.

This code is used to report the error:

NSString *errorIdentifier = [NSString stringWithFormat:@"(%@)[%d]",requestError.domain,requestError.code];
[FlurryAPI logError:errorIdentifier message:[requestError localizedDescription] exception:nil];

And the error appears reported as:

Platform: iPhone
Error ID: (NSPOSIXErrorDomain)[22]
Msg: Operation could not be completed. Invalid argument

Mapping that back..

requestError.domain = NSPOSIXErrorDomain
requestError.code = 22
[requestError localizedDescription] = Operation could not be completed. Invalid argument

All I've been able to dig up, is that error code 22 is EINVAL, but that hasn't yielded any further details.

Other errors I get are from the NSURL domain, which I totally expect under diverse network conditions:

NSURLErrorTimedOut
NSURLErrorCannotConnectToHost
NSURLErrorNetworkConnectionLost
NSURLErrorNotConnectedToInternet
+others

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...