Issue when rearranging List item in detail view using SwiftUI Navigation View and Sorted FetchRequest
UPDATE 2 (iOS 14 beta 3)
The issue seems to be fixed in iOS 14 beta 3: the Detail view does no longer pop when making changes that affect the sort order.
UPDATE
It seems Apple sees this as a feature, not a bug; today they replied to my feedback (FB7651251) about this issue as follows:
We would recommend using isActive and managing the push yourself using
the selection binding if this is the behavior you desire. As is this
is behaving correctly.This is because the identity of the pushed view changes when you
change the sort order.
As mentioned in my comment above I believe this is a bug in iOS 13.4
.
A workaround could be to use a NavigationLink outside of the List and define the List rows as Buttons that
a) set the task to be edited (a new @State var selectedTask) and
b) trigger the NavigationLink to TaskDetail(task: selectedTask!).
This setup will uncouple the selected task from its position in the sorted list thus avoiding the misbehaviour caused by the re-sort potentially caused by editing the dueDate.
To achieve this:
- add these two @State variables to struct ContentView
@State private var selectedTask: Task?
@State private var linkIsActive = false
- update the body of struct ContentView as follows
var body: some View {
NavigationView {
ZStack {
NavigationLink(
destination: linkDestination(selectedTask: selectedTask),
isActive: self.$linkIsActive) {
EmptyView()
}
List(tasks) { task in
Button(action: {
self.selectedTask = task
self.linkIsActive = true
}) {
NavigationLink(destination: EmptyView()){
Text("\(task.title)")
}
}
}
}
.navigationBarTitle("Tasks").navigationBarItems(trailing: Button("new") {self.addTask()})
}
}
- add the following struct to ContentView.swift
struct linkDestination: View {
let selectedTask: Task?
var body: some View {
return Group {
if selectedTask != nil {
TaskDetail(task: selectedTask!)
} else {
EmptyView()
}
}
}
}
Objects not sorted after creation Core Data SwiftUI
The problem seems that it is just not assigned in the provided code, because naming attribute by id
means noting in CoreData (and by default remains 0), so at least with added below it is updated & sorted properly (in testing model id is Int32, but it is not important)
for i in 1...5 {
let language = ProgrammingLanguage(context: self.managedObjectContext)
language.id = Int32(i) // << here !!
language.name = "\(i) SwiftUI"
Tested with Xcode 11.4 / iOS 13.4
Weird list view when I add navigationBarItem
Try using .navigationViewStyle as below:
struct ContentView: View {
@State private var isFullScreen = false
var body: some View {
NavigationView {
List{
Text("One")
Text("Two")
}
.navigationTitle("Testt")
.navigationBarItems(leading: Text("Add"))
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
Preselect the first item in the NavigationList, but also display the content
In general List's selection and NavigationLink
are different things (that case that on user interaction they react both does not change the idea), they should be bound explicitly.
Try to bind links to the same selection, like
List(boards, id: \.self, selection: $selectedBoard) { board in
NavigationLink(tag: board, selection: $selectedBoard) { // << here !!
BoardView(board: board)
} label: {
Text(board.name!)
}
Related Topics
Using "If Let" with Logical "Or" Operator
Swift 4 - Notification Center Addobserver Issue
iOS Tabbar Item Title Issue in iOS13
How to Animate Transition Between Views in Swiftui
Use a Function to Find Common Elements in Two Sequences in Swift
How Are Optional Values Implemented in Swift
Swift Standard Documentation Comment
Can a Swift Property Wrapper Reference the Owner of the Property Its Wrapping
Binary Operator '+' Cannot Be Applied to Two 'T' Operands
Using Guard with a Non-Optional Value Assignment
Dyld: Library Not Loaded: @Rpath/Libswiftcore.Dylib Problem with New Xcode (10.2)
Fbsdkapplicationdelegate Application Openurl:Sourceapplication:Annotation Deprecated