16 thoughts on “Core Data: Transient Properties on NSManagedObject

  1. i’ve been trying to grok the whole transient / transformable situation for a couple days, and your notes have helped clarify. i agree that the apple documentation leaves a great deal to be desired.

    I have a custom class that, like CLLocation in your example, wants to be part of a larger NSManagedObject entity. I’m thinking I’d like to use a transient or transformable attribute, but I’d like to figure out how those work and have a better idea of the implications of the choice before making it. this whole area would benefit from more examples like yours. thanks at least for taking a completely opaque view and making it streaky gray 🙂

    1. Susan, thanks for the kind words, glad it helped as it had stumped me for a while.

      I’ve yet to dig in deeper, but I don’t think the transformable bit is necessary – I suspect in my example that the CLLocation is being converted to NSData and carried around in memory like that which is an unnecessary overhead. I’m hoping that Core Data realises that is not necessary when the transient option is on.

      The reason in my case for choosing transformable was because it gives the property an ‘id’ type. Leave that off and the class generator doesn’t specify a type, which is a bit broken! The more I work with Core Data I realise that the generation of the class files is a bit unnecessary. As you get familiar with what it outputs, you realise you could hand code it and avoid some of the frustration.

  2. Thanks so much for taking the time to write this blog post. Really informative – and much better than Apple’s docs – as you noted.

  3. I am using some Transient property on my NSManagedObject to pre-calculate things that only need to be calculated once when dependent property is updated but when I reset the value on Transient property it never gets to saved.

    For example:

    -(NSString)age { // notify core data to access value [self willAccessValueForKey:@”age”]; // get primitive value NSString age = [self primitiveValueForKey:@”age”]; // is age already exist? if ( age == nil ) { // get age age = [GlobalHelper convertToAgeStringWithUTCDateOfBirth:(NSString*)self.dateOfBirth]; // set primitive value [self setPrimitiveValue:age forKey:@”age”]; } // notify core data done access value [self didAccessValueForKey:@”age”]; return age; }

    -(void)setDateOfBirth:(id)dateOfBirth { // change value for key [self willChangeValueForKey:@”dateOfBirth”]; [self willChangeValueForKey:@”age”]; // set primitive [self setPrimitiveValue:dateOfBirth forKey:@”dateOfBirth”]; [self setPrimitiveValue:nil forKey:@”age”]; // did change value for key [self didChangeValueForKey:@”age”]; [self didChangeValueForKey:@”dateOfBirth”]; } So as you can see from above code, when I am sync this object and when “dateOfBirth” field is updated it try to set “age” to “nil” and when accessing “age” field and if it is “nil” then calculate the age and never calculate it again.

    However the problem is when I am setting “age” field to “nil” in “setDateOfBirth” setting it never saved and when I am accessing age again “age” property still has the previous value but “dateOfBirth” is updated.

    Any help remotely related to this topic would be very much appreciated, I am having big trouble about this…

  4. I would like my NSManagedObject subclass to conform the the MKAnnotation protocol. So to conform to the protocol I need to implement the coordinate property (as documented here https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKAnnotation_Protocol/). Since coordinate this is a C struct (CLLocation2DCoordinate), I probably have to use a transient attribute and store the coordinate as NSValue. Am I right? If not, what approach do I have to use. Thx a lot!

  5. I gave the core location example a shot but unfortunately I couldn’t get core data to recognise there was a change to the coreLocation property when setting the latitude and longitude. That is, in the objects changed in context notification, coreLocation didn’t appear in the changedValues for the changedValuesForCurrentEvent in the object. I had to remove the setLatitude and setLongitude methods and only use setCoreLocation, then it worked correctly.

  6. Hi Dave! Soulfully from the heart to the soul for the article. And what can you say about NSSet and KVO in NSManagedObject ???

Leave a Reply

Your email address will not be published. Required fields are marked *