首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLConnection定时

NSURLConnection定时
EN

Stack Overflow用户
提问于 2012-05-31 21:38:33
回答 1查看 2.5K关注 0票数 5

在我们的NSURLConnection应用程序中,我一直有间歇性的NSURLConnection请求超时问题。它似乎发生得更晚了。一旦它进入这种状态,它就会保持这种状态。唯一的解决方案似乎是杀死该应用程序并重新启动它。

意见:

  • 执行NSURLConnection的核心代码没有更改(除了最近添加的一些自定义用户代理代码)。
  • 还没有找到一个可重复的情况,但在应用程序在后台放置了一段时间之后,似乎就会出现超时,特别是在运行3G (没有WiFi)的情况下。服务器上的
  • Apache在经历这些超时时不会记录来自客户端的请求。一些迹象表明,其他应用程序(比如邮件和Safari )会受到影响(例如,经历超时),但在我所在的地方,consistently.
  • 3G的覆盖范围并不牢固,不能排除引发问题的暂时性问题(假设不太可能)。
  • 所有请求都会转到我们自己的API服务器,并且是restful POST请求。由于timeoutInterval和POST请求的问题,
  • 我们使用了自己的NSTimer超时。我试着增加超时值--仍然会出现问题。

其他杂项物品:

  • 应用程序最近被转换为ARC .
  • 在iOS 5.1.1下运行应用。
  • 应用程序使用最新版本的UrbanAirship、TestFlight和Flurry。
  • 还使用TouchXML的ARC分支来解析响应。

F 225

如下所示,代码运行在主线程上。我以为那个线程上有什么东西阻塞了,但是挂起应用程序时看到的堆栈跟踪表明主线程没有问题。我认为NSURLConnection正在使用它自己的线程,必须阻止它。

代码语言:javascript
复制
#define relnil(v) (v = nil)

- (id) initWebRequestController
{
    self = [super init];
    if (self)
    {
        //setup a queue to execute all web requests on synchronously
        dispatch_queue_t aQueue = dispatch_queue_create("com.myapp.webqueue", NULL);
        [self setWebQueue:aQueue];
    }
    return self;
}

- (void) getStuffFromServer
{
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^{
        dispatch_sync([self webQueue], ^{            
            error_block_t errorBlock = ^(MyAppAPIStatusCode code, NSError * error){
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[self delegate] webRequestController:self didEncounterErrorGettingPointsWithCode:code andOptionalError:error];
                });
            };

            parsing_block_t parsingBlock = ^(CXMLDocument * doc, error_block_t errorHandler){
                NSError * error = nil;

                CXMLNode * node = [doc nodeForXPath:@"apiResult/data/stuff" error:&error];
                if (error || !node) {
                    errorHandler(MyAppAPIStatusCodeFailedToParse, error);
                }
                else {
                    stuffString = [node stringValue];
                }

                if (stuffString) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[self delegate] webRequestController:self didFinishGettingStuff:stuffString];
                    });
                }
                else {
                    errorHandler(MyAppAPIStatusCodeFailedToParse, error);
                }
            };

            NSURL * url = [[NSURL alloc] initWithString:[NSString stringWithFormat:MyAppURLFormat_MyAppAPI, @"stuff/getStuff"]];

            NSMutableURLRequest * urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
            NSMutableDictionary * postDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                    [[NSUserDefaults standardUserDefaults] objectForKey:MyAppKey_Token], @"token",
                                                    origin, @"from",
                                                    destination, @"to",
                                                    transitTypeString, @"mode",
                                                    time, @"time",
                                                    nil];

            NSString * postString = [WebRequestController httpBodyFromDictionary:postDictionary];
            [urlRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
            [urlRequest setHTTPMethod:@"POST"];

            if (urlRequest)
            {
                [self performAPIRequest:urlRequest withRequestParameters:postDictionary parsing:parsingBlock errorHandling:errorBlock timeout:kTimeout_Standard];
            }
            else
            {
                errorBlock(MyAppAPIStatusCodeInvalidRequest, nil);
            }

            relnil(url);
            relnil(urlRequest);
        });
    });
}

- (void) performAPIRequest: (NSMutableURLRequest *) request
     withRequestParameters: (NSMutableDictionary *) requestParameters
                   parsing: (parsing_block_t) parsingBlock 
             errorHandling: (error_block_t) errorBlock
                   timeout: (NSTimeInterval) timeout
{
    NSAssert([self apiConnection] == nil, @"Requesting before previous request has completed");

    NSString * postString = [WebRequestController httpBodyFromDictionary:requestParameters];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    NSString * erVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString * erBuildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    if ([erBuildVersion isEqualToString:erVersion] || [erBuildVersion isEqualToString:@""]) {
        erBuildVersion = @"";
    } else {
        erBuildVersion = [NSString stringWithFormat:@"(%@)", erBuildVersion];
    }
    NSString * iosVersion = [[UIDevice currentDevice] systemVersion];
    NSString * userAgent = [NSString stringWithFormat:@"MyApp/%@%@ iOS/%@", erVersion, erBuildVersion, iosVersion];
    [request setValue:userAgent forHTTPHeaderField:@"User-Agent"];

    [request setTimeoutInterval:(timeout-3.0f)];

    dispatch_sync(dispatch_get_main_queue(), ^{
        NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

        if (urlConnection)
        {
            [self setApiConnection:urlConnection];

            requestParseBlock = [parsingBlock copy];
            requestErrorBlock = [errorBlock copy];

            NSMutableData * aMutableData = [[NSMutableData alloc] init];
            [self setReceivedData:aMutableData];
            relnil(aMutableData);

            [urlConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

            [urlConnection start];
            relnil(urlConnection);

            NSTimer * aTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(timeoutTimerFired:) userInfo:nil repeats:NO];
            [self setTimeoutTimer:aTimer];
        }
        else
        {
            errorBlock(MyAppAPIStatusCodeInvalidRequest, nil);
        }
    });

    //we want the web requests to appear synchronous from outside of this interface
    while ([self apiConnection] != nil)
    {
        [NSThread sleepForTimeInterval:.25];
    }
}

- (void) timeoutTimerFired: (NSTimer *) timer
{
    [[self apiConnection] cancel];

    relnil(apiConnection);
    relnil(receivedData);

    [self requestErrorBlock](MyAppAPIStatusCodeTimeout, nil);

    requestErrorBlock = nil;
    requestParseBlock = nil;
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{    
    [self requestErrorBlock](MyAppAPIStatusCodeFailedToConnect, error);

    relnil(apiConnection);
    relnil(receivedData);
    [[self timeoutTimer] invalidate];
    relnil(timeoutTimer);
    requestErrorBlock = nil;
    requestParseBlock = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{    
    MyAppAPIStatusCode status = MyAppAPIStatusCodeFailedToParse;

    CXMLDocument *doc = [[self receivedData] length] ? [[CXMLDocument alloc] initWithData:[self receivedData] options:0 error:nil] : nil;

    DLog(@"response:\n%@", doc);

    if (doc)
    {
        NSError * error = nil;
        CXMLNode * node = [doc nodeForXPath:@"apiResult/result" error:&error];
        if (!error && node)
        {
            status = [[node stringValue] intValue];

            if (status == MyAppAPIStatusCodeOK)
            {
                [self requestParseBlock](doc, [self requestErrorBlock]);
            }
            else if (status == MyAppAPIStatusCodeTokenMissingInvalidOrExpired)
            {
                [Definitions setToken:nil];

                [self requestMyAppTokenIfNotPresent];

                [Definitions logout];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [[self delegate] webRequestControllerDidRecivedExpiredTokenError:self];
                });
            }
            else
            {
                [self requestErrorBlock](status, nil);                
            }
        }
        else
        {
            [self requestErrorBlock](status, nil);
        }
    }
    else
    {
        status = MyAppAPIStatusCodeUnexpectedResponse;
        [self requestErrorBlock](status, nil);
    }
    relnil(doc);

    relnil(apiConnection);
    relnil(receivedData);
    [[self timeoutTimer] invalidate];
    relnil(timeoutTimer);
    requestErrorBlock = nil;
    requestParseBlock = nil;
}

下面的URL是应用程序处于问题状态时队列/线程的一些屏幕快照。注意,我相信线程10与上一次超时时执行的取消有关,尽管互斥等待很好奇。此外,在22线程中,在遇到其他情况下出现问题时,Flurry的位也不一定会出现。

堆栈跟踪屏幕截图:

http://img27.imageshack.us/img27/5614/screenshot20120529at236.png http://img198.imageshack.us/img198/5614/screenshot20120529at236.png

也许我忽略了这些痕迹中的一些明显错误,因为我对iOS/Apple的开发还比较陌生。

如果我有NSURLConnection和相关代码的源代码,所有这一切都要简单得多,但是,尽管如此,我在这一点上还是在暗地里捅人。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-03 20:08:28

删除TestFlight 1.0SDK似乎解决了这个问题。TestFlight还证实,他们正在进行修复工作。考虑到这个bug最初被其他人确认已经有一个多月了,我想知道我们离修复有多近了。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10841641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档