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)

)



my tableView










share|improve this question





















  • 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














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)

)



my tableView










share|improve this question





















  • 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












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)

)



my tableView










share|improve this question













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)

)



my tableView







ios swift uitableview grand-central-dispatch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 15:20









ferryawijayanto

507




507











  • 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




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












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)")








share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    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)")








    share|improve this answer


























      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)")








      share|improve this answer
























        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)")








        share|improve this answer














        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)")









        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 15:39

























        answered Nov 11 at 15:34









        Robert Dresler

        1,7781318




        1,7781318



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto