gpt4 book ai didi

objective-c - KVO for one-to-many but NSNull object passed into observeValueForKeyPath

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:44:50 26 4
gpt4 key购买 nike

我有一个与成员类具有一对多关系的托管对象。当我为成员添加观察员时,它起作用了。当一个新成员被添加到关系中时,将使用新对象调用 observeValueForKeyPath 并更改包含新成员对象的字典。但是,将第二次触发 observeValueForKeyPath,所有值为 nil 并更改字典 new="NULL"。第二个触发点是什么?我设置了一个断点,但不确定是谁触发的。

@interface FooObject : NSManagedObject {}
@property (nonatomic, strong) NSString *fooId;
@property (nonatomic, strong) NSSet* members;
@end

@implementation FooObject
@dynamic fooId;
@dynamic members;

- (NSMutableSet*)membersSet {
[self willAccessValueForKey:@"members"];

NSMutableSet *result = (NSMutableSet*)[self mutableSetValueForKey:@"members"];

[self didAccessValueForKey:@"members"];
return result;
}

- (void)registerObservers {
[self addObserver:self
forKeyPath:@"members"
options:NSKeyValueObservingOptionNew
context:nil];
}

- (void)unregisterObservers {
@try{
[self removeObserver:self forKeyPath:@"members"];
}@catch(id anException){
//do nothing, obviously it wasn't attached because an exception was thrown
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
id valueNewSet = [change objectForKey:NSKeyValueChangeNewKey];
if (![valueNewSet isKindOfClass:[NSSet class]]) {
// not a NSSet, it contains <null> value
NSLog(@"%@", change);
NSLog(@"%@", object);
}
if ([[change objectForKey:NSKeyValueChangeKindKey] intValue] == NSKeyValueChangeInsertion) {
// insert change is valid, process the changes
}

}
@end

日志输出:

{
kind = 1;
new = "<null>";
}

<FooObject: 0xfa9cc60> (entity: FooObject; id: 0xfa9be00 <x-coredata://39DB31FD-6795-4FDE-B700-819AB22E5170/SHInterest/p6> ; data: {
fooId = nil;
members = nil;
})

编辑 1我在 NSLog(@"%@", change) 设置了一个断点;这是堆栈跟踪,但对于确定调用者是谁没有真正帮助。

main -> UIApplicationMain -> NSKeyValueNotifyObserver -> observeValueForKeyPath:ofObject:change:context

编辑 2也许这仍然是一个错误? http://www.cocoabuilder.com/archive/cocoa/182567-kvo-observevalueforkeypath-not-reflecting-changes.html

最佳答案

我遇到了同样的问题,其中“隐式”分配(以及因此 KVO 观察者的通知)显然仅作为解除分配 MO 的一部分发生:我保存一个子 MOC,然后释放它,然后 iOS 释放它的 MO .我假设它随后在解除分配相关 MO 的过程中将一对多关系临时设置为 NSNull(删除规则级联适用)。

因此,除了更改插入和删除类型之外,我的 KVO 观察器现在还接受 NSKeyValueChangeSetting 并断言 change[NSKeyValueChangeNewKey] == NSNull.null。称之为务实的解决方案。

关于objective-c - KVO for one-to-many but NSNull object passed into observeValueForKeyPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11835607/

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