Fix crash due to prematurely pruned statuses being fetched

If the app hasn't launched in long enough, we may be displaying old statuses as a result of state restoration. If the user leaves the app, those statuses can't get pruned, because the user may return. We need to make sure the lastFetchedAt date is current, since awakeFromFetch won't be called until the object is faulted in (which wasn't happening immediately during state restoration).
This commit is contained in:
Shadowfacts 2023-10-24 15:28:44 -04:00
parent 74820e8922
commit bde21fbc6c
3 changed files with 21 additions and 3 deletions

View File

@ -59,10 +59,15 @@ public final class AccountMO: NSManagedObject, AccountProtocol {
super.awakeFromFetch()
managedObjectContext?.perform {
self.lastFetchedAt = Date()
self.touch()
}
}
/// Update the `lastFetchedAt` date so this object isn't pruned early.
func touch() {
lastFetchedAt = Date()
}
}
extension AccountMO {

View File

@ -89,10 +89,15 @@ public final class StatusMO: NSManagedObject, StatusProtocol {
super.awakeFromFetch()
managedObjectContext?.perform {
self.lastFetchedAt = Date()
self.touch()
}
}
/// Update the `lastFetchedAt` date so this object isn't pruned early.
func touch() {
lastFetchedAt = Date()
}
}
extension StatusMO {

View File

@ -440,7 +440,15 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
private func loadStatusesToRestore(position: TimelinePosition) async -> Bool {
let originalPositionStatusIDs = position.statusIDs
let unloaded = position.statusIDs.filter({ mastodonController.persistentContainer.status(for: $0) == nil })
var unloaded = [String]()
for id in position.statusIDs {
if let status = mastodonController.persistentContainer.status(for: id) {
// touch the status so that, even if it's old, it doesn't get pruned when we go into the background
status.touch()
} else {
unloaded.append(id)
}
}
guard !unloaded.isEmpty else {
return true
}