mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
Create dedicated data layer types for querying and updating CoreData
This commit is contained in:
parent
3a248b6121
commit
b011cbde42
52 changed files with 2963 additions and 3035 deletions
99
Meshtastic/Persistence/QueryCoreDataController.swift
Normal file
99
Meshtastic/Persistence/QueryCoreDataController.swift
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
//
|
||||
// QueryCoreData.swift
|
||||
// Meshtastic
|
||||
//
|
||||
// Created(c) Garth Vander Houwen 1/16/23.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
|
||||
class QueryCoreDataController: ObservableObject {
|
||||
private let context: NSManagedObjectContext
|
||||
|
||||
init(context: NSManagedObjectContext) {
|
||||
self.context = context
|
||||
}
|
||||
|
||||
public func getNodeInfo(id: Int64) -> NodeInfoEntity? {
|
||||
|
||||
let fetchNodeInfoRequest = NodeInfoEntity.fetchRequest()
|
||||
fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
|
||||
|
||||
do {
|
||||
let fetchedNode = try context.fetch(fetchNodeInfoRequest)
|
||||
if fetchedNode.count == 1 {
|
||||
return fetchedNode[0]
|
||||
}
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func getStoreAndForwardMessageIds(seconds: Int) -> [UInt32] {
|
||||
|
||||
let time = seconds * -1
|
||||
let fetchMessagesRequest = MessageEntity.fetchRequest()
|
||||
let timeRange = Calendar.current.date(byAdding: .minute, value: time, to: Date())
|
||||
let milleseconds = Int32(timeRange?.timeIntervalSince1970 ?? 0)
|
||||
fetchMessagesRequest.predicate = NSPredicate(format: "receivedTimestamp >= %d", milleseconds)
|
||||
|
||||
do {
|
||||
let fetchedMessages = try context.fetch(fetchMessagesRequest)
|
||||
if fetchedMessages.count == 1 {
|
||||
return fetchedMessages.map { UInt32($0.messageId) }
|
||||
}
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
public func getTraceRoute(id: Int64) -> TraceRouteEntity? {
|
||||
|
||||
let fetchTraceRouteRequest = TraceRouteEntity.fetchRequest()
|
||||
fetchTraceRouteRequest.predicate = NSPredicate(format: "id == %lld", Int64(id))
|
||||
|
||||
do {
|
||||
let fetchedTraceRoute = try context.fetch(fetchTraceRouteRequest)
|
||||
if fetchedTraceRoute.count == 1 {
|
||||
return fetchedTraceRoute[0]
|
||||
}
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func getUser(id: Int64) -> UserEntity {
|
||||
|
||||
let fetchUserRequest = UserEntity.fetchRequest()
|
||||
fetchUserRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
|
||||
|
||||
do {
|
||||
let fetchedUser = try context.fetch(fetchUserRequest)
|
||||
if fetchedUser.count == 1 {
|
||||
return fetchedUser[0]
|
||||
}
|
||||
} catch {
|
||||
return UserEntity(context: context)
|
||||
}
|
||||
return UserEntity(context: context)
|
||||
}
|
||||
|
||||
public func getWaypoint(id: Int64) -> WaypointEntity {
|
||||
|
||||
let fetchWaypointRequest = WaypointEntity.fetchRequest()
|
||||
fetchWaypointRequest.predicate = NSPredicate(format: "id == %lld", Int64(id))
|
||||
|
||||
do {
|
||||
let fetchedWaypoint = try context.fetch(fetchWaypointRequest)
|
||||
if fetchedWaypoint.count == 1 {
|
||||
return fetchedWaypoint[0]
|
||||
}
|
||||
} catch {
|
||||
return WaypointEntity(context: context)
|
||||
}
|
||||
return WaypointEntity(context: context)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue