2022-10-03 16:52:00 -07:00
|
|
|
//
|
|
|
|
|
// UpdateCoreData.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Copyright(c) Garth Vander Houwen 10/3/22.
|
2022-10-03 20:13:33 -07:00
|
|
|
|
2022-10-03 16:52:00 -07:00
|
|
|
import CoreData
|
|
|
|
|
|
|
|
|
|
public func clearPositions(destNum: Int64, context: NSManagedObjectContext) -> Bool {
|
|
|
|
|
|
|
|
|
|
let fetchNodeInfoRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "NodeInfoEntity")
|
|
|
|
|
fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(destNum))
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
|
|
let fetchedNode = try context.fetch(fetchNodeInfoRequest) as! [NodeInfoEntity]
|
|
|
|
|
|
|
|
|
|
let newPostions = [PositionEntity]()
|
|
|
|
|
fetchedNode[0].positions? = NSOrderedSet(array: newPostions)
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try context.save()
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
|
context.rollback()
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
|
print("💥 Fetch NodeInfoEntity Error")
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-10-03 20:13:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func clearTelemetry(destNum: Int64, metricsType: Int32, context: NSManagedObjectContext) -> Bool {
|
|
|
|
|
|
|
|
|
|
let fetchNodeInfoRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "NodeInfoEntity")
|
|
|
|
|
fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(destNum))
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
|
|
let fetchedNode = try context.fetch(fetchNodeInfoRequest) as! [NodeInfoEntity]
|
|
|
|
|
|
|
|
|
|
let emptyTelemetry = [TelemetryEntity]()
|
|
|
|
|
fetchedNode[0].telemetries? = NSOrderedSet(array: emptyTelemetry)
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try context.save()
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
|
context.rollback()
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-10-03 16:52:00 -07:00
|
|
|
|
2022-10-03 20:13:33 -07:00
|
|
|
} catch {
|
|
|
|
|
print("💥 Fetch NodeInfoEntity Error")
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-10-03 16:52:00 -07:00
|
|
|
}
|
2022-10-03 21:19:10 -07:00
|
|
|
|
2022-12-17 23:53:06 -08:00
|
|
|
public func deleteChannelMessages(channel: ChannelEntity, context: NSManagedObjectContext) {
|
2022-11-24 23:25:44 -08:00
|
|
|
do {
|
2023-01-06 00:56:44 -08:00
|
|
|
let objects = channel.allPrivateMessages// try context.fetch(fetchChannelMessagesRequest) as! [NSManagedObject]
|
2022-12-03 01:08:06 -08:00
|
|
|
for object in objects {
|
|
|
|
|
context.delete(object)
|
|
|
|
|
}
|
2022-11-24 23:25:44 -08:00
|
|
|
try context.save()
|
|
|
|
|
} catch let error as NSError {
|
|
|
|
|
print("Error: \(error.localizedDescription)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 00:22:51 -08:00
|
|
|
public func deleteUserMessages(user: UserEntity, context: NSManagedObjectContext) {
|
|
|
|
|
|
|
|
|
|
do {
|
2023-01-06 00:56:44 -08:00
|
|
|
let objects = user.messageList//try context.fetch(fetchUserMessagesRequest) as! [NSManagedObject]
|
2022-12-03 01:08:06 -08:00
|
|
|
for object in objects {
|
|
|
|
|
context.delete(object)
|
|
|
|
|
}
|
2022-11-25 00:22:51 -08:00
|
|
|
try context.save()
|
|
|
|
|
} catch let error as NSError {
|
|
|
|
|
print("Error: \(error.localizedDescription)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 21:19:10 -07:00
|
|
|
public func clearCoreDataDatabase(context: NSManagedObjectContext) {
|
|
|
|
|
|
|
|
|
|
let persistenceController = PersistenceController.shared.container
|
|
|
|
|
for i in 0...persistenceController.managedObjectModel.entities.count-1 {
|
|
|
|
|
let entity = persistenceController.managedObjectModel.entities[i]
|
2022-10-06 08:56:15 -07:00
|
|
|
let query = NSFetchRequest<NSFetchRequestResult>(entityName: entity.name!)
|
|
|
|
|
let deleteRequest = NSBatchDeleteRequest(fetchRequest: query)
|
|
|
|
|
|
2022-11-24 23:25:44 -08:00
|
|
|
do {
|
|
|
|
|
try context.executeAndMergeChanges(using: deleteRequest)
|
2022-11-25 00:26:13 -08:00
|
|
|
} catch let error as NSError {
|
2022-11-24 23:25:44 -08:00
|
|
|
print(error)
|
2022-11-25 00:26:13 -08:00
|
|
|
}
|
2022-10-03 21:19:10 -07:00
|
|
|
}
|
|
|
|
|
}
|