I am currently coding an application that is using an XML document to retrieve data (I am using libxml2.2.7.3). I set it to load a local XML-file (in the project in xCode, so along with all the other project files). I found that I wanted to be able to edit this XML file and then have instant updates on the application. I fetch the data from the XML document like this:
我目前正在编写一个使用XML文档检索数据的应用程序(我正在使用libxml2.2.7.3)。我将它设置为加载本地xml文件(在xCode中的项目中,以及所有其他项目文件)。我发现我希望能够编辑这个XML文件,然后对应用程序进行即时更新。我从这样的XML文档中获取数据:
NSArray *dagensRetList = [self getAllItems:@"//dagensret" fileName:@"dagensret.xml"];
I thought that the easiest way to come around this would be to download the xml-file whenever there is a new version of that document available from the webserver that I provide (on every application launch / clicking the refresh button it would download the new file from the server - maybe let it check whether they have the same tag (weekNumber, it's a Danish coded application))
我认为最简单的方法来这将是下载xml文件每当有新版本的文档可以从我提供的网络服务器(在每一个应用程序启动/单击刷新按钮将从服务器下载新文件——也许让它检查他们是否有相同的标记(weekNumber,这是一个丹麦编码应用程序))
So I am thinking on what would be the most convenient way to download the file and should I keep this way of fetching the data or would it be more wise to keep the document on the server and they would then read directly from the server everytime? (It could however end up using a lot of traffic, but it's a product for my school so the user-base would be around 1200, but less as not everyone is using a smartphone)
所以我在想下载文件最方便的方式是什么,我应该保持这种获取数据的方式,还是更明智的做法是将文档保存在服务器上,然后他们每次都直接从服务器上读取?(不过,它最终可能会消耗大量流量,但这是我学校的产品,所以用户群大约在1200人左右,但不是所有人都在使用智能手机,所以流量会减少。)
How would you download a file from a webserver and then keep it cached?
如何从webserver下载文件并将其缓存?
1 个解决方案
#1
5
You should definitely cache the file on the device in case the user can't connect to the server.
如果用户无法连接到服务器,那么一定要缓存设备上的文件。
Something like this should get you started:
像这样的事情应该让你开始:
// Create a URL Request and set the URL
NSURL *url = [NSURL URLWithString:@"http://google.com"]
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// Display the network activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// Perform the request on a new thread so we don't block the UI
dispatch_queue_t downloadQueue = dispatch_queue_create("Download queue", NULL);
dispatch_async(downloadQueue, ^{
NSError* err = nil;
NSHTTPURLResponse* rsp = nil;
// Perform the request synchronously on this thread
NSData *rspData = [NSURLConnection sendSynchronousRequest:request returningResponse:&rsp error:&err];
// Once a response is received, handle it on the main thread in case we do any UI updates
dispatch_async(dispatch_get_main_queue(), ^{
// Hide the network activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (rspData == nil || (err != nil && [err code] != noErr)) {
// If there was a no data received, or an error...
} else {
// Cache the file in the cache directory
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"init.xml"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
[data writeToFile:path atomically:YES];
// Do whatever else you want with the data...
}
});
});
#1
5
You should definitely cache the file on the device in case the user can't connect to the server.
如果用户无法连接到服务器,那么一定要缓存设备上的文件。
Something like this should get you started:
像这样的事情应该让你开始:
// Create a URL Request and set the URL
NSURL *url = [NSURL URLWithString:@"http://google.com"]
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// Display the network activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// Perform the request on a new thread so we don't block the UI
dispatch_queue_t downloadQueue = dispatch_queue_create("Download queue", NULL);
dispatch_async(downloadQueue, ^{
NSError* err = nil;
NSHTTPURLResponse* rsp = nil;
// Perform the request synchronously on this thread
NSData *rspData = [NSURLConnection sendSynchronousRequest:request returningResponse:&rsp error:&err];
// Once a response is received, handle it on the main thread in case we do any UI updates
dispatch_async(dispatch_get_main_queue(), ^{
// Hide the network activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (rspData == nil || (err != nil && [err code] != noErr)) {
// If there was a no data received, or an error...
} else {
// Cache the file in the cache directory
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"init.xml"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
[data writeToFile:path atomically:YES];
// Do whatever else you want with the data...
}
});
});