繁体   English   中英

未调用iOS dispatch_async和NSURLConnection委托函数

[英]iOS dispatch_async and NSURLConnection delegate functions not being called

我认为,我已经编辑了这篇文章,使其更易于阅读。

在dispatch_async块中完成一些密集的字符串操作后,我需要调用NSUrlConnection。

我调用的URL上有.htaccess身份验证,所以我不能使用同步连接。

但是没有调用NSURLConnection委托方法。 我知道在浏览器中大约5秒钟后加载了URL,并且我使用一个没有身份验证的简单URL测试了代码,这没有任何区别。

什么阻止调用委托方法?

这个函数做了一些字符串操作的东西,需要一段时间才能完成:

- (void)performSearch {

    // set some defaults and work out if it is a valid search, setting bSearchOk

    if (bSearchOk) {

        // update some interface elements
        self.msgBox.text = @"Translating...";            
        self.plateInput.enabled = NO;
        self.searchProgress.hidden = NO;

        dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            // create a new search
            Search *oPlateSearch = [[Search alloc] initWithTerm:thisTerm];

            // ...
            // perform search... this calls a variety of slow and intensive functions
            // ...

            self.aFinalPlates = [oPlateSearch.aValidated copy];
            self.aCurrentPlates = [oPlateSearch.aFinals copy];        

            dispatch_async( dispatch_get_main_queue(), ^{                 
                [oPlateSearch release];              

                // make ajax call to check if plates can be bought
                [self getPrices];                 
            });
        });

    } else {
        // hide results
        self.searchResults.hidden = YES;
    }

}

这就是上面阻止结束的那个

/* call to get plate availability and prices */
-(void) getPrices {
    // set return message
    self.msgBox.text = @"Getting prices and availability";

    // ...
    // then I build my strRequest var, which is a valid working URL
    // ...

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strRequest]];

    NSURLConnection *loginConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

    NSLog('This will get logged');

    if(loginConnection) {
        NSLog('This will also get logged');
        self.jsonSearchResponse = [[[NSMutableData data] retain] autorelease];
        NSLog('And this will get logged, so it's not throwing errors');
    } else {
        NSLog(@"Failed to get search results");
    }
}

更新:我也试过这个,因为它是另一个类似问题的答案,但它没有奏效:

NSURLConnection *loginConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[loginConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[loginConnection start];

基于这个问题: 为什么在使用全局调度队列时不会调用NSURLConnection委托方法?

我已经开始了一个SO帖子,它展示了如何使用NSURLConnectiondispatch_async并配置NSRunLoop

使用信号量实现的NSURLConnection阻塞包装器

您感兴趣的代码片段是:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [NSURLConnection connectionWithRequest:request delegate:self];

    while(!self.finished) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
});

并在connectionDidFinishLoading ,将finished设置为YES:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    self.finish = YES;
}

来自iphonedevsdk.com论坛的某人为我解答了这个问题:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/97415-dispatch_async-nsurlconnection.html

答案是使用:

 [self performSelectorOnMainThread:@selector(getPrices) withObject:Nil waitUntilDone:NO];

我认为你是自动释放连接而不保留对它的任何引用,因此它会耗尽内存。 删除自动释放呼叫,看看问题是否消失。 如果是这样,只需找到一种方法来释放连接,这样它就不会泄漏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM