Tutorial :NSManagedObject subclass conform to the protocol?


My main problem is storing the data which is not supported by Core Data. I already have a CLLocation property stored as a transformable attribute. I think the right approach is to declare a transient coordinate property. I keep getting EXC_BAD_ACCESS errors however.


My current subclass has the following interface:

#import <Foundation/Foundation.h>  #import <CoreLocation/CoreLocation.h>    @interface Event : NSManagedObject {    }    @property (nonatomic, retain) NSString* title;  @property (nonatomic, retain) NSDate* timeStamp;  @property (nonatomic, retain) CLLocation *location;    @end  

So I need to add

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;  


- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;  

to conform to the protocol. (setCoordinate is optional, but I need it if I want to have the annotation draggable)

In core data, The location property is transformable. and I use @dynamic in the implementation to generate the accessors. I'm using this property throughout the code, so I don't want to keep this.

I think the best way to go about it is to define my coordinate property in core data as transient, but I am not definitely doing something wrong with the implementation.

- (CLLocationCoordinate2D)coordinate {      CLLocationCoordinate2D cor = CLLocationCoordinate2DMake(self.location.coordinate.latitude,      self.location.coordinate.longitude);      return cor;  }    - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {      CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:newCoordinate.latitude      longitude:newCoordinate.longitude];      [self.location release];      self.location = newLoc;  }   

I've tried several ways, but this is the most recent one.

Edit 2: The EXC_BAD_ACCESS in:



You can make a NSManagedObject subclass conform to whatever protocol you wish as long as the protocol doesn't somehow override the context's management of the instances. MKAnnotation protocol should be perfectly safe.


Your problem is most likely here:

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {      CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:newCoordinate.latitude                                                      longitude:newCoordinate.longitude];      [self.location release]; //<-- Don't release properties!      self.location = newLoc;  }  

The generator accessors will handle retention for you. When you release them directly you screw that management up. You're also leaking newLoc. Try:

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {      CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:newCoordinate.latitude                                                      longitude:newCoordinate.longitude];      self.location = newLoc;      [newLoc release];  }  


It would be good to know where you are getting the EXC_BAD_ACCESS errors, however here are a couple of ideas. First off, assuming that there is an outside class that wants to call setCoordinate: you should really change the @property to list coordinate as readwrite as you are teling the world they aren't allowed to change this value. The other thing you could try is to go ahead and actually sent coordinate in setCoordinate: then you can eliminate your custom coordinate method and allow Core Data to write you a faster one.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »