数据库未使用3g更新,但可以与Wi-Fi一起使用

时间:2012-09-10 16:36:14

标签: iphone database xcode caching

我想更新在线数据库。我已经意识到当我的网络连接到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正确吗?

1 个答案:

答案 0 :(得分: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并实施delegate functions以获取NSURLConnection的反馈。