混合新旧数据时应用程序崩溃

发布于 2024-10-21 20:31:38 字数 2614 浏览 7 评论 0原文

我提出了一个与此有点相似的问题,尽管旧问题实际上已经解决了,但我发现了一个与此有点相关的新问题。无论如何,我选择制作单独的主题。

我有一个应用程序可以从我的网络服务器上的属性列表中读取数据。我希望该应用程序在离线时可用,因此我将属性列表作为 Bands.plist 复制到 iPhone 上,并从本地 plist 读取数据。

我的问题是,如果数据已更新并且旧版本的 Bands.plist 存储在设备上,那么我的应用程序在更新时开始从 Bands.plist 读取,并且数据变成新旧数据的混合,这会导致应用程序崩溃。我不知道如何解决这个问题,希望有人有想法。

我有一张图片说明了我的问题:

On the left is the old data and on the right is the new data

发生了什么是,我的视图有一个带有页面控制器的子视图。每个页面都包含一个表视图,数据将加载到其中。

在这里你可以看到我从一个视图滑到另一个视图。第一个视图(左侧)是旧数据,第二个视图(右侧)是新数据。在这种视图转换中,应用程序崩溃了。

谁能帮我解决这个问题吗?似乎应用程序在保存数据之前就开始加载数据。

这是一些代码。请说更多代码是否有帮助。我不知道如何解决这个问题。

这是来自应用程序委托。这里从网络服务器检索数据并将其保存到 Bands.plist。

NSLog(@"[AppDelegate] Data has been downloaded.");

    // 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"];

    NSLog(@"[AppDelegate] Bands.plist has been populated.");

这是从表视图来看的。这里的数据是从 Bands.plist 加载的。

    pageNumber = page;

            NSLog(@"[TableView] Loading view.");

            NSArray *whatDays = [[NSArray alloc] initWithObjects:@"Onsdag", @"Torsdag", @"Fredag", @"Lørdag", @"Ikke programsat", nil];

            sections = [[NSMutableDictionary alloc] init];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Bands.plist"];
            tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

            // scan through data and sort
            for(int i = 0; i < [whatDays count]; i++) {
                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.weekDay == %@", [whatDays objectAtIndex:i]];
                NSArray *bandsThisDay = [tmpArray filteredArrayUsingPredicate:predicate];
                [sections setObject:bandsThisDay forKey:[whatDays objectAtIndex:i]];
            }

            [whatDays release];

是否可以从应用程序委托调用 tableview 实现中的方法并告诉它填充表?那么它正在等待下载完成吗?

I have made a question which is a bit similar to this, though the old one actually got solved but I found a new issue which relates a bit. Anyways, I chose to make seperate topics.

I have an application which reads data from a property list on my webserver. I want the application to be usable when offline therefore I copy the property list onto the iPhone as Bands.plist and I read the data from the local plist.

My problem is, that if the data has been updated and an old version of Bands.plist is stored on the device, then my application starts reading from Bands.plist while updating and the data becomes a mix of old and new data which causes the application to crash. I can't figure out how to fix this issue and hope that someone has an idea.

I have an image which illustrates my issue:

On the left is the old data and on the right is the new data

What happens is, that my view has a subview which has a page controller. Each page contains a table view where the data is loaded into.

Here you see that I am sliding from one view to another. In the first view (the left) is the old data and in the second view (the right) is the new data. In this transition of views, the application crashes.

Can anyone help me figure this out? It seems that the application starts loading the data before it is saved.

Here is some code. Please say, if more code will help. I can't figure out how to fix this issue.

This is from the App Delegate. Here the data is retrieved from the webserver and is saved to Bands.plist.

NSLog(@"[AppDelegate] Data has been downloaded.");

    // 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"];

    NSLog(@"[AppDelegate] Bands.plist has been populated.");

This is from the table view. Here the data is loaded from Bands.plist.

    pageNumber = page;

            NSLog(@"[TableView] Loading view.");

            NSArray *whatDays = [[NSArray alloc] initWithObjects:@"Onsdag", @"Torsdag", @"Fredag", @"Lørdag", @"Ikke programsat", nil];

            sections = [[NSMutableDictionary alloc] init];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Bands.plist"];
            tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

            // scan through data and sort
            for(int i = 0; i < [whatDays count]; i++) {
                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.weekDay == %@", [whatDays objectAtIndex:i]];
                NSArray *bandsThisDay = [tmpArray filteredArrayUsingPredicate:predicate];
                [sections setObject:bandsThisDay forKey:[whatDays objectAtIndex:i]];
            }

            [whatDays release];

Would it be possible to call a method in tableview implementation from the app delegate and tell it to populate the table? So it's waiting for the download to be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一城柳絮吹成雪 2024-10-28 20:31:38

我通过向 NSNotificationCenter 发送通知并向表视图添加一个观察者来解决此问题,该观察者将等待通知,然后在表上调用 reloadData

I solved this by sending a notification to NSNotificationCenter and add an observer to the table view which will wait for the notification and then call reloadData on the table.

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