Storing mutliple selected items in a list as individual records in new entity

Hello!

I have been trying to find a recent tutorial on how to handle this and it feels like it should be really simple but I haven’t been able to get this to work just yet.

I have an entity called ‘PeopleProfile’, which contains two attributes: “name” (String) and “id” (UUID).

I want a view where the user is able to see a selectable list of each “name” from every object in ‘PeopleProfile’, select the names they want, and then (after clicking a save button) an action triggers those selected names and all attributes from that person’s object to be saved as individual records to a new entity called “Group1”. (If the user changes those selections later, the records should be deleted or appended so that there are no duplicates)

After working with any selectable list examples that I find online, I seem to always have trouble with the transformation because they’ll typically use a simple string array with fixed values within the view and won’t show what to do with those selected elements. I need to be able to fetch all attributes for all objects within the ‘PersonProfile’ entity, let the user filter those objects by ‘name’ in the selector, and then create new itemized records for each selected object and all of its attributes in the ‘Group1’ entity.

I keep running into errors where the types can’t be converted and I need a better understanding on how to structure the flow of this process. If you could point me to a relevant example/tutorial or break down how the code should look, it would be much appreciated!

Thanks!

Lemme think about this for a while…

Group1 shouldn’t have all the properties of the PeopleProfile it should only have the id property of the person that’s saved, so it’s actually a list of UUID. When you retrieve the data, you connect the data together (called joining) from the PeopleProfile

This is what’s called a foreign key relationship in relational databases.

Your entities should look like this:

  • PeopleProfile: stores all the people

    • id as UUID
    • name as String
  • Groups: stores all the groups and what people are in that group

    • GroupNumber as Int, this shouldn’t be an “id” because id usually means something that’s unique, which in your example it’s just denoting that someone is in group 1
    • PeopleProfileId as UUID, this is the UUID of the PeopleProfile

When you fetch the data, you first fetch everyone from Groups with a specific GroupNumber, and then given that you have all the ids of the people. Next loop through those people and fetch all their information. (There might be a way to combine these, but I’m not sure what it is in CoreData). Haha I know what it would be writing normal SQL (which is used with relational databases)

That entity structure joining with the foreign key makes sense. I probably need to restructure the relationships between the entities as a first step. There is a one-to-many relationship between the ‘PersonProfile’ id and the ‘Group’ GroupNumber, so a person is able to be added to multiple groups, and multiple different ‘PersonProfile’ ids can be added to a GroupNumber, however there is a one-to-one relationship between a unique ‘PersonProfile’ id and a unique GroupNumber (maybe think of it as players in a league being added to a series of games where each game is a group).

I have a question about the bottom piece though. There are no GroupNumbers that exist at first, the user is creating a GroupNumber object each time they select names and press save. Essentially I am starting off with ‘PersonProfile’ entity data at the start of the process.

Is there a project/tutorial that you’ve seen that follows a similar process? I’m mostly struggling with the correct way to structure/code it out where multiple ‘PersonProfile’ ids can be stored to a GroupNumber object and then if there is a view where the user can select that GroupNumber object, those ‘PersonProfile’ ids allow the user to access all of the other ‘PersonProfile’ attributes associated with that id.

@camoroso89

That changes the model that I had in place. I assumed initially that a person could only be added to one group so the list of persons that was being presented to add to a group were those that had not already been added.

Then I thought if you are going to do that why not just have a single data entity with an Optional groupId which is where I am at now.

Now with the clarification that you have a one to many model, I’ll go back the the previous model that I had and also allow the person to be added to many groups.

This is what I came up with. Tricky challenge I must say so hope it helps out to some extent.

Thanks for putting this together Chris! Let me look through it.