简体   繁体   English

从未保存的列表中读取

[英]Reading from an unsaved plist

I am developing an application which retrieves its data from a plist located on my webserver. 我正在开发一个应用程序,该应用程序从位于我的Web服务器上的plist检索其数据。 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. 我希望用户能够在脱机时使用我的应用程序,因此,每次使用网络连接加载应用程序时,我都会在用户设备上保存plist的副本。 From there, I read the data from the plist located on the device. 从那里,我从设备上的plist读取数据。

I am having troubles, though. 不过,我遇到了麻烦。 I am starting the download of the data in didFinishLaunchingWithOptions method of the AppDelegate. 我正在didFinishLaunchingWithOptions方法中开始下载数据。 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 然后,我将数据添加到我的plist Bands.plistconnectionDidFinishLoading

- (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. 就像我的应用程序试图加载和处理尚不存在的数据一样。

Simply try checking for the existence of the plist first: 只需尝试首先检查plist是否存在:

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
   ...  // read the data, etc.
}
else {
   ...  // don't try to read it
}

Cheers, 干杯,
Sascha 萨沙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM