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