Thursday, September 11, 2025
HomeiOS Developmentios - Swift: How look forward to all my completion handlers to...

ios – Swift: How look forward to all my completion handlers to finish earlier than updating printed variable


Im at the moment utilizing the GooglePlaces API to seek for close by eating places. GooglePlacesAPI is bizarre such that the return worth of the search provides a GMSPlace object that has a .images property that solely provides metaData. Because of this, theres a separate api name the place I’ve to loop by means of every of these metadata objects and name getPhotoFromMetaData.

See documentation right here

That is the code I’ve up to now. The cardModels is a view mannequin that is being watch my UI and updates the cardstack. Can somebody present a extra environment friendly and proper manner to do that? The append conduct is making my UI truly append a card to the highest of the stack visually each time. My supposed conduct is that I need all of the api calls to fully end. Then use the entire information I bought kind them and make 1 single replace to the cardModels variable. I assume that may take away this bizarre visible stacking of every card loading.

import Basis
import GooglePlaces

class CardsViewModel: ObservableObject {
    @Printed var cardModels = [CardModel]()
    
    init() {
        var fashions = [CardModel]()
        GooglePlacesAPI().performNearbyRestaurantSearch { gmsplaces in
            gmsplaces.forEach { place in
                GooglePlacesAPI().getPhotoFromMetaData(id: place.placeID!) { picture in
                    DispatchQueue.fundamental.async {
                        self.cardModels.append(CardModel(restaurant: RestaurantModel(
                            id: place.placeID,
                            title: place.title,
                            uiimages: [image]
                        )))
                    }
                }
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments