0

I'm curious about structuring NSManagedObjects, specifically the nesting of them. This is my first time designing a data model so let me explain a bit before my question. Let's say I have an NSManagedObject for Users. Users have many different properties:

  • first name : NSString
  • last name : NSString
  • bio : NSString
  • photo : NSData
  • email : NSString
  • registrationTimestamp : NSDate
  • password : NSString
  • username : NSString
  • followers : User (many relationship)
  • people they are following : User (many relationship)

There are a lot of properties within that object, and there could be more. So my question essentially is, is it safe, or even proper, to nest NSManagedObjects? So that instead of all of those properties I have:

  • information : UserInformation
  • registration : UserRegistration
  • followers : User
  • following : User

Where UserInformation and UserRegistration would be separate NSManagedObjects that hold some of those original properties. I understand that this might not be the best case for nested objects, but what if I had more complicated objects that would be easier to understand if they were nested.

Thanks in advance for the input!

bpercevic
  • 450
  • 6
  • 14

2 Answers2

0

The right way for doing it is just drawing your ER model and defining the entities. If doing so, you define an entity for UserInformation and UserRegistration with a one to one relationship to User, then there is no problem doing it so. For the followers and following cases, you have to define self relationships (one to many to self entity https://stackoverflow.com/a/4504770/474740).

Define the model using relationships and marking them with the specific type (one to one, one to many).

Just keep in mind how could be more easy for you when saving and fetching the data.

Community
  • 1
  • 1
Floydian
  • 249
  • 2
  • 17
0

That is proper and safe, but would advise against it if you don't plan on making them atomic, reusable or have some other design that would benefit from it. Here is why... You will have to write longer statements to gain access to your properties, the database operations for fetches wi have to include joins to get the relationships properties, and you aren't really modeling the correct object... But if you need to for some reason other than just not liking models with lots of properties, then go ahead.

Grady Player
  • 14,399
  • 2
  • 48
  • 76