1

我想更新在线数据库。我意识到当我的网络连接到 Wi-Fi 时,按下刷新按钮时数据库会更新,但是当我的网络连接到 3g 时,数据库可能会或可能不会更新。如果它得到更新,即使我按下刷新按钮也需要很长时间。我认为缓存有问题,但我不确定如何将缓存放入我的代码中。这是我的代码:

- (void)downloadAtURLString:(NSString *)urlString
{
    NSMutableData *data = [[NSMutableData alloc] init];
    self.activeDownload = data;
    [data release];
    // encode the urlString with percent escapes
    NSString *urlStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [[NSURL alloc] initWithString:urlStr];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.urlConnection = conn;
    [conn release], [request release], [url release];
 }

我尝试使用此代码:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0];

我收到警告.. cachePolicy 是否正确?

4

1 回答 1

0

你试过这个吗?此示例忽略请求的本地缓存。

- (void)downloadAtURLString:(NSString *)urlString
{
    NSMutableData *data = [[NSMutableData alloc] init];
    self.activeDownload = data;
    [data release];
    // encode the urlString with percent escapes
    NSString *urlStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [[NSURL alloc] initWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.urlConnection = conn;
    [conn release], [request release], [url release];
 }

还包括NSURLConnectionDelegate在你的类中并实现委托函数以从 NSURLConnection 获得反馈。

于 2012-09-11T11:46:50.433 回答