ma11hew28
ma11hew28

Reputation: 126457

NSManagedObject: Should I use transient or a regular @property?

Why not just use a regular @property instead of transient? I don't care about supporting undo.

Upvotes: 2

Views: 959

Answers (1)

rgeorge
rgeorge

Reputation: 7327

If you don't need undo, there's a good chance a plain unmodeled @property is better. When explicitly modeling a property as transient, the main differences are:

  • Setting the property marks the object as dirty, even though nothing would actually be saved;
  • The property is cleared when the object turns into a fault;
  • Weird things can happen when merging changes, depending on your chosen merge strategy.

Some experiments working out the semantics of a transient modeled property might be found at https://web.archive.org/web/20160423093331/http://www.2pi.dk/tech/cocoa/transient_properties.html by Jakob Stoklund Olesen. Because that's a dying archive.org link, I'll excerpt some choice tidbits:

  • "A transient property ... You should be thinking of it as something whose value is nil in the persistent store."

  • "So what are transient properties good for? ... [for] any property you don't need to be stored, but would like undo support for."

  • "Another use ... is caching for properties that can't be stored. Cross-store relationships and attributes with unsupported types are the typical examples. Before saving, you convert the property into something that can be stored, and write it to a binary 'shadow' attribute."

  • "It is better to imagine transient properties as representing 'something that is nil in the persistent store', than the common 'fancy instance variables with undo'."

A good article and I wish Jakob had left it up.

Upvotes: 3

Related Questions