从未保存的 plist 中读取

发布于 2024-10-21 17:16:55 字数 2161 浏览 6 评论 0原文

我正在开发一个应用程序,它从位于我的网络服务器上的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-10-28 17:16:55

只需先尝试检查 plist 是否存在:

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

干杯,
萨沙

Simply try checking for the existence of the plist first:

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

Cheers,
Sascha

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文