I'm giving a go to the new TabSection with iOS 18 but I'm facing an issue with sections actions. I have the following section inside a TabView:
TabSection {
ForEach(accounts) { account in
Tab(account.name , systemImage: account.icon, value: SelectedTab.accounts(account: account)) {
Text(account.name)
}
}
} header: {
Text("Accounts")
}
.sectionActions {
AccountsTabSectionAddAccount()
}
I'm showing a Tab for each account and an action to create new accounts. The issue I'm facing is that when there are no accounts the entire section doesn't appear in the side bar including the action to create new accounts. To make matters worse the action doesn't show at all in macOS even when there are already accounts and the section is present in side bar. Is there some way to make the section actions always visible?
Post
Replies
Boosts
Views
Activity
I'm building a simple App using SwiftData. In my app a user can create, remove and edit posts. When editing, I want them to be able to hit "Save" to persist the changes or "Cancel" to discard the changes. The approach I'm using is to disable autosave and call modelContext.save() when saving and modelContext.rollback() when discarding the changes. So my modelContainer is defined as follows:
WindowGroup {
ContentView()
.modelContainer(for: [Post.self], isAutosaveEnabled: false)
}
and I Save and Cancel like this:
PostForm(post: post)
.toolbar {
ToolbarItemGroup(placement: .cancellationAction) {
Button("Cancel") {
if modelContext.hasChanges {
modelContext.rollback()
}
dismiss()
}
}
ToolbarItemGroup(placement: .confirmationAction) {
Button("Save") {
do {
if modelContext.hasChanges {
try modelContext.save()
}
} catch {
fatalError("Failed to save post: \(error.localizedDescription)")
}
callback?()
dismiss()
}
}
}
The issue I am facing is that after calling modelContext.rollback() my Posts aren't updating in the UI, they still show the changes. Restarting the app shows the Posts without the changes so I'm guessing that modelContext.rollback() is in fact discarding the changes and not persisting them in the Storage, the UI is the one that is not reacting to the change. Am I doing something wrong here? Is this approach correct?