Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBX-2864 mindboxPerformAndWait #273

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Mindbox/Database/MBDatabaseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> = CDEvent.fetchRequest(by: transactionId)
guard let entity = try findEvent(by: request) else {
Expand All @@ -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> = CDEvent.fetchRequest(by: event.transactionId)
guard let entity = try findEvent(by: request) else {
Expand All @@ -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 {
Expand All @@ -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> = CDEvent.fetchRequestForSend(lifeLimitDate: lifeLimitDate, retryDeadLine: retryDeadline)
request.fetchLimit = fetchLimit
Expand Down Expand Up @@ -149,7 +149,7 @@ class MBDatabaseRepository {
func countDeprecatedEvents() throws -> Int {
let context = persistentContainer.newBackgroundContext()
let request: NSFetchRequest<CDEvent> = 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)
Expand All @@ -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()
Expand All @@ -177,7 +177,7 @@ class MBDatabaseRepository {
@discardableResult
func countEvents() throws -> Int {
let request: NSFetchRequest<CDEvent> = 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 {
Expand Down Expand Up @@ -206,7 +206,7 @@ class MBDatabaseRepository {
}

private func delete(by request: NSFetchRequest<CDEvent>, 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)
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import CoreData

public extension NSManagedObjectContext {
func performAndWait<T>(_ block: () throws -> T) rethrows -> T {
func mindboxPerformAndWait<T>(_ block: () throws -> T) rethrows -> T {
return try _performAndWaitHelper(
fn: performAndWait, execute: block, rescue: { throw $0 }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,7 +84,7 @@ public class MBLoggerCoreDataManager {
}

public func getFirstLog() throws -> LogMessage? {
try context.performAndWait {
try context.mindboxPerformAndWait {
let fetchRequest = NSFetchRequest<CDLogMessage>(entityName: Constants.model)
fetchRequest.predicate = NSPredicate(value: true)
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)]
Expand All @@ -100,7 +100,7 @@ public class MBLoggerCoreDataManager {
}

public func getLastLog() throws -> LogMessage? {
try context.performAndWait {
try context.mindboxPerformAndWait {
let fetchRequest = NSFetchRequest<CDLogMessage>(entityName: Constants.model)
fetchRequest.predicate = NSPredicate(value: true)
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: false)]
Expand All @@ -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<CDLogMessage>(entityName: Constants.model)
fetchRequest.predicate = NSPredicate(format: "timestamp >= %@ AND timestamp <= %@",
from as NSDate,
Expand All @@ -132,7 +132,7 @@ public class MBLoggerCoreDataManager {
}

public func delete() throws {
try context.performAndWait {
try context.mindboxPerformAndWait {
let request = NSFetchRequest<NSFetchRequestResult>(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
Expand All @@ -152,7 +152,7 @@ public class MBLoggerCoreDataManager {
}

public func deleteAll() throws {
try context.performAndWait {
try context.mindboxPerformAndWait {
let request = NSFetchRequest<NSFetchRequestResult>(entityName: Constants.model)
request.includesPropertyValues = false
let results = try context.fetch(request)
Expand Down