核心数据:实现依赖于其他的属性

发布于 2024-12-04 19:13:15 字数 1275 浏览 1 评论 0原文

我的项目中有以下情况(我在其中使用核心数据):我有一个具有两个 BOOL 属性的实体:isCompleted 和 isNonVisit。它还具有第三个属性: NSNumber *status - 该属性的值取决于 isCompleted 和 isNonVisit 值。

当任一 BOOL 属性更改时,我希望状态属性自动实现。

所有三个属性都必须存在于底层数据库中,因为我使用 fetchedResultsController 来利用状态属性(作为排序描述符和sectionNameKeyPath)。

我想出了以下解决方案:

在 .h 文件中:

@property (nonatomic, retain) NSNumber *isCompleted;
@property (nonatomic, retain) NSNumber *isNonVisit;
@property (nonatomic, retain) NSNumber *status;

- (NSNumber *)calculateStatus;  //Returns proper status value based on isCompleted and nonVisit property values.

在 .m 文件中:

@dynamic isCompleted;
@dynamic isNonVisit;
@dynamic status;

- (void)setIsCompleted:(NSNumber *)newValue
{
  [self willChangeValueForKey:@"isCompleted"];
  [self setPrimitiveValue:newValue forKey:@"isCompleted"];
  [self didChangeValueForKey:@"isCompleted"];

  self.status = [self calculateStatus];
}


- (void)setIsNonVisit:(NSNumber *)newValue
{
  [self willChangeValueForKey:@"isNonVisit"];
  [self setPrimitiveValue:newValue forKey:@"isNonVisit"];
  [self didChangeValueForKey:@"isNonVisit"];

  self.status = [self calculateStatus];
}

该解决方案似乎有效。

所以,我的问题是:可以吗?我是否违反了 CoreData 或 KVO 的某些规则?

感谢您的任何建议。

I have following situation in my project (in which I use Core Data): I have an entity which has two BOOL properties: isCompleted and isNonVisit. It also has third property: NSNumber *status - the value of the property depends on both isCompleted and isNonVisit values.

When either of the BOOL property changes, I want status property to be actualised automatically.

All three properties must be present in underlying database, since I use fetchedResultsController that makes use of status property (as sort descriptor and as sectionNameKeyPath).

I came up with following solution:

in .h file:

@property (nonatomic, retain) NSNumber *isCompleted;
@property (nonatomic, retain) NSNumber *isNonVisit;
@property (nonatomic, retain) NSNumber *status;

- (NSNumber *)calculateStatus;  //Returns proper status value based on isCompleted and nonVisit property values.

in .m file:

@dynamic isCompleted;
@dynamic isNonVisit;
@dynamic status;

- (void)setIsCompleted:(NSNumber *)newValue
{
  [self willChangeValueForKey:@"isCompleted"];
  [self setPrimitiveValue:newValue forKey:@"isCompleted"];
  [self didChangeValueForKey:@"isCompleted"];

  self.status = [self calculateStatus];
}


- (void)setIsNonVisit:(NSNumber *)newValue
{
  [self willChangeValueForKey:@"isNonVisit"];
  [self setPrimitiveValue:newValue forKey:@"isNonVisit"];
  [self didChangeValueForKey:@"isNonVisit"];

  self.status = [self calculateStatus];
}

The solution seems to work.

So, my question is: Is it OK? Am I violating some rules of CoreData or KVO?

Thanks for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回旧景 2024-12-11 19:13:15

你的方法看起来不错。

我唯一的建议是通过使用访问器方法从状态中提取布尔信息而不是存储它们来减少冗余。您仍然应该能够仅使用状态变量来为提取请求使用所需的谓词。但存储这些额外信息的开销应该是最小的。

Your method seems sound.

The only suggestion I would have is to reduce the redundancy by extracting the boolean information from the status with accessor methods rather than storing them. You still should be able to use the desired predicates for your fetch requests just using the status variable. But the overhead of storing this extra information should be minimal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文