When to use Core Data Relationships

I want to develop an application when the user enters a first name it is connected to a last name. Some both the first and last name are connected. Every first name has a last name and vice versa. Do I need to set up a relationship between entities to do this successfully. I added a photo of what I have in my core data so far. Seeking advice.

Screen Shot 2022-04-23 at 12.56.05 AM

In your example you do not need a relationship because you have only one entity. The first and last name are attributes of the same entity.

Use relationships when an entity needs access to another entity. You must have 2 or more entities in your Core Data data model to use relationships.

Suppose you’re writing a blogging app. The data model has three entities: Post, Tag, and Category. A blog post can have multiple tags and categories. Creating a to-many tags relationship in the Post entity will store each post’s tags. Creating a to-many categories relationship in the Post entity will store each post’s categories.

By the way you should choose a different name than Data for your entity. The Foundation framework has a Data class. In your example Person or User would be a better entity name.

This topic was automatically closed after 166 days. New replies are no longer allowed.