From e1eac4cab0d45de478e99bfa51ad17029ff04501 Mon Sep 17 00:00:00 2001 From: Kviatkovskii Sergei Date: Sun, 26 Feb 2023 13:37:15 +0300 Subject: [PATCH] handling store db url --- Sources/KVKLogger/KVKLogger.swift | 16 ++++++++-------- Sources/KVKLogger/KVKLoggerProxyView.swift | 2 +- .../KVKPersistence\320\241ontroller.swift" | 6 ++++++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Sources/KVKLogger/KVKLogger.swift b/Sources/KVKLogger/KVKLogger.swift index c094a1a..d7c1da4 100644 --- a/Sources/KVKLogger/KVKLogger.swift +++ b/Sources/KVKLogger/KVKLogger.swift @@ -63,14 +63,14 @@ open class KVKLogger { details: String?) { let date = Date() if isEnableSaveIntoDB { - let item = ItemLog(context: store.backgroundContext) - item.createdAt = date - item.status_ = status?.rawValue - item.logType = logType - item.type = type - item.details = details - item.items = items - item.data = data + let item = store.getNewItem() + item?.createdAt = date + item?.status_ = status?.rawValue + item?.logType = logType + item?.type = type + item?.details = details + item?.items = items + item?.data = data store.save() } diff --git a/Sources/KVKLogger/KVKLoggerProxyView.swift b/Sources/KVKLogger/KVKLoggerProxyView.swift index d58568b..3ae4f9a 100644 --- a/Sources/KVKLogger/KVKLoggerProxyView.swift +++ b/Sources/KVKLogger/KVKLoggerProxyView.swift @@ -252,7 +252,7 @@ struct KVKLoggerView_Previews: PreviewProvider { newItem3.type = ItemLogType.network newItem3.logType = KVKLogType.print newItem3.items = "Test description network Test description network Test description network" - result.backgroundContext.saveContext() + result.save() return Group { KVKLoggerProxyView() KVKLoggerProxyView() diff --git "a/Sources/KVKLogger/KVKPersistence\320\241ontroller.swift" "b/Sources/KVKLogger/KVKPersistence\320\241ontroller.swift" index a8a6984..b8322e6 100644 --- "a/Sources/KVKLogger/KVKPersistence\320\241ontroller.swift" +++ "b/Sources/KVKLogger/KVKPersistence\320\241ontroller.swift" @@ -42,7 +42,13 @@ struct KVKPersistenceСontroller { backgroundContext = container.newBackgroundContext() } + func getNewItem() -> ItemLog? { + guard container.persistentStoreDescriptions.first?.url != nil else { return nil } + return ItemLog(context: backgroundContext) + } + func save() { + guard container.persistentStoreDescriptions.first?.url != nil else { return } backgroundContext.saveContext() }