Meshtastic-Apple/Meshtastic/Persistence/QueryCoreData.swift

57 lines
1.6 KiB
Swift
Raw Normal View History

2023-01-16 17:40:28 -08:00
//
// QueryCoreData.swift
// Meshtastic
//
// Created(c) Garth Vander Houwen 1/16/23.
//
import CoreData
public func getNodeInfo(id: Int64, context: NSManagedObjectContext) -> NodeInfoEntity? {
2023-01-31 10:50:17 -08:00
let fetchNodeInfoRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "NodeInfoEntity")
fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
do {
let fetchNodeInfo = try context.fetch(fetchNodeInfoRequest) as! [NodeInfoEntity]
if fetchNodeInfo.count == 1 {
return fetchNodeInfo[0]
}
} catch {
return nil
2023-01-31 10:50:17 -08:00
}
return nil
2023-01-31 10:50:17 -08:00
}
public func getUser(id: Int64, context: NSManagedObjectContext) -> UserEntity {
let fetchUserRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserEntity")
fetchUserRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
do {
let fetchedUser = try context.fetch(fetchUserRequest) as! [UserEntity]
if fetchedUser.count == 1 {
return fetchedUser[0]
}
} catch {
return UserEntity(context: context)
}
return UserEntity(context: context)
}
2023-01-16 17:40:28 -08:00
public func getWaypoint(id: Int64, context: NSManagedObjectContext) -> WaypointEntity {
let fetchWaypointRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "WaypointEntity")
fetchWaypointRequest.predicate = NSPredicate(format: "id == %lld", Int64(id))
do {
let fetchedWaypoint = try context.fetch(fetchWaypointRequest) as! [WaypointEntity]
if fetchedWaypoint.count == 1 {
return fetchedWaypoint[0]
}
} catch {
2023-01-16 23:16:57 -08:00
return WaypointEntity(context: context)
2023-01-16 17:40:28 -08:00
}
2023-01-16 23:16:57 -08:00
return WaypointEntity(context: context)
2023-01-16 17:40:28 -08:00
}