SwiftData - Deleting related data

I’ve been reading a lot of articles on Swift Data. Right now, I’m concentrating on one-to-many relationships.

So I have a class called Account that has a one-to-many relationship with a class called Transaction. According to the information I’ve read, transactions are added using something like

account.transactions.append(newTransaction)

rather than

modelContext.insert(transaction)

This makes sense, since you want the transaction to go to a particular array and there’s no way to do that with the second method. (By the way, I was surprised to find that the insert also works for replacing an item, but I guess that’s the way classes work).

Anyway, when it comes to deleting an item from the transaction array, the documents I’ve read show that it’s done like this:

modelContext.delete(transaction)

When I do this, I get a runtime error: "Thread 1: EXC_BREAKPOINT (code=1, sucode=0x1c4352d5c)

What I’ve been doing is this:

account.transactions.remove(at: index)

This seems to work just fine, so I’m wondering why this is happening and if using the ‘remove’ method is causing a problem that I haven’t seen yet.

Hi Pete,

If you are a CWC+ member I would encourage you to take a look at the iOS Databases course under the “Launch Your First App” option when you log in to the CWC+ web page. It covers the issue of adding, deleting and updating records.

I’ve watched Chris’ video and many others. They don’t always say the same thing. I did discover some interesting behavior concerning adding models to a relationship. Check out the following code:

  private func saveTransaction()
  {
    let fromTransaction = Transaction(name: "To \(toAccount!.name)",
                                      amount: Double(amount)! * -1,
                                      memo: memo,
                                      categoryName: "Transfer",
                                      date: date)

    let toTransaction = Transaction(name: "From \(fromAccount!.name)",
                                    amount: Double(amount)!,
                                    memo: memo,
                                    categoryName: "Transfer",
                                    date: date)

    fromTransaction.account = fromAccount
    toTransaction.account = toAccount
    
    fromTransaction.transferTransaction = toTransaction
    toTransaction.transferTransaction = fromTransaction
    
    fromAccount!.createRunningBalance()
    toAccount!.createRunningBalance()
    
    dismiss()
  }

Notice that there is no code to add the transaction model to the account. The transaction is actually added after the following instruction (verified with print statements).

    fromTransaction.account = fromAccount

The one-to-many relationship is established between the account property in the Transaction model and the transactions property in the Account model. I suspect that Xcode sets up a ‘didSet’ on the Transactions account property where it appends the transaction to the designated account’s transactions array.

All the instructions I’ve seen show the ‘many’ item being added to the ‘one’ item using .append. It looks like that’s not necessary.

I wonder if this is intentional.