objective c - (KVO) Is it possible to remove all observers of an object, without knowing who is observing? -


here's current scenario: have object, , make copy of can modify properties w/o touching original. when i'm done copy gets dealloc'd, these warnings:

an instance 0x6080001250a0 of class linegraphic deallocated while key value observers still registered it. current observation info: <nskeyvalueobservationinfo 0x618000043ea0> ( <nskeyvalueobservance 0x6180000c1ce0: observer: 0x6000001834d0, key path: points, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x600000056fe0> <nskeyvalueobservance 0x6180000c1e30: observer: 0x6000001834d0, key path: shadow, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x600000057460> <nskeyvalueobservance 0x6180000c9bc0: observer: 0x6000001834d0, key path: secondheadstyle, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x60000005d7c0> <nskeyvalueobservance 0x6180000c1ff0: observer: 0x6000001834d0, key path: linestyle, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x600000051640> <nskeyvalueobservance 0x6180000c1f10: observer: 0x6000001834d0, key path: fillcolor, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x600000054f70> <nskeyvalueobservance 0x6180000c1b20: observer: 0x6000001834d0, key path: strokewidth, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x600000054d30> <nskeyvalueobservance 0x6180000c2140: observer: 0x6000001834d0, key path: strokecolor, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x6000000548b0> <nskeyvalueobservance 0x6180000c1d50: observer: 0x6000001834d0, key path: firstheadstyle, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x600000055060> <nskeyvalueobservance 0x6180000c1ea0: observer: 0x6000001834d0, key path: opacity, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x6080002421f0> <nskeyvalueobservance 0x6180000c1f80: observer: 0x6000001834d0, key path: visibleopacity, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x608000247560> <nskeyvalueobservance 0x6180000ca090: observer: 0x6000001834d0, key path: rotation, options: <new: no, old: yes, prior: yes> context: 0x0, property: 0x60800024a590> ) 

this how copied object:

imagedocument *selfcopy = [self copy]; 

i believe when doing this, of observers linegraphic property observe copy of linegraphic in copy of self. when selfcopy gets dealloc'd, these warnings come up.

i figured best solution remove observers-- because in case of copy, don't need there anyways.

from searching online, find ways remove observations object making. need other way, remove observers object. lot of resources reference removing observers nsnotifications, whereas need remove observers key paths (kvo)

i thinking if else fails, iterate on of observers of observed object (linegraphic)-- problem one: there lot of observers, , two: if there more observers added later, have add removal of them here in code. solution wouldn't work well

is possible remove observers of object, without knowing observing?

yes, but...

your problem kvo model designed around idea observer responsible both registering , de-registering observer; not observed objects responsibility; , trying usurp model.

the removeobserver methods throw exception if object being removed not observer; if remove observer observer not know, @ point attempt de-register, , exception, not good.

so think whether want do.

still want it?

i believe when doing this, of observers linegraphic property observe copy of linegraphic in copy of self.

this not expected usual behaviour, kvo used watch specific instance , copying instance creates new distinct object. possible implement copying observers copied, confusing in general , not recommended - observers not aware observing different object, , cannot later de-register themselves...

still want it?

well have been warned... think overriding, details left you.

an alternative suggestion:

don't usurp kvo, work it.

add method class updates instance (your original object) instance (your replacement object). method can update backing variables properties directly, without calling setters, , call methods kvo's willchange & didchange groups such no notifications made until whole object has been updated.

now can create "replacement", , update original. observers continue observe same object, , when time comes de-register themselves.

hth


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -