here is relevant for my question part of my code and it works almost fine, except one thing. The data is always sorted randomly. About code: i should receive data (name of CoffeeShop and .jpg) from firebase and display the foto in tableView
var coffeeShopLists: [CoffeeShopList] = []
struct CoffeeShopList {
let name: String
let shopImage: UIImage
}
func loadShops () {
coffeeShopLists = []
db.collection("coffeeShop").getDocuments { (querySnapshot, error) in
if let e = error {
print("Error\(e)")
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
print(doc.data())
if let shop = data["name"] as? String {
print(shop)
self.getFoto(named: shop)
// self.coffeeShopLists.append(shopList) //retrieving Data from firebase
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
}
}
}
func getFoto (named: String) {
let storage = Storage.storage().reference().child("CoffeeShops/\(named).jpg")
storage.getData(maxSize: 1 * 1024 * 1024) {data, error in
if let error = error {
return print(error)
} else {
if let image = UIImage(data: data!) {
let newList = CoffeeShopList(name: named, shopImage: image)
self.coffeeShopLists.append(newList)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "CoffeeShopCell", bundle: nil), forCellReuseIdentifier: "ReusableCell")
loadShops()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! CoffeeShopCell
cell.shopImage.image = coffeeShopLists[indexPath.row].shopImage
return cell
}
How i said: it works. but it should be sorted how it sorted in firebase collection.But its not. And sorry for my english.(
after editing code like in comments bellow ordering by Position.Here is the result of print statements:
["position": 1, "name": soren]
soren
["position": 2, "name": ramozoti]
ramozoti
["position": 3, "name": coffeesoul]
coffeesoul //until this part the result is always the same(correct) and it should be by this order. But the results bellow is always in random order. That part belong to function getFoto()
coffeesoul
[CoffeeShop.CoffeeShopList(name: "coffeesoul", shopImage: <UIImage:0x600000e17600 anonymous {1080, 1080}>)]
ramozoti
[CoffeeShop.CoffeeShopList(name: "coffeesoul", shopImage: <UIImage:0x600000e17600 anonymous {1080, 1080}>), CoffeeShop.CoffeeShopList(name: "ramozoti", shopImage: <UIImage:0x600000e17840 anonymous {621, 621}>)]
soren
[CoffeeShop.CoffeeShopList(name: "coffeesoul", shopImage: <UIImage:0x600000e17600 anonymous {1080, 1080}>), CoffeeShop.CoffeeShopList(name: "ramozoti", shopImage: <UIImage:0x600000e17840 anonymous {621, 621}>), CoffeeShop.CoffeeShopList(name: "soren", shopImage: <UIImage:0x600000e10000 anonymous {1080, 1080}>)]
the struct feels each time by the random order
So, im on the right way. The last question to finish it:How i can sort this struct by the third value
[CoffeeShop.CoffeeShopList(name: "ramozoti", shopImage: Optional(<UIImage:0x600003b27de0 anonymous {621, 621}>), position: 2), CoffeeShop.CoffeeShopList(name: "soren", shopImage: Optional(<UIImage:0x600003b34480 anonymous {1080, 1080}>), position: 1), CoffeeShop.CoffeeShopList(name: "coffeesoul", shopImage: Optional(<UIImage:0x600003b31b00 anonymous {1080, 1080}>), position: 3)]
This is it, its working now. i made extra function
func sort () {
sortedShopLists = coffeeShopLists.sorted {$0.position < $1.position}
}
and passt it into getFoto() function after self.coffeeShopLists.append(newList)
maybe it was somewhere more elegant way to solve my problem but i found just this.