开发者

Implementing a Transient Property with Core Data

I can't figure this out. What I have read about transient properties tells me they can be identified in the object model with an undefined type. But the compiler complains about this with an error that the type is unknown.

From the Core Dat Programming Guide:

If the non-supported attribute is an object, then in the managed object model you specify its type as undefined, and that it is transient. When you implement the entity’s custom class, there is no need to add an instance variable for the attribute—you can use the managed object's private internal store. A point to note about the implementations described below is that they cache the transient value. This makes accessing the value more efficient—it is also necessary for change management. If you define custom instance variables, you should clean up these variables in didTurnIntoFault rather than dealloc or finalize.

Here is the header file:

#import <Foundati开发者_JS百科on/Foundation.h>
#import <CoreData/CoreData.h>

@class SearchTerms;

@interface SearchResult : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * lattitude;
@property (nonatomic, retain) NSString * details;
@property (nonatomic, retain) NSString * endTime;
@property (nonatomic, retain) NSString * longitude;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * imageLink;
@property (nonatomic, retain) NSString * startTime;
@property (nonatomic, retain) UNKNOWN_TYPE coordinate;
@property (nonatomic, retain) UNKNOWN_TYPE subtitle;
@property (nonatomic, retain) SearchTerms * searchUsed;

@end

I am trying to include the properties for an MKAnnotation with title, subtitle, and coordinate . Here, I need to derive subtitle from other fields, and derive coordinate from longitude and latitude.

I'm not sure how to reconcile what the Guide says and what looks plainly wrong, and the compiler says so.

Once I get the header right, I may be able to get the implementation right, and I'll use awakeFromFault to set the values. I'm not sure if I need to release the subtitle, which will be an NSString, using didTurnIntoFault, but that seems to be what the Guide says to do.

I haven't seen really good example of how to implement a simple transient property. I am tempted to jsut add the properties to the managed object entity and forget about mentioning it in the managed object model. But it seems that I would be overlooking something if I do that.


You need to change the property's type to id, or whatever is most suitable:

@interface SearchResult : NSManagedObject
{}
@property (nonatomic, retain) id coordinate;
@end

Another way to handle this is via KVC and dependent keys:

@implementation SearchResult
+ (NSSet *) keyPathsForValuesAffectingCoordinate
{
  return [NSSet setWithObjects:@"latitude", @"longitude", nil];
}

- (id) coordinate
{
  // Derive the coordinate value
}
@end


The problem here is that the MKAnnotation protocol stores its coordinate value in a CLLocationCoordinate2D which, is not an object but a struct and it does not support key-value coding. To use it as a transitional property, you will need to wrap it in an object.

The Core Data Programming Guide: Scalar Value Constraints

If you want to use a scalar type or structure that is not one of those supported directly by Core Data and not one of the structures supported by key-value coding, you must store it in your managed object as an object—typically an NSValue instance, although you can also define your own custom class. You will then treat it as an object value as described later in this article. It is up to users of the object to extract the required structure from the NSValue (or custom) object when retrieving the value, and to transform a structure into an NSValue (or custom) object when setting the value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜