How do I avoid duplicating objects when saving a list to a parent?

My object is a recipe which has a list of ingredients and a list of directions.

dynamic var _id : String? = “”
dynamic var title : String? = “”
var ingredients = List()
var directions = List()

Below is how I am currently saving a recipe. Every time I save a recipe, I end up with duplicates of all of my ingredients and directions orphaned on my Realm Object Server. I imagine this is because I’m creating individual ingredients and directions before adding them to a List, then updating the recipe with the resulting lists. Do I need to add primary keys to the direction and ingredient schema, or am I structuring this function wrong?

func saveRecipe(title: String, directions: String, ingredients: String) {

    //convert ingredients to Realm list and save
    let ingredientArray = ingredients.trimmingCharacters(in: .whitespacesAndNewlines).components(separatedBy: "\n").map({
        (name: String) -> Ingredient in
        let ingToAdd = Ingredient() = name.trimmingCharacters(in: .whitespacesAndNewlines)
        return ingToAdd
    let ingredientRealmList = List<Ingredient>()
    ingredientRealmList.append(objectsIn: ingredientArray)

    //convert directions to Realm list and save
    let directionArray = directions.trimmingCharacters(in: .whitespacesAndNewlines).components(separatedBy: "\n").map({
        (text: String) -> Direction in
        let dirToAdd = Direction()
        dirToAdd.text = text.trimmingCharacters(in: .whitespaces)
        return dirToAdd
    let directionRealmList = List<Direction>()
    directionRealmList.append(objectsIn: directionArray)

    realmManager.update(localRecipes[selectedRecipe], with: [
        "title": title,
        "ingredients": ingredientRealmList,
        "directions": directionRealmList