簡體   English   中英

當項目添加到核心數據NSManagedObject關系的NSSet中或從中刪除時,如何觸發通知?

[英]How to trigger notifications when item added to or removed from Core Data NSManagedObject relationship's NSSet?

每當將項目添加到自定義NSManagedObject子類中的多對多關系/ NSSet或從中刪除項時,觸發NSNotification的正確方法是什么?

我有一個自定義NSManagedObject子類,該子類與另一個NSManagedObject子類具有一對多的無序關系。 為了清楚起見,假設這兩個子類是TeacherStudent ,其中一個Teacher可以具有多個Student對象,但是每個Student只分配給一個Teacher

我希望每當將Student添加到Teacher或從中刪除Student都可以觸發通知,無論是因為僅將Student分配給Teacher還是從Teacher分配Student ,或者是因為從核心數據中完全刪除了Student

我嘗試使用KVO,但似乎無法 NSSetcount屬性添加觀察者 將觀察者添加到@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM