2011-03-10 88 views
0

我已經提出了一個類似於這個問題的問題,雖然舊問題實際上已經解決了,但是我發現了一個涉及了一點的新問題。無論如何,我選擇了分開的話題。當新舊數據混淆時應用程序崩潰

我有一個應用程序從我的web服務器上的屬性列表讀取數據。我希望應用程序在離線時可用,因此我將屬性列表作爲Bands.plist複製到iPhone上,然後從本地plist讀取數據。

我的問題是,如果數據已更新並且舊版本的Bands.plist存儲在設備上,則我的應用程序在更新時開始從Bands.plist中讀取數據,並且數據變成新舊混合導致應用程序崩潰的數據。我無法弄清楚如何解決這個問題,並希望有人有一個想法。

我有我的說明問題的圖像:

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

會發生什麼事是,我的觀點有它有一個頁面控制器子視圖。每個頁面都包含數據加載到的表格視圖。

在這裏,您看到我從一個視圖滑動到另一個視圖。在第一個視圖(左側)是舊數據,在第二個視圖(右側)是新數據。在這個視圖的轉換中,應用程序崩潰。

任何人都可以幫我解決這個問題嗎?看起來應用程序在保存之前開始加載數據。

這是一些代碼。請說,如果更多的代碼將有所幫助。我無法弄清楚如何解決這個問題。

這是來自App Delegate。這裏的數據從網絡服務器中檢索並保存到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]; 

是否有可能從應用程序委託調用表視圖實現中的方法,並告訴它填充表?所以它正在等待下載完成?

回答

0

我通過發送通知到NSNotificationCenter並添加一個觀察者到表視圖,它將等待通知,然後在表上呼叫reloadData解決此問題。

相關問題