I'm currently working on bridging some HomeKit functions to React Native. I need to be able to convert my array of HMHome
objects to be interpretable to React Native. I assume I'll need to use RCTConvert
, but I'm a bit flustered.
I'm subscribing to an event where the user's HMHome
s are ready to be returned as seen below in HomeKitPage.js
.
this.subscription = RNHomeKitEmitter.addListener(
'homesReady',
(homes) => console.log(homes)
);
My RNHomeKit.m
is emitting the homesReady
event and returns the list of homes as seen below.
- (void)homeManagerDidUpdateHomes:(HMHomeManager *)myHomeManager {
RCTLog(@"HOMEMANAGERDIDUPDATEHOMES");
RCTLog(@"PRIMARY HOME: %@", self.myHomeManager.primaryHome);
RCTLog(@"MY LIST OF HOMES: %@", self.myHomeManager.homes);
[self sendEventWithName:@"homesReady" body:@{@"homes": self.myHomeManager.homes}];
}
The output for My List of Homes from RCTLog is:
MY LIST OF HOMES: (
"[ name = Matt's Awesome Home, primary : Yes ]"
)
So my output seen by the JavaScript console from the EventEmitter is an array where the index is the user's number of homes, and each object is null
(as expected).
How can I convert this into an array of JSON objects useable by my JavaScript?
This can't be the ideal way to do it, but it works:
#RNHomeKit.m
for (HMHome *home in self.myHomeManager.homes) {
NSDictionary *homeObject = @{
@"name": home.name,
@"isPrimary": [NSNumber numberWithBool:home.primary]
};
[homesArray addObject: homeObject];
};
[self sendEventWithName:@"homesReady" body:@{@"homes": homesArray}];
#HomeKitPage.js
RNHomeKit.createHome();
this.subscription = RNHomeKitEmitter.addListener(
'homesReady',
(response) => this.setState({ homes: response.homes })
);
}