好吧,您只需在自己的示例程序中尝试一下,正确设置的时间不会超过一个小时。
我的猜测是 --- 不需要额外的编码。如果 Apple 关于 CoreData 的文档是正确的,那么普通属性/关系和“瞬态”之间的唯一区别是后者不会持久化,这意味着当您“保存”时它不会更新持久性存储。
我猜想否则它的所有方面都是完整的,包括 KVO/KVC 合规性、撤消支持、验证和通过删除规则自动更新。唯一的问题是,在重新获取实体后 --- 瞬态关系将始终为零。
为此 --- 我当然不建议将临时关系设置为“非可选”,因为对于大多数实体而言,大多数时间它很可能为空。
我会建立一个反向关系(也是暂时的,并且明智地命名)并且让两个删除规则都是“Nullify”。
到目前为止是暂时的关系。
但是我想出了一个替代方案,试图解决几乎相同的问题。我的“约会”是相关的约会之一,但不仅仅是“最新的”,而是第一个“未完成”的约会。非常相似的逻辑。
我添加了一个新的计算属性到我的“医生”实体生成的 NSManagedObject 子类中,而不是暂时的关系,如下所示:
@interface XXDoctor (XXExtensions)
/**
@brief Needs manual KVO triggering as it is dependent on a collection.
Alternatively, you can observe insertions and deletions of the appointments, and trigger KVO on this propertyOtherwise it can be auto-
@return the latest of the to-many appointments relation.
**/
@property (readonly) XXAppointment *latestAppointment; // defined as the
@end
实施:
#import "XXDoctor".h"
#import "XXAppointment.h"
@implementation XXDoctor (XXExtensions)
// this won't work because "appointments" is a to-many relation.
//+ (NSSet *)keyPathsForValuesAffectingLatestAppointment {
// return [NSSet setWithObjects:@"appointments", nil];
//}
- (XXAppointment *) latestAppointment {
NSInteger latestAppointmentIndex = [self.appointments indexOfObjectPassingTest:^BOOL(XXAppointment *appointment, NSUInteger idx, BOOL *stop) {
*stop = (appointment.dateFinished == nil);
return *stop;
}];
return (latestAppointmentIndex == NSNotFound) ? nil : [self.appointments objectAtIndex: latestAppointmentIndex];
}
@end