键值 API 为设备、会议室和自动化对象提供了新的自定义数据管理 API。自定义数据的生命周期严格限定于与其关联的对象的生命周期。例如,与会议室对象关联的自定义数据(如“Lucy 的会议室”)将随会议室对象自动删除、移动或转移(跨结构)。
Swift API 更改以
CustomAppData
控制器为中心,并为自定义应用
数据所属的对象提供扩展,包括标准的创建、读取、更新和删除
(CRUD) 功能。
设备示例
guard let device = home.devices.first(where: { $0.name == deviceName }) else { return }
var response = try await device.customAppData[key]
print("Before: \(response)")
try await device.customAppData.update(key: key, value: value)
response = try await device.customAppData[key]
print("After: \(response)")
try await device.customAppData.delete(key)
会议室示例
guard let room = home.rooms.first(where: { $0.name == roomName }) else { return }
try await room.customAppData.update(key: "nickname", value: "Lucy's Room")
let response = try await room.customAppData["nickname"]
try await room.customAppData.delete("nickname")
自动化示例
guard let automation = home.automations.first(where: { $0.name == automationName }) else { return }
var response = try await automation.customAppData[key]
print("Before: \(response)")
try await automation.customAppData.update(key: key, value: value)
response = try await automation.customAppData[key]
print("After: \(response)")
try await automation.customAppData.delete(key)
限制
- 键必须是包含有效 UTF-8 字符的字符串,且长度上限为 128。键可以是空字符串。
- 值必须是包含有效 UTF-8 字符的字符串,且长度上限为 1024。值可以是空字符串。
- 每个对象可以有 10 行键值对。