mastodon-ios/Mastodon/Protocol/StatusProvider/StatusProvider+UITableViewD...

51 lines
1.9 KiB
Swift
Raw Normal View History

2021-03-10 12:12:53 +01:00
//
// StatusProvider+UITableViewDataSourcePrefetching.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-10.
//
import UIKit
import CoreData
import CoreDataStack
extension StatusTableViewCellDelegate where Self: StatusProvider {
func handleTableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
2021-04-01 08:39:15 +02:00
// prefetch reply status
2021-03-10 12:12:53 +01:00
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
let domain = activeMastodonAuthenticationBox.domain
var statusObjectIDs: [NSManagedObjectID] = []
for item in items(indexPaths: indexPaths) {
switch item {
case .homeTimelineIndex(let objectID, _):
let homeTimelineIndex = managedObjectContext.object(with: objectID) as! HomeTimelineIndex
2021-04-01 08:39:15 +02:00
statusObjectIDs.append(homeTimelineIndex.status.objectID)
case .status(let objectID, _):
2021-03-10 12:12:53 +01:00
statusObjectIDs.append(objectID)
default:
continue
}
}
let backgroundManagedObjectContext = context.backgroundManagedObjectContext
backgroundManagedObjectContext.perform { [weak self] in
guard let self = self else { return }
for objectID in statusObjectIDs {
2021-04-01 08:39:15 +02:00
let status = backgroundManagedObjectContext.object(with: objectID) as! Status
guard let replyToID = status.inReplyToID, status.replyTo == nil else {
2021-03-10 12:12:53 +01:00
// skip
continue
}
self.context.statusPrefetchingService.prefetchReplyTo(
domain: domain,
2021-04-01 08:39:15 +02:00
statusObjectID: status.objectID,
statusID: status.id,
2021-03-10 12:12:53 +01:00
replyToStatusID: replyToID,
authorizationBox: activeMastodonAuthenticationBox
)
}
}
}
}