AccountBackupDataAttribute: handle nil

This commit is contained in:
Peter 2019-05-16 20:35:56 +02:00
parent e62a476550
commit a55a097d54

View File

@ -930,18 +930,20 @@ public struct AccountBackupData: Codable, Equatable {
} }
public final class AccountBackupDataAttribute: AccountRecordAttribute, Equatable { public final class AccountBackupDataAttribute: AccountRecordAttribute, Equatable {
public let data: AccountBackupData public let data: AccountBackupData?
public init(data: AccountBackupData) { public init(data: AccountBackupData?) {
self.data = data self.data = data
} }
public init(decoder: PostboxDecoder) { public init(decoder: PostboxDecoder) {
self.data = try! JSONDecoder().decode(AccountBackupData.self, from: decoder.decodeDataForKey("data")!) self.data = try? JSONDecoder().decode(AccountBackupData.self, from: decoder.decodeDataForKey("data") ?? Data())
} }
public func encode(_ encoder: PostboxEncoder) { public func encode(_ encoder: PostboxEncoder) {
encoder.encodeData(try! JSONEncoder().encode(self.data), forKey: "data") if let data = self.data, let serializedData = try? JSONEncoder().encode(data) {
encoder.encodeData(serializedData, forKey: "data")
}
} }
public static func ==(lhs: AccountBackupDataAttribute, rhs: AccountBackupDataAttribute) -> Bool { public static func ==(lhs: AccountBackupDataAttribute, rhs: AccountBackupDataAttribute) -> Bool {