Key Value API มี API การจัดการข้อมูลที่กำหนดเองใหม่สำหรับออบเจ็กต์อุปกรณ์ ห้อง และระบบอัตโนมัติ วงจรการใช้งานของข้อมูลที่กำหนดเองจะจำกัดขอบเขตไว้ที่วงจรการใช้งานของออบเจ็กต์ที่เชื่อมโยงกับข้อมูลนั้น ตัวอย่างเช่น ข้อมูลที่กำหนดเอง (เช่น "ห้องของลูซี่") ที่เชื่อมโยงกับออบเจ็กต์ห้องจะถูกลบ ย้าย หรือโอน (ข้ามโครงสร้าง) โดยอัตโนมัติพร้อมกับออบเจ็กต์ห้อง
การเปลี่ยนแปลง 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 ที่ถูกต้อง โดยมีความยาวสูงสุด 1, 024 ตัว ค่าอาจเป็นสตริงว่างก็ได้
- แต่ละออบเจ็กต์สามารถมีคู่คีย์-ค่าได้ 10 แถว