I have a function in my SwiftUI app that waits 0.5 seconds for all of the data to come in and then rearranges the order of a list based on that data. This eventually works, but it makes for some weird jumping around for the first second or so of the list order because it is being changed after 0.5 seconds. The code looks like this:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.restaurants.sort {
let waitTime1 = self.waitTimes[$0.id!] ?? -1
let waitTime2 = self.waitTimes[$1.id!] ?? -1
if (waitTime1 >= 0 && waitTime2 < 0) {
return true
} else if (waitTime1 < 0 && waitTime2 >= 0) {
return false
} else if (waitTime1 >= 0 && waitTime2 >= 0) {
return waitTime1 < waitTime2
} else if (waitTime1 < 0 && waitTime2 < 0) {
return false
} else {
return false
}
}
}
Essentially, I need wait 0.5 seconds for the self.waitTimes
data to come in or else the list will sort with out it. I want to try to show a ProgressView
over the regular content view while this sorting is taking place, for about a second. I tried putting all my content in a separate content view and then setting it equal to a ProgressView
for a second then setting it back to the original content, but this didn't work:
contents = AnyView(ProgressView())
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
contents = AnyView(origContents)
self.restaurants.sort {
let waitTime1 = self.waitTimes[$0.id!] ?? -1
let waitTime2 = self.waitTimes[$1.id!] ?? -1
if (waitTime1 >= 0 && waitTime2 < 0) {
return true
} else if (waitTime1 < 0 && waitTime2 >= 0) {
return false
} else if (waitTime1 >= 0 && waitTime2 >= 0) {
return waitTime1 < waitTime2
} else if (waitTime1 < 0 && waitTime2 < 0) {
return false
} else {
return false
}
}
}
Does anyone have any suggestions for how I can have the ProgressView
be displayed for a second while my list gets sorted? Thank you