我正在尝试了解 iOS Core 数据瞬态属性,但在理解某些行为时遇到了麻烦。
设置
我有两个上下文,一个主上下文和一个私有上下文。我称它们为 mainContext 和 threadedContext 。
线程上下文是父上下文,主上下文是子上下文。(我这样做是因为我的线程上下文比我的主线程和 UI 更频繁地更改模型。
我有需要通过上下文传递的值的瞬态属性。
我发现有时我失去了价值,有时我不依赖于我如何运行。
样本
此代码已被简化以显示问题。我有一个 Person 对象。Person 对象有一个名为“other”的瞬态实体,您将看到我为其分配了一个 Other 对象,该对象具有几个简单的属性,仅此而已。
- (void)case1
{
NSManagedObjectContext *mainThreadContext = [AppDelegate appDelegate].mainThreadContext;
NSManagedObjectContext *threadedContext = [AppDelegate appDelegate].threadedContext;
__block NSManagedObjectID *objectID = nil;
[mainThreadContext performBlockAndWait:^{
//create
Person *aPerson = [self createAPersonOnContext:mainThreadContext];
//setup
Other *other = [[Other alloc] init];
aPerson.other = other;
aPerson.other.favoriteColor = @"Blue";
aPerson.other.city = @"Provo";
//save
NSError *error = nil;
[mainThreadContext save:&error];
objectID = aPerson.objectID;
NSLog(@"%@",aPerson);
}];
}
Run Code Online (Sandbox Code Playgroud)
当我像这样检索对象时,仍然设置了 person.other 属性(请注意,我在检索对象后正在保存: …