Key Value API 為裝置、房間和自動化動作物件提供新的自訂資料管理 API。自訂資料的生命週期嚴格限定於相關聯物件的生命週期。舉例來說,與 Room 物件相關聯的自訂資料 (例如「Lucy 的房間」) 會自動刪除、移動或轉移 (跨結構) 該 Room 物件。
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 列鍵/值組合。