Does pull-to-refresh change the value of an array?

I am working on updating on iOS using Swift.
I have an array with city names, cityNames = ["Chicago", "New York City"]
I implemented an update to update temperature data from the Internet. Therefore, every time I run pull-to-refresh, it goes to the Internet and gets the temperature for each city in the cityNames array.
Here is the update code for

 var weatherDetail = [Weather]() // Pull to refresh func refreshData() { var cityNames = [String]() for (index, _) in weatherDetail.enumerate() { let info = weatherDetail[index] cityNames.append(info.cityName) } print(cityNames) weatherDetail.removeAll() for city in cityNames { self.forwardGeocoding(city) } weatherCityTable.reloadData() refreshControl.endRefreshing() } 

In the above code, weatherDetail is an array of the model (I'm not sure how to express this, but Weather is a model that contains the names of cities, temperature, sunrise time, high / low temperature.
forwardGeocoding is a function that receives geographic coordination for a city and then sends a request for meteorological data for that city.
Renovation work, the problem I am facing, the first 2.3 times when I pull, works without problems. But as I pull more time, the array will suddenly change to cityNames = ["Chicago", "Chicago"]
Thank you for your help, please let me know if you need more information.
UPDATE:
I deleted weatherDetail.removeAll() , try just adding the same data to the array. After the update, he displays "Chicago", "New York City", "Chicago", "Chicago" . If I update it more times, it prints something like "Chicago", "New York City", "Chicago", "Chicago", "Chicago", "Chicago"

+7
arrays ios uitableview swift pull-to-refresh
source share
3 answers

Is forwardGeocoding synchronous? When is weatherDetail installed / updated?

It seems to me that you have some kind of synchronization problem that occurs here, probably aggravated by the delay.

+5
source share

Using enumerate() and append() for this is not a good approach; there is a more elegant and reliable way to achieve this:

 let cityNames:[String] = weatherDetail.map { weather -> String in weather.cityName } 

Or just write:

 let cityNames:[String] = weatherDetail.map { $0.cityName } 
+4
source share

If the names of the cities are repeated twice, this means that the information in the weatherDetail array weatherDetail repeated twice. Try printing weatherDetail before printing cityNames . If weatherDetail repeated twice, you should find code that adds the same Weather objects twice and excludes it.

For testing purposes, find all places that modify weatherDetail , and before these statements put weatherDetail.removeAll() . If this fixes your problem, search for code that adds redundant information to weatherDetail .

+3
source share

All Articles