从未保存的 plist 中读取
我正在开发一个应用程序,它从位于我的网络服务器上的 plist 检索其数据。因此,该应用程序依赖于网络连接。我希望用户能够在离线时使用我的应用程序,因此每次通过网络连接加载应用程序时,我都会在用户设备上保存 plist 的副本。从那里,我从设备上的 plist 读取数据。
不过,我遇到了麻烦。我开始在 AppDelegate 的 didFinishLaunchingWithOptions
方法中下载数据。这样做是这样的:
if(hasConnection) { // returns TRUE or FALSE based on Apple's example on checking network reachability
NSLog(@"Starting download of data.");
// loading using NSURLConnection
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:FETCH_URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
}
}
然后我将数据添加到 connectionDidFinishLoading
中的 plist Bands.plist
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// throw data into a property list (plist)
NSMutableArray *tmpArray = [NSPropertyListSerialization propertyListFromData:receivedData mutabilityOption:NSPropertyListMutableContainers format:nil errorDescription:nil];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithArray:tmpArray];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Bands.plist"];
[plistArray writeToFile:path atomically:YES];
[plistArray release];
// release the connection, and the data object
[connection release];
[receivedData release];
}
但是当第一次加载应用程序时,它崩溃了。我相信这是由于应用程序试图访问该数据,即使它尚未保存。如果我删除尝试访问本地保存的数据的部分,则没有问题。如果我再次添加它并重新启动应用程序(第二次加载),我也没有问题。
有谁知道如何解决这个问题?
就好像我的应用程序尝试加载和处理尚不存在的数据。
I am developing an application which retrieves its data from a plist located on my webserver. Therefore the application is dependent on a network connection. I want the user to be able to use my application when offline and therefore I am saving a copy of the plist on the users device every time the application is loaded with network connection. From there, I read the data from the plist located on the device.
I am having troubles, though. I am starting the download of the data in didFinishLaunchingWithOptions
method of the AppDelegate. This is done like this:
if(hasConnection) { // returns TRUE or FALSE based on Apple's example on checking network reachability
NSLog(@"Starting download of data.");
// loading using NSURLConnection
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:FETCH_URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
}
}
Then I add the data to my plist Bands.plist
in connectionDidFinishLoading
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// throw data into a property list (plist)
NSMutableArray *tmpArray = [NSPropertyListSerialization propertyListFromData:receivedData mutabilityOption:NSPropertyListMutableContainers format:nil errorDescription:nil];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithArray:tmpArray];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Bands.plist"];
[plistArray writeToFile:path atomically:YES];
[plistArray release];
// release the connection, and the data object
[connection release];
[receivedData release];
}
But when loading the application for the first time, it crashes. I believe this is due to the application trying to reach this data even though it is not yet saved. If I remove the part that tries to reach the data saved locally, I have no problem. Neither do I have a problem if I add it again and restart the application (second load).
Does anyone have an idea how to fix this issue?
It is as if my application tries to load and handle data which does not yet exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需先尝试检查 plist 是否存在:
干杯,
萨沙
Simply try checking for the existence of the plist first:
Cheers,
Sascha