我一直在试图弄清楚为什么NSURLConnection没有向我的班级调用它的委托方法。该方法将在99.999%的时间内正常工作,但在奇怪的情况下,将创建NSURLConnection,然后不回调。我已经使用调试信息检查了线程,以确保它在主线程上执行(此函数只能通过按下始终在主线程上的UI元素来调用)。我还检查了那时的连接数,并且已经多次检查过1次因此我无法达到任何形式的连接最大计数。用于此次调用的URL每次都相同,因此我排除了错误的URL,因为它在大多数时间都有效
下面的代码显示了被调用的函数:
- (void) getProperties:(NSString *) userID propertyFlag:(PropertyFlag)propertyFlag delegate:(id) target setState:(id) state
{
if(!userID)
{
NSException *anException = [[NSException alloc] initWithName:NSInvalidArgumentException reason:@"userId is equal to nil" userInfo:nil];
[anException raise];
}
NSDictionary *userState = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:target, state, self, FUNCTION_NAME, nil]
forKeys:[NSArray arrayWithObjects:@"callback", @"state", @"_self", @"request", nil]];
// Create the connection state handler and set the delegates and type
ConnectionStateHandler *connectionStateHandler = [[ConnectionStateHandler alloc] init];
connectionStateHandler.delegate = self;
connectionStateHandler.state = userState;
// create the target URL
NSString *targetURL = [NSString stringWithFormat:@"someurl"];
NSURL *url = [NSURL URLWithString:targetURL];
DLog(@"getProperties:propertyFlag:delegate:setState - url: %@", targetURL);
DLog(@"Is%@ main thread", ([NSThread isMainThread] ? @"" : @" NOT"));
// create the request
NSMutableURLRequest *request = [PSMNSURLRequestBuilder createRequestWithURL:url];
connectionStateHandler.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:connectionStateHandler] autorelease];
}
我已经实现了所有必需的委托方法,如前所述,它通常正常工作。如果有人能给我任何关于除了线程,连接数和错误URL以外的其他问题的任何想法,我会非常感谢它!
提前感谢任何评论者
编辑:
我在ConnectionStateHandler对象中实现了以下委托方法(每个方法都有登录以显示它们何时被调用,但是它们在极少数情况下没有被调用):
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (NSURLRequest *)connection: (NSURLConnection *)inConnection willSendRequest:(NSURLRequest *)inRequest redirectResponse: (NSURLResponse *)inRedirectResponse;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection