Skip to content

Commit

Permalink
trying to fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
kvyatkovskys committed Apr 4, 2023
1 parent 7950546 commit 1276120
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
17 changes: 8 additions & 9 deletions Sources/KVKLogger/KVKLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ open class KVKLogger {
details: String?) {
let date = Date()
if isEnableSaveIntoDB {
let item = store.getNewItem()
item?.createdAt = date
item?.status = status ?? .info
item?.logType = logType
item?.type = type
item?.details = details
item?.items = items
item?.data = data
store.save()
let item = ItemLogProxy(createdAt: date,
data: data,
details: details,
items: items,
logType: logType,
status: status ?? .info,
type: type)
store.save(log: item)
}

if isDebugMode != false {
Expand Down
4 changes: 0 additions & 4 deletions Sources/KVKLogger/KVKLoggerProxyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ import SwiftUI

public struct KVKLoggerView: View {
private let persistenceContainer = KVKLogger.shared.store
@Environment (\.scenePhase) private var scenePhase

public init() {}

public var body: some View {
KVKLoggerProxyView()
.environment(\.managedObjectContext, persistenceContainer.viewContext)
.onChange(of: scenePhase) { (_) in
persistenceContainer.save()
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions Sources/KVKLogger/KVKModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ struct CurateContainer: Identifiable {
}
}

struct ItemLogProxy {
var createdAt: Date?
var data: Data?
var details: String?
var items: String?
var logType: KVKLogType?
var status: KVKStatus?
var type: ItemLogType?
}

final class ItemLog: NSManagedObject {

@NSManaged var createdAt_: Date?
Expand Down
21 changes: 12 additions & 9 deletions Sources/KVKLogger/KVKPersistenceСontroller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,25 @@ final class KVKPersistenceСontroller {
checkOldRecords()
}

func getNewItem() -> ItemLog? {
ItemLog(context: backgroundContext)
}

func save() {
func save(log: ItemLogProxy) {
// temporary checking a file
if let url = cacheDBURL, !FileManager.default.fileExists(atPath: url.path) {
debugPrint("Can't find DB in directory.")
return
}

guard backgroundContext.hasChanges && !backgroundContext.insertedObjects.isEmpty else { return }


backgroundContext.performAndWait { [weak self] in
guard let self else { return }
do {
try self?.backgroundContext.save()
let itemLog = ItemLog(context: self.backgroundContext)
itemLog.createdAt_ = log.createdAt
itemLog.data_ = log.data
itemLog.details_ = log.details
itemLog.items_ = log.items
itemLog.status_ = log.status?.rawValue
itemLog.logType_ = log.logType?.rawValue
itemLog.type_ = log.type?.rawValue
try self.backgroundContext.save()
} catch {
debugPrint("Could not save data. \(error), \(error.localizedDescription)")
}
Expand Down

0 comments on commit 1276120

Please sign in to comment.