Deleting all items in an array causes issues with the ForEach used to render the arrays items

I use Core Data with two entities: Set and Link. Both entities contain a UUID id. Sets can contain 1 or more Links and this is represented in a 1-many relationship

When the user wants to delete ALL links, this code is called

for link in learningSet.webLinksArray.reversed(){
       container.viewContext.delete(link)
}

do{
    try container.viewContext.save()
} catch let error {
    print("Failure deleting all links
 }

Here is the view code that renders link


if !learningSet.webLinksArray.isEmpty{
  LazyVGrid(columns: columns,
            alignment: .center, 
            spacing: 16,
            pinnedViews: []) {
       ForEach(learningSet.webLinksArray, id: \.id) { link in
                RichWebLinkView(webLink: link)
       }
   }
}

It all works, but I get the following error/warning in the console ForEach<Array, Optional, ModifiedContent<ModifiedContent<DeckMiniView, _FrameLayout>, _FlexFrameLayout>>: the ID nil occurs multiple times within the collection, this will give undefined results!

LazyVGridLayout: the ID nil is used by multiple child views, this will give undefined results!

It's unclear why the LazyVGrid is even being re-rendered when the weblinksArray is empty. Im guessing this is happening while the delete for loop is running and the array isn't fully empty.

Any suggestions on how to fix this would be appreciated.

Replies

That was it. Using a batch delete request in core data seems to work fine.