Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 05-09-2010, 03:40 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 29
leeus is on a distinguished road
Default KVO on MKAnnotations

I have the following code in my


Code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
//Other Code
[newAnnotation addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:@"ANSELECTED"];
//Other Code
}
And I use the KVO to see when it is selected or not.

However when I then call

Code:
[mapView removeAnnotations:existingpoints];
I get a KVO error and can't work out the code to remove the observer. Any ideas on the code to remove it as the bit here doesn't work
Code:
An instance 0x3b13e0 of class MKAnnotationView is being deallocated while key value observers are still registered with it. Observation info is being leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:

<NSKeyValueObservance 0x3afde0: Observer: 0x31dc20, Key path: selected, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10f984, Property: 0x3afcf0>
Current removal code....

Code:
for (id annotation in mapView.annotations) {

[self removeObserver:annotation forKeyPath:@"selected"];

}
leeus is offline   Reply With Quote
Old 05-10-2010, 12:59 AM   #2 (permalink)
Dr. Touch Cocoa Helpdesk
iPhone Dev SDK Supporter
 
Join Date: Sep 2008
Location: Vienna, Austria
Posts: 537
Oliver Drobnik is on a distinguished road
Send a message via AIM to Oliver Drobnik Send a message via MSN to Oliver Drobnik Send a message via Skype™ to Oliver Drobnik
Default

Quote:
Originally Posted by leeus View Post
I have the following code in my


Code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
//Other Code
[newAnnotation addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:@"ANSELECTED"];
//Other Code
}
And I use the KVO to see when it is selected or not.

However when I then call

Code:
[mapView removeAnnotations:existingpoints];
I get a KVO error and can't work out the code to remove the observer. Any ideas on the code to remove it as the bit here doesn't work
Code:
An instance 0x3b13e0 of class MKAnnotationView is being deallocated while key value observers are still registered with it. Observation info is being leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:

<NSKeyValueObservance 0x3afde0: Observer: 0x31dc20, Key path: selected, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10f984, Property: 0x3afcf0>
Current removal code....

Code:
for (id annotation in mapView.annotations) {

[self removeObserver:annotation forKeyPath:@"selected"];

}
KVO observers are registered in your annotations and NOT your mapview. So "self removeObserver:" cannot work. you'd have to use [annotation removeObserver instead.
__________________
regards

Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.

Linguan – makes localizing strings file fun!

Cocoanetics Parts Store – easy to use yet professionally looking components that you can use to spruce up your own apps. Augmented Reality, Calendar Control, Pin Lock or Purchase Button are only some examples. You get full source code, no static library crap, and lifetime support. Check it out today!
Oliver Drobnik is offline   Reply With Quote
Old 05-10-2010, 01:15 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 29
leeus is on a distinguished road
Default

Quote:
Originally Posted by Oliver Drobnik View Post
KVO observers are registered in your annotations and NOT your mapview. So "self removeObserver:" cannot work. you'd have to use [annotation removeObserver instead.

Thanks Oliver,

I get an error when I change it round to the correct way...

Code:
2010-05-10 07:12:30.766 LittleApp[4493:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <LittleAppViewController 0x31ddf0> for the key path "selected" from <MyObject 0x3a0160> because it is not registered as an observer.'
leeus is offline   Reply With Quote
Old 05-10-2010, 01:18 AM   #4 (permalink)
Dr. Touch Cocoa Helpdesk
iPhone Dev SDK Supporter
 
Join Date: Sep 2008
Location: Vienna, Austria
Posts: 537
Oliver Drobnik is on a distinguished road
Send a message via AIM to Oliver Drobnik Send a message via MSN to Oliver Drobnik Send a message via Skype™ to Oliver Drobnik
Default

Quote:
Originally Posted by leeus View Post
Thanks Oliver,

I get an error when I change it round to the correct way...

Code:
2010-05-10 07:12:30.766 LittleApp[4493:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <LittleAppViewController 0x31ddf0> for the key path "selected" from <MyObject 0x3a0160> because it is not registered as an observer.'
Just make sure that you use exactly the same parameters you did for removing the observer as you did for registering it.
__________________
regards

Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.

Linguan – makes localizing strings file fun!

Cocoanetics Parts Store – easy to use yet professionally looking components that you can use to spruce up your own apps. Augmented Reality, Calendar Control, Pin Lock or Purchase Button are only some examples. You get full source code, no static library crap, and lifetime support. Check it out today!
Oliver Drobnik is offline   Reply With Quote
Old 05-28-2010, 04:41 PM   #5 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 1
brewem is on a distinguished road
Default

You'll need to send the remove message to the view associated with the annotation, not the annotation itself. Try the following...

Code:
	
	for (id <MKAnnotation> annotation in mapView.annotations) {			
		[[mapView viewForAnnotation:annotation] removeObserver:self forKeyPath:@"selected"];
	}
brewem is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 355
5 members and 350 guests
.Snipe, baja_yu, guusleijsten, NSString, Paul Slocum
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,645
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, leighec68
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:29 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0