diff --git a/Mindbox/Database/MBDatabaseRepository.swift b/Mindbox/Database/MBDatabaseRepository.swift index e92578c0..bcb008c1 100644 --- a/Mindbox/Database/MBDatabaseRepository.swift +++ b/Mindbox/Database/MBDatabaseRepository.swift @@ -68,7 +68,7 @@ class MBDatabaseRepository { // MARK: - CRUD operations func create(event: Event) throws { - try context.performAndWait { + try context.mindboxPerformAndWait { let entity = CDEvent(context: context) entity.transactionId = event.transactionId entity.timestamp = Date().timeIntervalSince1970 @@ -80,7 +80,7 @@ class MBDatabaseRepository { } func read(by transactionId: String) throws -> CDEvent? { - try context.performAndWait { + try context.mindboxPerformAndWait { Logger.common(message: "Reading event with transactionId: \(transactionId)", level: .info, category: .database) let request: NSFetchRequest = CDEvent.fetchRequest(by: transactionId) guard let entity = try findEvent(by: request) else { @@ -93,7 +93,7 @@ class MBDatabaseRepository { } func update(event: Event) throws { - try context.performAndWait { + try context.mindboxPerformAndWait { Logger.common(message: "Updating event with transactionId: \(event.transactionId)", level: .info, category: .database) let request: NSFetchRequest = CDEvent.fetchRequest(by: event.transactionId) guard let entity = try findEvent(by: request) else { @@ -106,7 +106,7 @@ class MBDatabaseRepository { } func delete(event: Event) throws { - try context.performAndWait { + try context.mindboxPerformAndWait { Logger.common(message: "Deleting event with transactionId: \(event.transactionId)", level: .info, category: .database) let request = CDEvent.fetchRequest(by: event.transactionId) guard let entity = try findEvent(by: request) else { @@ -119,7 +119,7 @@ class MBDatabaseRepository { } func query(fetchLimit: Int, retryDeadline: TimeInterval = 60) throws -> [Event] { - try context.performAndWait { + try context.mindboxPerformAndWait { Logger.common(message: "Quering events with fetchLimit: \(fetchLimit)", level: .info, category: .database) let request: NSFetchRequest = CDEvent.fetchRequestForSend(lifeLimitDate: lifeLimitDate, retryDeadLine: retryDeadline) request.fetchLimit = fetchLimit @@ -149,7 +149,7 @@ class MBDatabaseRepository { func countDeprecatedEvents() throws -> Int { let context = persistentContainer.newBackgroundContext() let request: NSFetchRequest = CDEvent.deprecatedEventsFetchRequest(lifeLimitDate: lifeLimitDate) - return try context.performAndWait { + return try context.mindboxPerformAndWait { Logger.common(message: "Counting deprecated elements", level: .info, category: .database) do { let count = try context.count(for: request) @@ -167,7 +167,7 @@ class MBDatabaseRepository { let eraseRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) infoUpdateVersion = nil installVersion = nil - try context.performAndWait { + try context.mindboxPerformAndWait { try context.execute(eraseRequest) try saveEvent(withContext: context) try countEvents() @@ -177,7 +177,7 @@ class MBDatabaseRepository { @discardableResult func countEvents() throws -> Int { let request: NSFetchRequest = CDEvent.countEventsFetchRequest() - return try context.performAndWait { + return try context.mindboxPerformAndWait { Logger.common(message: "Events count limit: \(limit)", level: .info, category: .database) Logger.common(message: "Counting events...", level: .info, category: .database) do { @@ -206,7 +206,7 @@ class MBDatabaseRepository { } private func delete(by request: NSFetchRequest, withContext context: NSManagedObjectContext) throws { - try context.performAndWait { + try context.mindboxPerformAndWait { Logger.common(message: "Finding elements to remove", level: .info, category: .database) let events = try context.fetch(request) @@ -275,7 +275,7 @@ private extension MBDatabaseRepository { store.metadata[key.rawValue] = value persistentContainer.persistentStoreCoordinator.setMetadata(store.metadata, for: store) do { - try context.performAndWait { + try context.mindboxPerformAndWait { try saveContext(context) Logger.common(message: "Did save metadata of \(key.rawValue) to: \(String(describing: value))", level: .info, category: .database) } } catch { diff --git a/MindboxLogger/Shared/Extensions/NSManagedObjectContext+Extension.swift b/MindboxLogger/Shared/Extensions/NSManagedObjectContext+Extension.swift index ac897371..842e1712 100644 --- a/MindboxLogger/Shared/Extensions/NSManagedObjectContext+Extension.swift +++ b/MindboxLogger/Shared/Extensions/NSManagedObjectContext+Extension.swift @@ -10,7 +10,7 @@ import Foundation import CoreData public extension NSManagedObjectContext { - func performAndWait(_ block: () throws -> T) rethrows -> T { + func mindboxPerformAndWait(_ block: () throws -> T) rethrows -> T { return try _performAndWaitHelper( fn: performAndWait, execute: block, rescue: { throw $0 } ) diff --git a/MindboxLogger/Shared/LoggerRepository/MBLoggerCoreDataManager.swift b/MindboxLogger/Shared/LoggerRepository/MBLoggerCoreDataManager.swift index 21274eb1..3a2b62de 100644 --- a/MindboxLogger/Shared/LoggerRepository/MBLoggerCoreDataManager.swift +++ b/MindboxLogger/Shared/LoggerRepository/MBLoggerCoreDataManager.swift @@ -75,7 +75,7 @@ public class MBLoggerCoreDataManager { try delete() } - try self.context.performAndWait { + try self.context.mindboxPerformAndWait { let entity = CDLogMessage(context: self.context) entity.message = message entity.timestamp = timestamp @@ -84,7 +84,7 @@ public class MBLoggerCoreDataManager { } public func getFirstLog() throws -> LogMessage? { - try context.performAndWait { + try context.mindboxPerformAndWait { let fetchRequest = NSFetchRequest(entityName: Constants.model) fetchRequest.predicate = NSPredicate(value: true) fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)] @@ -100,7 +100,7 @@ public class MBLoggerCoreDataManager { } public func getLastLog() throws -> LogMessage? { - try context.performAndWait { + try context.mindboxPerformAndWait { let fetchRequest = NSFetchRequest(entityName: Constants.model) fetchRequest.predicate = NSPredicate(value: true) fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: false)] @@ -116,7 +116,7 @@ public class MBLoggerCoreDataManager { } public func fetchPeriod(_ from: Date, _ to: Date) throws -> [LogMessage] { - try context.performAndWait { + try context.mindboxPerformAndWait { let fetchRequest = NSFetchRequest(entityName: Constants.model) fetchRequest.predicate = NSPredicate(format: "timestamp >= %@ AND timestamp <= %@", from as NSDate, @@ -132,7 +132,7 @@ public class MBLoggerCoreDataManager { } public func delete() throws { - try context.performAndWait { + try context.mindboxPerformAndWait { let request = NSFetchRequest(entityName: Constants.model) let count = try context.count(for: request) let limit: Double = (Double(count) * 0.1).rounded() // 10% percent of all records should be removed @@ -152,7 +152,7 @@ public class MBLoggerCoreDataManager { } public func deleteAll() throws { - try context.performAndWait { + try context.mindboxPerformAndWait { let request = NSFetchRequest(entityName: Constants.model) request.includesPropertyValues = false let results = try context.fetch(request)