Thursday, September 11, 2025
HomeiOS Developmentios - SwiftData doesn't persist modifications utilizing iOS18

ios – SwiftData doesn’t persist modifications utilizing iOS18


Firstly I might wish to apologise for not being exact within the title however I can not even take into consideration something higher.

I’ve received a simplified app demoing the problem. It really works completely high-quality in iOS17 however within the newest betas of iOS18, there’s one thing not on.

I exploit 2 mannequin contexts, one is created in UI by @Question macro, the second is by mannequin actor. In response to Apple I can have mannequin contexts as many as I would like, but when I attempt to modify information utilizing these two fashions, solely UI mannequin context persists the modifications. Modifications made by mannequin actor context are after ~20 seconds rolled again.

I’ve received a demo app to play with you may get it right here

State of affairs:

  1. Create a word (add title and assessment id)
  2. Go to Critiques view (there may be proven assessment id and what number of nots have this id connected)
  3. Edit the word and modify the assessment id and save.
  4. Go to Critiques view – you possibly can see the modifications you made
  5. Acquired to Notes view after which return to Critiques view after about 20 seconds.
  6. Modifications are usually not continued.

EditNoteView.swift

@MainActor
struct EditNoteView: View
{
    @Surroundings(.dismiss) non-public var dismiss
    @Surroundings(.dataModel) non-public var dataModel

    non-public var word: Be aware

    @State non-public var title: String
    @State non-public var reviewId: String

    var physique: some View {

        ...

        .toolbar {
            ToolbarItem(placement: .confirmationAction) {
                Button {
                    Activity {
                        await updateNote()
                    }
                } label: {
                    Textual content("Save")
                }
            }
        }
    }

    init (word: Be aware) {
        self.word = word
        _title = State(initialValue: word.title)
        _reviewId = State(initialValue: word.reviewId)
    }

    @MainActor
    func updateNote() async
    {
        // The next line does work in iOS 18 - all updates occur in a single mannequin context
        //await dataModel.updateNote(title: title, reviewId: reviewId, persistentModelId: word.persistentModelID)

        // The next line does NOT work in iOS 18 - word is up to date in 2 completely different mannequin contexts - solely major actor saves information
        await dataModel.updateReviewId(reviewId: reviewId, persistentModelId: word.persistentModelID)
        word.title = title // if this line is commented it really works too

        dismiss()
    }
}

DataModelActor.swift

@ModelActor
ultimate public actor DataModelActor : DataModel
{
    ...

    public func updateReviewId(reviewId: String, persistentModelId: PersistentIdentifier) {
        guard let word = modelContext.mannequin(for: persistentModelId) as? Be aware else {
            return
        }
        word.reviewId = reviewId
        strive? self.modelContext.save()
    }
}

I anticipate that the code ought to work like it really works on iOS17.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments