mirror of
git://jb55.com/damus
synced 2024-09-19 19:46:51 +00:00
add ability to check the freshness of a PersistedProfile
This commit is contained in:
parent
3f7b0a4d6e
commit
d58a1e0ba3
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user