1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-09-25 03:33:01 +02:00

EC-842 Delete items that are not included in the sync to the watch (#2242)

This commit is contained in:
Federico Maccaroni 2022-12-15 13:03:34 -03:00 committed by GitHub
parent f6895a0733
commit a19b5c4e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,25 +41,28 @@ extension CipherService: CipherServiceProtocol {
}
}
func saveCiphers(_ ciphers: [Cipher], completionHandler: @escaping () -> Void){
dbHelper.insertBatch("CipherEntity", items: ciphers) { item, context in
guard let cipher = item as! Cipher? else { return [:] }
let c = cipher.toCipherEntity(moContext: context)
guard let data = try? JSONEncoder().encode(c) else
{
Log.e("Error converting to data")
return [:]
}
guard let cipherDict = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any ] else
{
Log.e("Error converting json data to dict")
return [:]
}
return cipherDict
func saveCiphers(_ ciphers: [Cipher], completionHandler: @escaping () -> Void) {
let cipherIds = ciphers.map { $0.id }
deleteAll(ciphers[0].userId, notIn: cipherIds) {
self.dbHelper.insertBatch("CipherEntity", items: ciphers) { item, context in
guard let cipher = item as! Cipher? else { return [:] }
let c = cipher.toCipherEntity(moContext: context)
guard let data = try? JSONEncoder().encode(c) else
{
Log.e("Error converting to data")
return [:]
}
guard let cipherDict = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any ] else
{
Log.e("Error converting json data to dict")
return [:]
}
return cipherDict
} completionHandler: {
completionHandler()
} completionHandler: {
completionHandler()
}
}
}
@ -67,4 +70,14 @@ extension CipherService: CipherServiceProtocol {
let predicate = withUserId == nil ? nil : NSPredicate(format: "userId = %@", withUserId!)
dbHelper.deleteAll("CipherEntity", predicate: predicate, completionHandler: completionHandler)
}
func deleteAll(_ withUserId: String? = nil, notIn: [String], completionHandler: @escaping () -> Void) {
var predicateList : [NSPredicate] = []
if let userId = withUserId {
predicateList.append(NSPredicate(format: "userId = %@", userId))
}
predicateList.append(NSPredicate(format: "NOT (id in %@)", notIn))
let predicate = NSCompoundPredicate(type: .and, subpredicates: predicateList)
dbHelper.deleteAll("CipherEntity", predicate: predicate, completionHandler: completionHandler)
}
}