Swiftgram/Postbox/DeviceContactImportInfoTable.swift
2018-03-12 22:52:35 +04:00

32 lines
1.0 KiB
Swift

import Foundation
public protocol DeviceContactImportIdentifier: PostboxCoding {
var key: ValueBoxKey { get }
}
final class DeviceContactImportInfoTable: Table {
static func tableSpec(_ id: Int32) -> ValueBoxTable {
return ValueBoxTable(id: id, keyType: .binary)
}
func get(_ identifier: DeviceContactImportIdentifier) -> PostboxCoding? {
if let value = self.valueBox.get(self.table, key: identifier.key), let object = PostboxDecoder(buffer: value).decodeRootObject() {
return object
} else {
return nil
}
}
func set(_ identifier: DeviceContactImportIdentifier, value: PostboxCoding?) {
if let value = value {
let encoder = PostboxEncoder()
encoder.encodeRootObject(value)
withExtendedLifetime(encoder, {
self.valueBox.set(self.table, key: identifier.key, value: encoder.readBufferNoCopy())
})
} else {
self.valueBox.remove(self.table, key: identifier.key)
}
}
}