add ability to check the freshness of a PersistedProfile
This commit is contained in:
@@ -66,7 +66,7 @@ final class ProfileDatabase {
|
||||
background_context?.mergePolicy = NSMergePolicy(merge: .mergeByPropertyObjectTrumpMergePolicyType)
|
||||
}
|
||||
|
||||
private func get_persisted(id: String) -> PersistedProfile? {
|
||||
func get_persisted(id: String) -> PersistedProfile? {
|
||||
let request = NSFetchRequest<PersistedProfile>(entityName: entity_name)
|
||||
request.predicate = NSPredicate(format: "id == %@", id)
|
||||
request.fetchLimit = 1
|
||||
|
||||
@@ -9,6 +9,8 @@ import Foundation
|
||||
|
||||
class Profiles {
|
||||
|
||||
static let db_freshness_threshold: TimeInterval = 24 * 60 * 60
|
||||
|
||||
/// This queue is used to synchronize access to the profiles dictionary, which
|
||||
/// prevents data races from crashing the app.
|
||||
private var queue = DispatchQueue(label: "io.damus.profiles",
|
||||
@@ -55,4 +57,22 @@ class Profiles {
|
||||
return profiles[id]
|
||||
}
|
||||
}
|
||||
|
||||
func has_fresh_profile(id: String) -> Bool {
|
||||
// check memory first
|
||||
var profile: Profile?
|
||||
queue.sync {
|
||||
profile = profiles[id]?.profile
|
||||
}
|
||||
if profile != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
// then disk
|
||||
guard let persisted_profile = database.get_persisted(id: id),
|
||||
let pull_date = persisted_profile.network_pull_date else {
|
||||
return false
|
||||
}
|
||||
return Date.now.timeIntervalSince(pull_date) < Profiles.db_freshness_threshold
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user