iOS CarPlay swift asynchronous request in templateApplicationScene

74 views Asked by At

I am trying to load some CPListItem's with data from an asynchronous call. Using dummy data this works fine (not called asyncronously)

My fetchAllFavourites methods runs correctly but nothing is displayed on the carPlay simulator.

import Foundation
import CarPlay

class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
    
    var interfaceController: CPInterfaceController?
    var stationItems: [CPListItem] = []
    
    func loadList(withStations stations: [CPListItem]) {

          let section = CPListSection(items: stations)
          let listTemplate = CPListTemplate(title: "Select a station",
                                            sections: [section])
          
          // Set the root template of the CarPlay interface
          // to the list template with a completion handler
          interfaceController?.setRootTemplate(listTemplate,
                                            animated: true) { success, error in
              
              // add anything you want after setting the root template
          }
      }
    
    func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) {
    
        FetchFavourites().fetchAllFavourites { stations in
            for station in stations {
                let testStation = CPListItem(text: station.type, detailText: station.adresse, image: UIImage(systemName: "BP"))
                
                testStation.accessoryType = .disclosureIndicator
                testStation.handler =  {  testStation, completion in
                    
                    print("Clicked")
                    completion()
                }
                self.stationItems.append(testStation)
            }
            self.loadList(withStations: self.stationItems)
        }
    }
}
0

There are 0 answers