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