homeManager.homes.count delay?

I'm having a problem where occasionally, when the homeManagerDidUpdateHomes returns, I check for the home count and get zero. My code checks for this – in case the user has not set up any Home – and sends an alert. But the alert is being sent even when there IS a home. This happens maybe 1 time 10.


    func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
        let homeCount: Int = homeManager.homes.count
        if homeCount == 0 {
            let alert = UIAlertController(title: "There are no Homes!", message: "Please create a home", preferredStyle: .alert)
            let action = UIAlertAction(title: "OK", style: .default) { _ in
                print("Go Use HomeKit to set up your home")
                return
            }
            alert.addAction(action)
            self.present(alert, animated: true) { }
        }

        for home in homeManager.homes as [HMHome]{
//  Look for accessory, service, characteristic


Everything continues to work fine afterwards despite the apparent zero count, so I'm thinking it's a timing problem of some sort. Am I **** something worng? Should I add a brief delay to ensure the homes.count is up to date?


Now that I think about it, this problem may only be occurring when my app first opens, which causes a call to the homeManager. Once the app is up and running, I don't think the error occurs in subsequent calls to the homeManager.

Replies

@waynehend Did you figure out why is the delay for homes.count ?

Sorry, I didn't see your response until just now.


No, I never did figure out this issue. I removed the code that sent an alert when no homes were found, and so I wouldn't be seeing any problem one way or the other.