Implementing a Transitional Object with Master Data

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

In the Core Dat Programming Guide:

If the unsupported attribute is an object, then in the model of the managed object you specify its type as undefined and that it is temporary. When you implement a custom entity class, there is no need to add an instance variable for the attribute - you can use the closed internal storage of the managed object. It should be noted that the implementations described below are that they cache the transition value. This makes access to value more efficient - it is also necessary for managing change. If you define your own instance variables, you should clear these variables in didTurnIntoFault, not dealloc or terminate.

Here is the header file:

#import <Foundation/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 properties for MKAnnotation with a title, subtitles and coordinates. Here I need to get subtitles from other fields and get the coordinate in longitude and latitude.

I'm not sure how to come to terms with what the manual says and what looks obviously wrong, and the compiler talks about it.

Once I get the header to the right, I can get the implementation correctly, and I will use awakeFromFault to set the values. I'm not sure if I need to release subtitles that will be NSString using didTurnIntoFault, but this is similar to what the manual says does.

I have not seen a really good example of how to implement a simple transient property. I am tempted to add properties to the managed entity object and forget about mentioning it in the managed entity model. But it seems that I will ignore something if I do this.

+4
source share
2 answers

The problem is that the MKAnnotation protocol stores the coordinate value in CLLocationCoordinate2D , which is not an object, but a structure and does not support key encoding. To use it as a transient property, you will need to wrap it in an object.

Master Data Programming Guide: Scaling Limitations on a Scale

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 encoding, you must save it in your managed object as an object - usually an instance of NSValue, although you can also define your own custom class. Then you will consider it as an object as described later in this article. It depends on the user object to retrieve the required structure from the NSValue (or user) object when retrieving the value and converting the structure to the NSValue (or user) object when setting the value.

+1
source

You need to change the type of the property to id or what is most suitable:

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

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

 @implementation SearchResult + (NSSet *) keyPathsForValuesAffectingCoordinate { return [NSSet setWithObjects:@"latitude", @"longitude", nil]; } - (id) coordinate { // Derive the coordinate value } @end 
+2
source

Source: https://habr.com/ru/post/1369023/


All Articles