ios – SwiftData doesn’t persist modifications utilizing iOS18

0
26
ios – SwiftData doesn’t persist modifications utilizing iOS18


In the beginning I might prefer to apologise for not being exact within the title however I can’t even take into consideration something higher.

I’ve acquired 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 need, but when I attempt to modify knowledge 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 acquired a demo app to play with you may get it right here

Situation:

  1. Create a observe (add title and assessment id)
  2. Go to Evaluations view (there’s proven assessment id and what number of nots have this id hooked up)
  3. Edit the observe and modify the assessment id and save.
  4. Go to Evaluations view – you may see the modifications you made
  5. Obtained to Notes view after which return to Evaluations view after about 20 seconds.
  6. Modifications usually are not persevered.

EditNoteView.swift

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

    non-public var observe: Observe

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

    var physique: some View {

        ...

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

    init (observe: Observe) {
        self.observe = observe
        _title = State(initialValue: observe.title)
        _reviewId = State(initialValue: observe.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: observe.persistentModelID)

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

        dismiss()
    }
}

DataModelActor.swift

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

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

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here