iOS6中的cachedResponseForRequest块和sendSynchronousRequest

3

在Xcode 4.4中不存在这种情况,但是在4.5中,如果从cachedResponseForRequest内部调用sendSynchronousRequest并使用ios6模拟器或真实设备,则调用不会返回直到超时。

同样,使用sendAsynchronousRequest的循环并旋转(每0.05秒检查一次完成),完成循环(经过10秒),但是请求永远不会完成(如果从cachedResponseForRequest中调用)。

在Xcode 4.4中(模拟ios5),这种情况不会发生。有什么想法吗?

1个回答

0

只需使用方法“sendAsynchronousRequest”。 这是我的解决方案,它非常有效。

[NSURLConnection sendAsynchronousRequest:newRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data,NSError *error){
        NSLog(@"--- caching ---");
        if (error) {
            NSLog(@"%@", error);
            NSLog(@"not cached: %@", absoluteString);
        }
        NSString *filename = [self sha1:absoluteString];
        NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        [fileManager createFileAtPath:path contents:data attributes:nil];
        [fileManager release];

        NSURLResponse *newResponse = [[NSURLResponse alloc] initWithURL:response.URL MIMEType:response.MIMEType expectedContentLength:data.length textEncodingName:nil];

        NSDictionary *responseInfo = [NSDictionary dictionaryWithObjectsAndKeys:filename, @"filename", newResponse.MIMEType, @"MIMEType", nil];
        [responsesInfo setObject:responseInfo forKey:absoluteString];
        cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:newResponse data:data];
        [newResponse release];
        [cachedResponses setObject:cachedResponse forKey:absoluteString];
        [cachedResponse release];
    }];

因为它只适用于iOS5.0及更高版本。您可能需要先检查iOS版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接