2010-10-25 50 views
1

我正在使用Core Data + sqlite作爲數據緩存。該應用程序將文件讀​​入核心數據,然後使用Core Data運行該應用程序。保存完成NSManagedObjectContext和文件。但是,我注意到,如果我退出應用程序並重新加載它而沒有重新填充Core Data數據庫,則使用-save保存的一些(但不是全部)數據不會被保存到數據存儲區。保存一個NSManagedObjectContext的實例不會永久保存一個可變的可變屬性

對我的託管對象所做的更改全部在主線程的單個批處理中完成,並且在完成所有更改後發送-save:消息。未保存的數據是可轉換屬性,這是核心數據對象中唯一可轉換的屬性。這裏是保存對象的代碼:

NSInteger columnIndex = [headers indexOfObject:questionID]; 
    if (NSNotFound != columnIndex) { 

        // parsedLine is a mutable array already 
     NSMutableArray *parsedLine = person.dataFromFile.parsedLine; 
     [parsedLine replaceObjectAtIndex:columnIndex withObject:answer]; 
     person.dataFromFile.parsedLine = parsedLine; 

     person.questionsAnsweredByPerson = [NSNumber numberWithInt:[FileParser questionsAnsweredInRow:person.dataFromFile.parsedLine withRowHeaders:headers]]; 

     person.address.street.questionsAnsweredByPeopleOnStreet = [NSNumber numberWithInt:[self questionsAnsweredByPeopleOnStreet]]; 

     //NSLog(@"rawLineBefore:\n%@", person.dataFromFile.rawLine); 
     person.dataFromFile.rawLine = [ReconstructCSV composeCSVLineFromArray:person.dataFromFile.parsedLine]; 
     //NSLog(@"rawLineAfter:\n%@", person.dataFromFile.rawLine); 


     Voter_SurveyAppDelegate *appDelegate = (Voter_SurveyAppDelegate *)[[UIApplication sharedApplication] delegate]; 

     NSError *error; 

     NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext]; 
     if (![managedObjectContext save:&error]) { 
      // XXX inform the user there was a fatal error opening the file. Low disk space? 
      NSLog(@"Unresolved error - could not save managedObjectContext - %@, %@", error, [error userInfo]); 
      abort(); 
     } 
     return YES; 
    } 

abort();沒有被調用,所以我假設 - 保存;被正確調用。我懷疑它是相關的,但在這個代碼在主線程上運行後,我使用一個新的NSManagedObjectContext在另一個線程上執行一個NSFetchRequest。在其他線程上沒有發生與核心數據相關的其他事情。

爲什麼變形屬性不能保存?

回答

1

問題是可變形的屬性不喜歡可變的對象。正如在this question的回答中指出的,NSMutableDictionary沒有得到保存。在我的情況下,它是一個NSMutableArray。