xcode - Key-Value Observing with NSMutableData iOS -
in model have nsmutabledata object called data. want observe object , notification when data added or removed.
i understand to-many relationship, can't use normal kvo first adding observer code
[object addobserver:self forkeypath:@"data" options:nskeyvalueobservingoptionnew context:null];
and implement observevalueforkeypath:ofobject:change:context:
method when notification send.
i have read key-value observing documentation , found other posts it. observing changes mutable array using kvo vs. nsnotificationcenter & observing nsmutablearray insertion/removal use nsmutablearray example. possible nsmutabledata? don't understand have implement then.
can tell me have implement in class kvo nsmutabledata object working? or if not possible, solution?
many thanks!
there common misunderstanding kvo: not possible observe inner state of otherwise kvo compliant property.
that's case nsdata: kvo point of view data property of object not change, it's state of nsdata changes. if nsmutabledata expose kvo compliant properties contents observe changes observing key path (like @"data.contents") that's not case.
kvo works kvc compliant properties documented kvo compliant. cocoa framework classes don't give guarantee , it's bug observe objects , key paths.
a solution case not expose nsmutabledata
public property instead use immutable data , add methods appenddata:
custom class. give opportunity changes data object in these methods , emit kvo notifications manually (using willchangevalueforkey:@"data"
... change ... didchangevalueforkey:@"data"
).
Comments
Post a Comment