gpt4 book ai didi

ios - 当项目添加到核心数据 NSManagedObject 关系的 NSSet 或从中删除时如何触发通知?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:01:45 25 4
gpt4 key购买 nike

每当在自定义 NSManagedObject< 中的一对多关系/NSSet 中添加或删除项目时,触发 NSNotification 的正确方法是什么 子类?

我有一个自定义的 NSManagedObject 子类,它与另一个 NSManagedObject 子类具有一对多的无序关系。为清楚起见,假设这两个子类是 TeacherStudent,其中一个 Teacher 可以有多个 Student 对象,但是其中每个 Student 只分配给一个 Teacher

我希望能够在 Teacher 添加或删除 Student 时触发通知,无论是因为 Student是简单地分配给或分配自 Teacher 还是因为 Student 已从 Core Data 中完全删除。

我试过使用 KVO,但你似乎无法将观察者添加到 NSSetcount 属性 添加观察者到 @dynamic 属性。此外,我尝试实现自己的自定义对多访问器方法,如 Apple's documentation 中所述。 ,但在测试中似乎从未调用过我的自定义访问器方法。如果我的实现有问题,下面是我在 Teacher 中的实现方式:

@implementation Teacher

@dynamic students;

- (void)addStudentsObject:(Student *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:NSStringFromSelector(@selector(students))
withSetMutation:NSKeyValueUnionSetMutation
usingObjects:changedObjects];
[[self primitiveStudents] addObject:value];
[self didChangeValueForKey:NSStringFromSelector(@selector(students))
withSetMutation:NSKeyValueUnionSetMutation
usingObjects:changedObjects];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TEACHER_STUDENT_WAS_ADDED object:self];
}

- (void)removeStudentsObject:(Student *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:NSStringFromSelector(@selector(students))
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:changedObjects];
[[self primitiveStudents] removeObject:value];
[self didChangeValueForKey:NSStringFromSelector(@selector(students))
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:changedObjects];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TEACHER_STUDENT_WAS_REMOVED object:self];
}

- (void)addStudents:(NSSet *)values
{
[self willChangeValueForKey:NSStringFromSelector(@selector(students))
withSetMutation:NSKeyValueUnionSetMutation
usingObjects:values];
[[self primitiveStudents] unionSet:values];
[self didChangeValueForKey:NSStringFromSelector(@selector(students))
withSetMutation:NSKeyValueUnionSetMutation
usingObjects:values];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TEACHER_STUDENTS_WERE_ADDED object:self];
}

- (void)removeStudents:(NSSet *)values
{
[self willChangeValueForKey:NSStringFromSelector(@selector(students))
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:values];
[[self primitiveStudents] minusSet:values];
[self didChangeValueForKey:NSStringFromSelector(@selector(students))
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:values];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TEACHER_STUDENTS_WERE_REMOVED object:self];
}

...

@end

最佳答案

根本问题原来是 Apple Docs 中花哨的访问器示例您用作引用的那些实际上不是关系的所有访问器。它们是您可以根据需要实现的额外便利访问器。但是因为您试图通过覆盖所有 setter (或任何您想要调用的可变访问器)来插入代码,所以您还需要覆盖最基本的 setter 。

对多关系的最基本访问器是整个关系的 NSSet 对象的 setter 和 getter。在您的情况下,- (NSSet*) students- (void) setStudents:(NSSet*)students

因此,至少,您的通知需要发布在 setStudents 中:

-(void) setStudents:(NSSet*)students{ 
[self willChangeValueForKey:@"students"];
[self setPrimitiveValue:students forKey:@"students"];
[self didChangeValueForKey:@"students"];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TEACHER_STUDENTS_WERE_ADDED object:self];

}

这是默认使用的 setter。

关于ios - 当项目添加到核心数据 NSManagedObject 关系的 NSSet 或从中删除时如何触发通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29359450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com