我使用 Core Data + sqlite 作为数据缓存。应用程序将文件读入核心数据,然后使用核心数据运行应用程序。保存到 NSManagedObjectContext 和文件。不过,我注意到,如果我退出应用程序并重新加载它而不重新填充 Core Data 数据库,则使用 -save: 保存的一些(但不是全部)数据不会保存到数据存储中。
对我的托管对象的更改都是在主线程上的一个批次中完成的,在所有更改完成后发送 -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;
}
中止();没有被调用,所以我假设 -save; 被正确调用。
我怀疑它是否相关,但是在主线程上运行此代码后,我在不同的线程上使用新的 NSManagedObjectContext 执行 NSFetchRequest。在其他线程上没有发生与 Core Data 相关的其他事情。
为什么不保存可转换属性?