Reputation: 31
This is my code, I decode a long json, make a object use, @State, ... What's wrong here why the scrolling is so extremely slow? I tried the Paul Hudson trick by adding .id(UUID()) ad list level, but with no success. Has it something to do with the dispatchingQueue.main.async that I place badly? Really I have no idea. Line breaks: double space or Any help is welcome!
import SwiftUI
struct ContentView: View {
@State private var shifts = [Shift]()
var body: some View {
NavigationView {
List(shifts, id: \.id) { shift in
HStack() {
Text(shift.date)
.font(.caption2)
padding()
Text(shift.name)
.font(.footnote)
padding()
Text(shift.planned)
.font(.footnote)
Text(":")
.font(.footnote)
Text(shift.replaced)
.font(.footnote)
}
}.id(UUID())
}.onAppear(perform: {
loadData()
})
}
}
extension ContentView
{
func loadData() {
let login = "xxx"
let password = "xxxx!"
let ftpServer = "www.xxxx.be"
let fileName = "export.json"
let ftpUrl = "ftp://\(login):\(password)@\(ftpServer)/\(fileName)"
guard let url = URL(string: ftpUrl) else {
print("error connection")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let ShiftDataStore = try? JSONDecoder().decode(ShiftDataStore.self, from: data) {
DispatchQueue.main.async {
self.shifts = ShiftDataStore.shifts
}
}
}
}.resume()
}
}
import Foundation
struct Shift: Decodable,Hashable {
var name: String
var planned: String
var replaced: String
var gsm: String
var function: String
var replacedFunction : String
var date: String
var id: UUID = UUID()
enum CodingKeys: String, CodingKey {
case name = "N"
case planned = "S"
case replaced = "RS"
case gsm = "M"
case function = "R"
case replacedFunction = "RR"
case date = "D"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
planned = try values.decode(String.self, forKey: .planned)
replaced = try values.decode(String.self, forKey: .replaced)
gsm = try values.decode(String.self, forKey: .gsm)
function = try values.decode(String.self, forKey: .function)
replacedFunction = try values.decode(String.self, forKey: .replacedFunction)
date = try values.decode(String.self, forKey: .date)
}
}
struct ShiftDataStore: Decodable {
var shifts: [Shift]
}
Upvotes: 2
Views: 1103
Reputation: 31
By changing padding() to spacer() fixed the scrolling issue. thanks for reading and helping!
Upvotes: 1