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.