Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Observe changes to the HomeKit database


May 22, 2021 HomeKit App Development guide


Table of contents


Observe changes to the HomeKit database

Each Home has a HomeKit database. A s shown in the following image, the HomeKit database is securely synchronized with the iOS devices of home authorized users and the iOS devices of potential guests. To show users the latest data, your app needs to observe changes in the HomeKit database.

Observe changes to the HomeKit database

HomeKit proxy method

HomKit uses proxy design patterns to notify applications of changes to HomeKit objects. I n general, if your application calls a HomeKit method with completion parameters and the method is successfully called, the associated proxy message is sent to other HomeKit apps, whether they are installed on the same iOS device or on a remote iOS device. T hese apps can even run on guests' iOS devices. I f your app initiated a data change, but the proxy message is not sent to your app, adding code to the completion processing method and the associated proxy method to refresh the data and update the view becomes a must. I f the home layout changes significantly, reload all information about the home. W hen the program processing is complete, check that the method is successful before updating the app. Homkit also calls proxy methods to notify your application of changes in the state of the home network.

For example, the following illustration illustrates the process of using a proxy method: in response to a user's action, your application calls addRoomWithName:completeHandler: method, and no errors occur, the completion handler should update all views of home. I f successful, homeKit will send home:didAddRoom: message to the agent of homes in other apps. Therefore, you implement this home:didAddRoom: the method should also update all views of home.

Observe changes to the HomeKit database

The application can only accept proxy messages when the foretime is running. W hen your app is in the background, changes to the HomeKit database are not batched. T hat is, if your app is in the background, your app won't receive home:didAddRoom: messages when other apps successfully add a room to home. When your application runs in the foretime, your app will receive homeManagerDidUpdateHomes: A message that indicates that your application is reloading all the data.

Observe changes to the Homes collection

Set up the agent for home manager and implement the HMHomeManagerDelegate protocol, which can receive proxy messages when the primary home or home collection changes.

All applications need to implement homeManagerDidUpdateHomes: Method, which is called after the initial acquisition of homes is complete. F or the new home manager, the value of the primaryHome property is nil and the homes array is an empty array until the method is called. T he homeManager DidUpdateHomes method is also called when the application starts running in the foretime: the data changes when it is running in the background. The homeManager DidUpdateHomes: Method reloads all the data associated with homes.

Observe the changes in homes

1. Add the HMHomeManagerDelegate agent and homeManager properties to your class interface. The code is as follows:

@interface AppDelegate () @property (strong, nonatomic) HMHomeManager *homeManager;
@end

2. Create the home manager object and set its proxy

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.homeManager = [[HMHomeManager alloc] init];
self.homeManager.delegate = self;
return YES;
}

3. Implement the proxy method called when homes change. For example, if multiple view controllers show homes-related information, you can publish a change notification to update all views.

- (void)homeManagerDidUpdateHomes:(HMHomeManager *)manager {
// Send a notification to the other objects
[[NSNotificationCenter defaultCenter]
postNotificationName:@"UpdateHomesNotification"
object:self];
}
- (void)homeManagerDidUpdatePrimaryHome:(HMHomeManager
*)manager {
// Send a notification to the other objects
[[NSNotificationCenter defaultCenter]
postNotificationName:@"UpdatePrimaryHomeNotification"
object:self];
}

The view controller registers the change notification and performs the appropriate action.

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateHomes:)
name:@"UpdateHomesNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updatePrimaryHome:)
name:@"UpdatePrimaryHomeNotification" object:nil];

Observe the changes in individual home

The view controller that presents home information should act as a proxy for the home object and update the view of the view controller when the home changes.

Observe changes to a specific home object

1. Add the home proxy protocol to the class interface.

@interface HomeViewController () @end

2. Set up accessories agent

home.delegate = self;

3. Implement the HMHomeDelegate protocol

For example: implement home:didAddAccessory: and home:didRemoveAccessory: Method to update the view that shows the accessory. T he room property of the HMAccessory class allows you to obtain the room to which the accessory belongs. ( For accessories, the default room is the return value of the roomForEntireHome method.)

Bridge Note: When you add a bridge interface to home, the accessories at the bottom of the bridge interface are automatically added to home. Your agent will receive the home:didAddAccessory: message for each accessory after the bridge interface, but your agent will not receive the home:didAddAccessory: message for the bridge interface.

Observe the changes in the accessories

The status of the accessory can change at any time. A ccessories may not be available, can be removed, or may be closed. Please update the user interface to reflect changes in the status of accessories, especially if your app allows users to control accessories.

In these steps, let's assume that you've retrieved the accessory object from the HomeKit database, as described in Getting the Accessories in a Room.

Observe changes in individual accessories

Add an accessory agent protocol to the class interface.

@interface AccessoryViewController ()  
@end

Set up the agent for the accessory

accessory.delegate = self;

Implement the HMAccessory Delegate protocol

For example, perform AccessoryDidUpdateReachability: Method to enable or disable accessory control.

- (void)accessoryDidUpdateReachability:(HMAccessory *)accessory {
    if (accessory.reachable == YES) {
       // Can communicate with the accessory
    } else {
       // The accessory is out of range, turned off, etc
    }
}

If you show the service status and features of an accessory, follow these proxy methods to update its view accordingly:

accessoryDidUpdateServices:

accessory:service:didUpdateValueForCharacteristic:

For accessory services, see Accessing Services and Their Features .