load tableView fast
up vote
3
down vote
favorite
Hi I have a problem with UI in my app, I make a closure UITableView so it will display half of my screen and set a tableView delegate and dataSource in viewDidLoad. but my UI is load very slow, I try to use DispatchQueue in my request but nothing works. and try DispatchQueue.global(qos: .background).async in tableView delegate and datasource, the UI show up and table view being display but there is a warning say tableView delegate and dataSource main run in main thread. But the object load slow too. can anyone suggest what should I do?
this is my code
let tableView: UITableView = {
let view = UITableView()
view.backgroundColor = .white
view.layer.cornerRadius = 20
return view
}()
this is my function and called in viewDidLoad
fileprivate func setupTableView() {
DispatchQueue.main.async {
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: self.cellId)
self.tableView.separatorColor = .clear
self.tableView.backgroundColor = .clear
self.tableView.rowHeight = 53
}
}
this is my request code
DispatchQueue.global(qos: .background).async {
Alamofire.request(prayerUrl, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseData(completionHandler: { (dataResponse) in
if let err = dataResponse.error {
print("Failed to fetch data:", err)
return
}
guard let data = dataResponse.data else { return }
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let decodeErr {
print("Failed to decode:", decodeErr)
}
})
}
ios swift uitableview grand-central-dispatch
add a comment |
up vote
3
down vote
favorite
Hi I have a problem with UI in my app, I make a closure UITableView so it will display half of my screen and set a tableView delegate and dataSource in viewDidLoad. but my UI is load very slow, I try to use DispatchQueue in my request but nothing works. and try DispatchQueue.global(qos: .background).async in tableView delegate and datasource, the UI show up and table view being display but there is a warning say tableView delegate and dataSource main run in main thread. But the object load slow too. can anyone suggest what should I do?
this is my code
let tableView: UITableView = {
let view = UITableView()
view.backgroundColor = .white
view.layer.cornerRadius = 20
return view
}()
this is my function and called in viewDidLoad
fileprivate func setupTableView() {
DispatchQueue.main.async {
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: self.cellId)
self.tableView.separatorColor = .clear
self.tableView.backgroundColor = .clear
self.tableView.rowHeight = 53
}
}
this is my request code
DispatchQueue.global(qos: .background).async {
Alamofire.request(prayerUrl, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseData(completionHandler: { (dataResponse) in
if let err = dataResponse.error {
print("Failed to fetch data:", err)
return
}
guard let data = dataResponse.data else { return }
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let decodeErr {
print("Failed to decode:", decodeErr)
}
})
}
ios swift uitableview grand-central-dispatch
The answer by Robert Dresler is good, but remember the slow loading may also be caused by your server, and not your code.
– Zonily Jame
Nov 11 at 18:28
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Hi I have a problem with UI in my app, I make a closure UITableView so it will display half of my screen and set a tableView delegate and dataSource in viewDidLoad. but my UI is load very slow, I try to use DispatchQueue in my request but nothing works. and try DispatchQueue.global(qos: .background).async in tableView delegate and datasource, the UI show up and table view being display but there is a warning say tableView delegate and dataSource main run in main thread. But the object load slow too. can anyone suggest what should I do?
this is my code
let tableView: UITableView = {
let view = UITableView()
view.backgroundColor = .white
view.layer.cornerRadius = 20
return view
}()
this is my function and called in viewDidLoad
fileprivate func setupTableView() {
DispatchQueue.main.async {
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: self.cellId)
self.tableView.separatorColor = .clear
self.tableView.backgroundColor = .clear
self.tableView.rowHeight = 53
}
}
this is my request code
DispatchQueue.global(qos: .background).async {
Alamofire.request(prayerUrl, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseData(completionHandler: { (dataResponse) in
if let err = dataResponse.error {
print("Failed to fetch data:", err)
return
}
guard let data = dataResponse.data else { return }
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let decodeErr {
print("Failed to decode:", decodeErr)
}
})
}
ios swift uitableview grand-central-dispatch
Hi I have a problem with UI in my app, I make a closure UITableView so it will display half of my screen and set a tableView delegate and dataSource in viewDidLoad. but my UI is load very slow, I try to use DispatchQueue in my request but nothing works. and try DispatchQueue.global(qos: .background).async in tableView delegate and datasource, the UI show up and table view being display but there is a warning say tableView delegate and dataSource main run in main thread. But the object load slow too. can anyone suggest what should I do?
this is my code
let tableView: UITableView = {
let view = UITableView()
view.backgroundColor = .white
view.layer.cornerRadius = 20
return view
}()
this is my function and called in viewDidLoad
fileprivate func setupTableView() {
DispatchQueue.main.async {
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: self.cellId)
self.tableView.separatorColor = .clear
self.tableView.backgroundColor = .clear
self.tableView.rowHeight = 53
}
}
this is my request code
DispatchQueue.global(qos: .background).async {
Alamofire.request(prayerUrl, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseData(completionHandler: { (dataResponse) in
if let err = dataResponse.error {
print("Failed to fetch data:", err)
return
}
guard let data = dataResponse.data else { return }
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let decodeErr {
print("Failed to decode:", decodeErr)
}
})
}
ios swift uitableview grand-central-dispatch
ios swift uitableview grand-central-dispatch
asked Nov 11 at 15:20
ferryawijayanto
547
547
The answer by Robert Dresler is good, but remember the slow loading may also be caused by your server, and not your code.
– Zonily Jame
Nov 11 at 18:28
add a comment |
The answer by Robert Dresler is good, but remember the slow loading may also be caused by your server, and not your code.
– Zonily Jame
Nov 11 at 18:28
The answer by Robert Dresler is good, but remember the slow loading may also be caused by your server, and not your code.
– Zonily Jame
Nov 11 at 18:28
The answer by Robert Dresler is good, but remember the slow loading may also be caused by your server, and not your code.
– Zonily Jame
Nov 11 at 18:28
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
It takes some time than you get data from http request, so your table view will be without data until http request isn't completed. In this case I suggest writing code without any DispatchQueue.main.async etc.
Just write this to your viewDidLoad function:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: cellId)
tableView.separatorColor = .clear
tableView.backgroundColor = .clear
tableView.rowHeight = 53
getPrayersData() // this is moment when you probably haven't got data yet
}
Now create function for getPrayerData()
func getPrayersData() {
Alamofire.request(prayerUrl, method: .get).responseJSON { response in
// this is moment when request is completed
guard let data = response.data else {
print("Failed to fetch data:", response.error)
return
}
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
self.tableView.reloadData()
} catch {
print("Failed to decode: (error)")
}
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
It takes some time than you get data from http request, so your table view will be without data until http request isn't completed. In this case I suggest writing code without any DispatchQueue.main.async etc.
Just write this to your viewDidLoad function:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: cellId)
tableView.separatorColor = .clear
tableView.backgroundColor = .clear
tableView.rowHeight = 53
getPrayersData() // this is moment when you probably haven't got data yet
}
Now create function for getPrayerData()
func getPrayersData() {
Alamofire.request(prayerUrl, method: .get).responseJSON { response in
// this is moment when request is completed
guard let data = response.data else {
print("Failed to fetch data:", response.error)
return
}
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
self.tableView.reloadData()
} catch {
print("Failed to decode: (error)")
}
}
}
add a comment |
up vote
4
down vote
accepted
It takes some time than you get data from http request, so your table view will be without data until http request isn't completed. In this case I suggest writing code without any DispatchQueue.main.async etc.
Just write this to your viewDidLoad function:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: cellId)
tableView.separatorColor = .clear
tableView.backgroundColor = .clear
tableView.rowHeight = 53
getPrayersData() // this is moment when you probably haven't got data yet
}
Now create function for getPrayerData()
func getPrayersData() {
Alamofire.request(prayerUrl, method: .get).responseJSON { response in
// this is moment when request is completed
guard let data = response.data else {
print("Failed to fetch data:", response.error)
return
}
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
self.tableView.reloadData()
} catch {
print("Failed to decode: (error)")
}
}
}
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
It takes some time than you get data from http request, so your table view will be without data until http request isn't completed. In this case I suggest writing code without any DispatchQueue.main.async etc.
Just write this to your viewDidLoad function:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: cellId)
tableView.separatorColor = .clear
tableView.backgroundColor = .clear
tableView.rowHeight = 53
getPrayersData() // this is moment when you probably haven't got data yet
}
Now create function for getPrayerData()
func getPrayersData() {
Alamofire.request(prayerUrl, method: .get).responseJSON { response in
// this is moment when request is completed
guard let data = response.data else {
print("Failed to fetch data:", response.error)
return
}
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
self.tableView.reloadData()
} catch {
print("Failed to decode: (error)")
}
}
}
It takes some time than you get data from http request, so your table view will be without data until http request isn't completed. In this case I suggest writing code without any DispatchQueue.main.async etc.
Just write this to your viewDidLoad function:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(PrayerTimeViewCell.self, forCellReuseIdentifier: cellId)
tableView.separatorColor = .clear
tableView.backgroundColor = .clear
tableView.rowHeight = 53
getPrayersData() // this is moment when you probably haven't got data yet
}
Now create function for getPrayerData()
func getPrayersData() {
Alamofire.request(prayerUrl, method: .get).responseJSON { response in
// this is moment when request is completed
guard let data = response.data else {
print("Failed to fetch data:", response.error)
return
}
do {
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach({ (item) in
let shubuh = Prayer(prayerName: "Shubuh", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyNight"), prayerTime: item.fajr)
let dzuhur = Prayer(prayerName: "Dzuhur", prayerIcon: #imageLiteral(resourceName: "Sunny"), prayerTime: item.dhuhr)
let ashar = Prayer(prayerName: "Ashar", prayerIcon: #imageLiteral(resourceName: "PartlyCloudyDay"), prayerTime: item.asr)
let maghrib = Prayer(prayerName: "Maghrib", prayerIcon: #imageLiteral(resourceName: "Overcast"), prayerTime: item.maghrib)
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
})
self.tableView.reloadData()
} catch {
print("Failed to decode: (error)")
}
}
}
edited Nov 11 at 15:39
answered Nov 11 at 15:34
Robert Dresler
1,466218
1,466218
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250149%2fload-tableview-fast%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The answer by Robert Dresler is good, but remember the slow loading may also be caused by your server, and not your code.
– Zonily Jame
Nov 11 at 18:28