From abfd365ce129ea2644b54d9d767ef1d4fc91f391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Thu, 25 Apr 2024 10:22:20 +0200 Subject: [PATCH] refactor: Use safeWrite() from CoreDB --- MailCore/API/Extension/Realm+SafeWrite.swift | 42 ------------------- MailCore/Cache/MailboxInfosManager.swift | 3 +- .../MailboxManager+Search.swift | 3 +- 3 files changed, 4 insertions(+), 44 deletions(-) delete mode 100644 MailCore/API/Extension/Realm+SafeWrite.swift diff --git a/MailCore/API/Extension/Realm+SafeWrite.swift b/MailCore/API/Extension/Realm+SafeWrite.swift deleted file mode 100644 index 11194679f..000000000 --- a/MailCore/API/Extension/Realm+SafeWrite.swift +++ /dev/null @@ -1,42 +0,0 @@ -/* - Infomaniak Mail - iOS App - Copyright (C) 2022 Infomaniak Network SA - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - */ - -import Foundation -import RealmSwift - -public extension Realm { - func uncheckedSafeWrite(_ block: () throws -> Void) throws { - if isInWriteTransaction { - try block() - } else { - try write(block) - } - } - - func safeWrite(_ block: () throws -> Void) throws { - #if DEBUG || TEST - dispatchPrecondition(condition: .notOnQueue(.main)) - #endif - - if isInWriteTransaction { - try block() - } else { - try write(block) - } - } -} diff --git a/MailCore/Cache/MailboxInfosManager.swift b/MailCore/Cache/MailboxInfosManager.swift index 4325d06a4..71e85bfd1 100644 --- a/MailCore/Cache/MailboxInfosManager.swift +++ b/MailCore/Cache/MailboxInfosManager.swift @@ -18,6 +18,7 @@ import Foundation import InfomaniakCore +import InfomaniakCoreDB import Realm import RealmSwift @@ -123,7 +124,7 @@ public final class MailboxInfosManager { public func removeMailboxesFor(userId: Int) { let realm = getRealm() let userMailboxes = realm.objects(Mailbox.self).where { $0.userId == userId } - try? realm.uncheckedSafeWrite { + try? realm.safeWrite { realm.delete(userMailboxes) } } diff --git a/MailCore/Cache/MailboxManager/MailboxManager+Search.swift b/MailCore/Cache/MailboxManager/MailboxManager+Search.swift index a803b2a8c..fb492fdaf 100644 --- a/MailCore/Cache/MailboxManager/MailboxManager+Search.swift +++ b/MailCore/Cache/MailboxManager/MailboxManager+Search.swift @@ -18,6 +18,7 @@ import Foundation import InfomaniakCore +import InfomaniakCoreDB import RealmSwift // MARK: - Search @@ -35,7 +36,7 @@ public extension MailboxManager { ) let realm = getRealm() - try? realm.uncheckedSafeWrite { + try? realm.safeWrite { realm.add(searchFolder, update: .modified) } return searchFolder