In this simple example, no matter what I try, I cannot get the UI to update while the lines of the stream are being read. Not until the entire stream is finished does the UI update. This happens for the built in AsyncSequence or my custom one. (I am simulating a delay, but regardless of whether or not I use it, I don't get incremental updates.)
struct ContentView: View {
@State var entries: [String] = []
var body: some View {
List {
ForEach(entries, id: \.self) { entry in
Text(entry)
}
}
.task {
do {
let (bytes, _) = try await URLSession.shared.bytes(from: URL(string: "https://itunes.apple.com/us/rss/toppodcasts/limit=1/explicit=false/genre=1301/json")!)
for try await line in bytes.lines {
await update(line)
await mySleep(1)
}
} catch {
print("Error")
}
}
}
func update(_ line: String) async {
entries.append(line)
}
func mySleep(_ seconds: Int) async {
sleep(UInt32(seconds))
}
}