0

我正在尝试像这样覆盖我的 NSManagedObject 访问器:

- (NSSet *)children {

    NSSet *children = nil;

    [self willAccessValueForKey:@"children"];
    children = [self primitiveChildren];
    [self didAccessValueForKey:@"children"];

   //if no children, let's look in our db to see if any children
   //belong to me (parent object), and if they do, let's assign the
   //children to myself (parent)
    if(!children.count) {

         NSFetchRequest *request = [ChildMO fetchRequestForManagedObjectContext:self.managedObjectContext];
         request.predicate = [NSPredicate predicateWithFormat:@"predicate IN %@", @[self.id]];

         children = [NSSet setWithArray:[self.managedObjectContext executeFetchRequest:request error:nil]];

         [self willChangeValueForKey:@"children"];
         [self setPrimitiveChildren:children];
         [self didChangeValueForKey:@"children"];
    }

    return children;
}

但是,当我在将子 NSSet 分配给我的 ManagedObject 之前调用“willChangeValueForKey”时,似乎再次调用了子 getter,并且我进入了无限循环条件。

有任何想法吗?

4

0 回答 0