Categories
Mastering Development

tvOS: Collectionview is not showing data from the REST API response?

I am following this blog for implementing the Collectionview in my tvOS application. In this blog the data is adding on the CityViewDatasource class. Static datas are added on this blog like below:

        Cities.Clear();
        Cities.Add(new CityInfo("City01.jpg", "Houses by Water", false));
        Cities.Add(new CityInfo("City02.jpg", "Turning Circle", true));
        Cities.Add(new CityInfo("City03.jpg", "Skyline at Night", true));
        Cities.Add(new CityInfo("City04.jpg", "Golden Gate Bridge", true));
        Cities.Add(new CityInfo("City05.jpg", "Roads by Night", true));
        Cities.Add(new CityInfo("City06.jpg", "Church Domes", true));
        Cities.Add(new CityInfo("City07.jpg", "Mountain Lights", true));
        Cities.Add(new CityInfo("City08.jpg", "City Scene", false));

I need to change the static data into dynamic by calling a REST API like below:

 try
        {
            HttpClient client = new HttpClient();
            var response = await client.GetAsync("My REST API");
            if (response.IsSuccessStatusCode)
            {
                var Response = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
                if (!string.IsNullOrWhiteSpace(Response.ToString()))
                {
                    var category = JsonConvert.DeserializeObject<VideoList>(Response.ToString());
                    Cities.Clear();
                    for (int i = 0; i < category.webContentHBList.Count; i++)
                    {
                        Cities.Add(new CityInfo(category.webContentHBList[i].ImageUrl, category.webContentHBList[i].title, true));
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine("exception:>>" + e);
        }

But when I run the project no data is coming on the UI, only a blank screen is showing. No errors are showing in the output window.

I have included the service domains on the info.plist under NSAppTransportSecurity. The REST API calling is success and I able to get the count of items. I have added a sample project here for the reference.

Leave a Reply

Your email address will not be published. Required fields are marked *