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

Iphone 混合新旧数据时应用程序崩溃,iphone,sdk,load,save,plist,Iphone,Sdk,Load,Save,Plist,我提出了一个有点类似的问题,虽然旧的问题实际上已经解决了,但我发现了一个新的问题,它有点相关。无论如何,我选择了单独的主题 我有一个从Web服务器上的属性列表读取数据的应用程序。我希望应用程序在脱机时可用,因此我将属性列表作为Bands.plist复制到iPhone上,并从本地plist读取数据 我的问题是,如果数据已更新,并且设备上存储了旧版本的Bands.plist,则我的应用程序在更新时会开始从Bands.plist读取数据,并且数据会成为新旧数据的混合体,从而导致应用程序崩溃。我不知道如

我提出了一个有点类似的问题,虽然旧的问题实际上已经解决了,但我发现了一个新的问题,它有点相关。无论如何,我选择了单独的主题

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

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

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

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

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

有人能帮我弄清楚吗?应用程序似乎在保存数据之前就开始加载数据

下面是一些代码。请说,如果更多的代码将有所帮助。我不知道如何解决这个问题

这是来自应用程序代理的。在这里,从Web服务器检索数据并保存到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.");
    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];
这是来自表视图的。此处数据从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.");
    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];
pageNumber=page;
NSLog(@“[TableView]加载视图”);
NSArray*whatDays=[[NSArray alloc]initWithObjects:@“Onsdag”、@“Torsdag”、@“Fredag”、@“Lørdag”、@“ike programsat”,nil];
sections=[[NSMutableDictionary alloc]init];
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*path=[DocumentsDirectoryStringByAppendingPathComponent:@“Bands.plist”];
tmpArray=[[NSMutableArray alloc]initWithContentsOfFile:path];
//扫描数据并排序
对于(int i=0;i<[whatDays count];i++){
NSPredicate*谓词=[NSPredicate谓词格式:@“SELF.weekDay==%@,[whatDays objectAtIndex:i]];
NSArray*bandsThisDay=[tmpArray filteredarrayingpredicate:predicate];
[sections setObject:bandsThisDay forKey:[whatDays objectAtIndex:i]];
}
[发布日期];

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

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