mik*_*uhl 3 iphone objective-c libxml2 ios
我目前正在编写一个使用XML文档来检索数据的应用程序(我正在使用libxml2.2.7.3).我将其设置为加载本地XML文件(在xCode中的项目中,以及所有其他项目文件).我发现我希望能够编辑此XML文件,然后在应用程序上进行即时更新.我从XML文档中获取数据,如下所示:
NSArray *dagensRetList = [self getAllItems:@"//dagensret" fileName:@"dagensret.xml"];
Run Code Online (Sandbox Code Playgroud)
我认为解决这个问题的最简单方法是在我提供的网络服务器上提供该文档的新版本时下载xml文件(在每个应用程序启动/单击刷新按钮时它将下载新文件从服务器 - 也许让它检查他们是否有相同的标签(weekNumber,它是丹麦编码的应用程序))
所以我在考虑下载文件最方便的方法,我应该采用这种方式获取数据,还是将文档保存在服务器上更为明智,然后每次都直接从服务器读取?(但它最终可能会占用大量流量,但它是我学校的产品,因此用户群大约为1200,但不是每个人都使用智能手机)
您如何从网络服务器下载文件然后保持缓存?
如果用户无法连接到服务器,您肯定应该将文件缓存在设备上.
这样的事情应该让你开始:
// 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...
}
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2967 次 |
| 最近记录: |