我認爲這個問題很簡單和常見,但我仍然不明白爲什麼它不起作用。讓我公開一下上下文:KVO和核心數據 - 自我觀察管理對象
比方說,我有一個很好的核心數據模型和一個名爲Document的實體。這份文件有一個類型,日期,編號和版本...例如,類型:d,日期:17-10-2015,編號:和版本。 本文檔具有以下四個值計算的標識符:D20151017-24-R03。
將會有很多這些文件,我將不得不通過它的標識符來搜索它們,而且我還會使用很多NSFetchedResultsController
。所以暫時的可能性是正確的。
這是我所做的。四個相關屬性的觀察第一寄存器:
- (instancetype)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context {
self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
if (self) {
[self addObserver:self forKeyPath:_Property(documentTypeRaw) options:0 context:KVODocumentIdContext];
[self addObserver:self forKeyPath:_Property(date) options:0 context:KVODocumentIdContext];
[self addObserver:self forKeyPath:_Property(number) options:0 context:KVODocumentIdContext];
[self addObserver:self forKeyPath:_Property(version) options:0 context:KVODocumentIdContext];
}
return self;
}
然後,註銷釋放時:
- (void)dealloc {
[self removeObserver:self forKeyPath:_Property(documentTypeRaw) context:KVODocumentIdContext];
[self removeObserver:self forKeyPath:_Property(date) context:KVODocumentIdContext];
[self removeObserver:self forKeyPath:_Property(number) context:KVODocumentIdContext];
[self removeObserver:self forKeyPath:_Property(version) context:KVODocumentIdContext];
}
,最後,管理的通知:
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == KVODocumentIdContext) {
[self updateDocumentId];
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
只是這裏的updateDocumentId
:
- (void) updateDocumentId {
NSString * prefix = [self documentTypePrefix:self.documentTypeRaw];
NSString * date = [self.date documentIdFormat];
NSString * number = [NSString stringWithFormat:@"%.2d",[self.number shortValue]];
NSString * version = [self.version isEqualToNumber:@0][email protected]"":[NSString stringWithFormat:@"-R%.2d",[self.version shortValue]];
self.documentId = [NSString stringWithFormat:@"%@%@-%@%@",prefix,date,number,version];
}
對我來說,這應該已經非常完美...但是...這不...
我有一個好:
failed: caught "NSInternalInconsistencyException", "<MBSDocument: 0x7fd9dbb45f40> (entity: MBSDocument; id: 0x7fd9dbb3cd00 <x-coredata:///MBSDocument/tB55CB581-AEC0-4211-A78A-7C48377BACC2612> ; data:
...
An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: date
Observed object: <MBSDocument: 0x7fd9dbb45f40> (entity: MBSDocument; id: 0x7fd9dbb3cd00 <x-coredata:///MBSDocument/tB55CB581-AEC0-4211-A78A-7C48377BACC2612> ; data:
...
我試過很多東西,其中包括刪除通話到super
在observeValueForKeyPath:ofObject:change:context:
,或登記在init
等,但沒有奏效。那麼,一些幫助將不勝感激。
在此先感謝。
編輯:這是上下文是如何定義的:
static void * KVODocumentIdContext = &KVODocumentIdContext;
編輯2:文檔類從NSManagedObject
繼承。
您是如何使用上下文進行觀察的。看起來上下文似乎有所不同。 – Sandeep
根據http://nshipster.com/key-value-observing/#correct-context-declarations我以同樣的方式宣佈上下文(我已編輯帖子) – Zaphod