3

I have two entities (Session and User). Session entity has loggedInUser relationship to User entity. And the User entity has session inverse relationship to Session entity.

Xcode generates properties with retain attribute for both direct and inverse relationships. Is it ok from object graph point of view? My understanding is that reverse relationship should be represented as assign property.

Also, in the schema editor it's not visible which relationship is the main one (i.e. loggedInUser has session as its inverse, and session's inverse is loggedInUser).

Maybe I'm missing something?

Thanks.

Mikayel Aghasyan
  • 225
  • 2
  • 11
  • What do you mean the `main` relationship? A relationship is important both ways. A `session` has one `user` or conversely a `user` has one `session`. Why is one direction more important than the other? – Paul.s Oct 30 '11 at 16:54
  • I mean that in my design Session is the parent object and the user is child to it. Otherwise there will be retain loop. – Mikayel Aghasyan Oct 30 '11 at 16:58

1 Answers1

2

Doesn't really explain why as such but the Apple docs for Core Data state

In addition to always being nonatomic, dynamic properties only honor retain or copy attributes—assign is treated as retain. You should use copy sparingly as it increases overhead. You cannot use copy for relationships because NSManagedObject does not adopt the NSCopying protocol, and it's irrelevant to the behavior of to-many relationships.

So by reading that even if you did set a relationship as assign it would be treated as retain

Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • So, I guess the framework will take care of releasing objects correctly (avoiding retain cycles). Thanks for bringing this. – Mikayel Aghasyan Oct 30 '11 at 17:20
  • Another question is that Xcode 4.2 generates "retain" instead of "strong" with ARC enabled. Do you think I should change it to "strong"? – Mikayel Aghasyan Oct 30 '11 at 17:22
  • 1
    Core Data does not avoid retain cycles. You have to manage them yourself by pruning the tree with `-refreshObject:mergeChanges:` when you traverse a cycle. – adonoho Oct 31 '11 at 13:31