I’ve received a simplified app demoing the difficulty. It really works completely positive in iOS 17, however within the newest betas of iOS 18, there’s one thing not on.
I am utilizing 2 mannequin contexts, one is created in UI by means of @Question macro, the second is by utilizing mannequin actor. In keeping with Apple, I can have as many mannequin contexts 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 will get it right here
Situation:
- Create a word (add title and evaluation id)
- Go to Opinions view (there’s proven evaluation id and what number of nots have this id hooked up)
- Edit the word and modify the evaluation id and save.
- Go to Opinions view – you possibly can see the modifications you made
- Obtained to Notes view after which return to Opinions view after about 20 seconds.
- Modifications are usually not continued.
Be aware.swift
@Mannequin
remaining public class Be aware
{
var title: String = ""
var reviewId: String = ""
init( _ title: String, _ reviewId: String) {
self.title = title
self.reviewId = reviewId
}
}
EditNoteView.swift
@MainActor
struct EditNoteView: View
{
@Setting(.dismiss) non-public var dismiss
@Setting(.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 {
Process {
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
remaining public actor DataModelActor : DataModel
{
...
public func updateNote(title: String, reviewId: String, persistentModelId: PersistentIdentifier) {
guard let word = modelContext.mannequin(for: persistentModelId) as? Be aware else {
return
}
word.title = title
word.reviewId = reviewId
attempt? self.modelContext.save()
}
public func updateReviewId(reviewId: String, persistentModelId: PersistentIdentifier) {
guard let word = modelContext.mannequin(for: persistentModelId) as? Be aware else {
return
}
word.reviewId = reviewId
attempt? self.modelContext.save()
}
}
I count on that the code ought to work like it really works on iOS17.