NSURLConnection返回旧数据,尽管NSURLRequestReloadIgnoringLocalAndRemoteCacheData

时间:2012-10-07 20:48:00

标签: objective-c ios xcode nsurlconnection

我遇到了严重的问题。在我的应用程序中,我使用NSURLConnection下载XML文件。到现在为止还挺好。但有时,虽然我将cachePolicy设置为NSURLRequestReloadIgnoringLocalAndRemoteCacheData,但NSURLConnection会返回所请求文件的旧版本。 这个错误的真正恶心的部分是,连接总是返回文件的旧版本,直到我激活,然后停用飞行模式或我重新启动设备。

另一个有趣的事实是:当我激活Wifi并调用应用程序重新加载文件时,将返回该文件的正确和实际版本。但在关闭Wifi之后,旧版本再次被重新调整。

我真的对这个问题感到绝望,希望有人可以帮助我吗?

以下是我班级的代码,下载文件:

#import "HTTPReciever.h"
#import "Constants.h"

@interface HTTPReciever ()
@property (strong, nonatomic) NSMutableData *receivedData;
@property (strong, nonatomic) NSString *directoryPath;
@property (strong, nonatomic) NSString *filename;
@property (assign, nonatomic) BOOL storeData;
@end

@implementation HTTPReciever
@synthesize receivedData = _receivedData;
@synthesize delegate = _delegate;
@synthesize directoryPath = _directoryPath;
@synthesize filename = _filename;


- (BOOL)recieveDataFromURL:(NSURL *)url
      storeAtDirectoryPath:(NSString *)directoryPath
                  withName:(NSString *)name
{
    [self setDirectoryPath:directoryPath];
    [self setFilename:name];
    [self setStoreData:YES];
    return [self recieveDataFromURL:url];
}

- (BOOL)recieveDataFromURL:(NSURL *)url
{
    NSURLRequest *theRequest = [[NSURLRequest alloc] initWithURL:url
                                                     cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                 timeoutInterval:cDefaultHTTPTimeOutInterval];

    self.receivedData = nil;
    self.receivedData = [[NSMutableData alloc] initWithLength:0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest
                                                                  delegate:self
                                                          startImmediately:NO];
    if ([NSURLConnection canHandleRequest:theRequest] && connection) {
        [connection start];
        return YES;
    } else {
        return NO;
    }
}



#pragma mark - NSURLConnectionDelegate protocol
- (void)connection:(NSURLConnection *)connection
     didReceiveData:(NSData *)data
{
    [self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self.receivedData setLength:0];
    if ([response isKindOfClass:[NSHTTPURLResponse self]]) {
        NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
        NSString *modified = [headers objectForKey:@"Last-Modified"];
        if (modified) {
            // not yet used
        }
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if (self.storeData) {
        [[NSFileManager defaultManager] createDirectoryAtPath:self.directoryPath
                                  withIntermediateDirectories:YES
                                                   attributes:nil
                                                        error:nil];
        [self.receivedData writeToFile:[NSString stringWithFormat:@"%@%@", self.directoryPath, self.filename]
                            atomically:YES];
    }

    [self.delegate receiver:self
       didFinishLoadingData:self.receivedData
                lastVersion:YES];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    [self.delegate receiverFailedLoadingData:self];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}


@end

0 个答案:

没有答案