[英]How to trigger notifications when item added to or removed from Core Data NSManagedObject relationship's NSSet?
每當將項目添加到自定義NSManagedObject
子類中的多對多關系/ NSSet
或從中刪除項時,觸發NSNotification
的正確方法是什么?
我有一個自定義NSManagedObject
子類,該子類與另一個NSManagedObject
子類具有一對多的無序關系。 為了清楚起見,假設這兩個子類是Teacher
和Student
,其中一個Teacher
可以具有多個Student
對象,但是每個Student
只分配給一個Teacher
。
我希望每當將Student
添加到Teacher
或從中刪除Student
都可以觸發通知,無論是因為僅將Student
分配給Teacher
還是從Teacher
分配Student
,或者是因為從核心數據中完全刪除了Student
。
我嘗試使用KVO,但似乎無法 向 將觀察者添加到NSSet
的count
屬性添加觀察者 @dynamic
屬性。 此外,我嘗試實現蘋果文檔中概述的自己的自定義“一對多”訪問器方法,但在測試中似乎從未調用過我的自定義訪問器方法。 如果我的實現出現問題,這是我在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中精美的訪問器示例實際上並不是該關系的所有訪問器。 它們是您可以實現的額外的便捷訪問器。 但是,因為您試圖通過覆蓋所有設置器(或任何您想要調用變異訪問器的方法)來插入代碼,所以您還需要覆蓋最基本的設置器。
一對多關系的最基本訪問器是整個關系的NSSet
對象的設置器和獲取器。 在您的情況下, - (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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.