2015-05-11 7 views
18

Ho alcuni problemi con i seguenti arresti anomali recuperati dalla sezione "Arresti anomali" in Xcode. Solo pochi dispositivi sono interessati da questo rapporto di arresto anomalo.Blocco con removeObserver: forKeyPath: in Foundation

Ho analizzato il problema ma credo che sia un bug nel framework Apple. Ma non riesco a trovare un modo per replicarlo.

Qui una discussione simile: Help with crash in removeObserver:forKeyPath:.

Eventuali suggerimenti?

Discussione 0 Nome: Discussione 0 Crashed:

0 Fondazione
0x23507591 _NSKeyValueReplaceObservationInfoForObject + 69 (NSKeyValueObserving.m: 1166)

1 Fondazione
0x23506fe7 - [NSObject (NSKeyValueObserverRegistration) _removeObserver: forProperty:] + 327 (NSKeyValueObserving.m: 1552)

2 Fondazione
0x23506b03 - [NSObject (NSKeyValueObserverRegistration) removeObserver: forKeyPath:] + 163 (NSKeyValueObserving.m: 1696)

3 Foundation
0x235069a7 - [NSObject (NSKeyValueObserverRegistration) removeObserver: forKeyPath: contesto:] + 219 (NSKeyValueObserving.m: 1663)

4 ApplicationName 0x0002e233 - [Supervisore removeObjectObserver: forKeyPath:] + 115 (Supervisor.m: 344)

dove 012.321.882.149.è

- (void) removeObjectObserver:(id)object forKeyPath:(NSString *)keyPath { 

    @try {   
     [object removeObserver:self forKeyPath:keyPath context:PrivateKVOContext]; 

    } @catch (NSException *exception) { } 
} 

risposta

6

Observers in Objective-C deve essere utilizzato con attenzione: non aggiungere contemporaneamente multipli osservatori alla proprietà dello stesso oggetto, e avvolgere la rimozione se ce n'è uno:

if ([self observationInfo]) { 
     @try { 
      [self removeObserver:self forKeyPath:keyPath]; 
     } 
     @catch (NSException *exception) {} 
    } 

Si verificano arresti anomali perché si tenta di rimuovere due volte l'osservatore o si rimuove un osservatore inesistente.

Si dovrebbe aggiungere observers questo modo:

[yourObject addObserver:self forKeyPath:keypath options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil/yourContext]; 

EDIT: È possibile rimuovere un osservatore su un oggetto già deallocare, con conseguente questo incidente.

+0

[sé observationInfo] questo codition non è soddisfacente! dovrei controllare [yourObject observationInfo]? –

3

Normalmente si dispone di un ivar per essere in grado di sapere se si sta osservando il keypath dell'oggetto al momento oppure no. Come @property (...) BOOL textFieldTextObserving; E i vostri metodi di aggiunta/rimozione-osservazione dovrebbero controllare questa proprietà prima di aggiungere/rimuovere per evitare di aggiungere/rimuovere due volte l'osservatore. Puoi anche usare NSDictionary se ci sono molti oggetti e keypath osservanti (per mantenere @ (BOOL) come oggetti e -identificatori come chiavi).

In ogni caso, fare cose usando @ try-exception non è un modo consigliato Objective-C. di Apple docs dice:

"You should not use a try-catch block in place of standard programming checks for Objective-C methods. In the case of an NSArray, for example, you should always check the array’s count to determine the number of items before trying to access an object at a given index. The objectAtIndex: method throws an exception if you make an out-of-bounds request so that you can find the bug in your code early in the development cycle—you should avoid throwing exceptions in an app that you ship to users." https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ErrorHandling/ErrorHandling.html

Problemi correlati