Compare commits
No commits in common. "5a098df931f51e46b62123a2ade17e7e7a27b415" and "f4f2a5546c4c73ed7e63c5bdb0d9e4752208744d" have entirely different histories.
5a098df931
...
f4f2a5546c
|
@ -78,8 +78,8 @@ class MastodonCachePersistentStore: NSPersistentContainer {
|
||||||
if self.backgroundContext.hasChanges {
|
if self.backgroundContext.hasChanges {
|
||||||
try! self.backgroundContext.save()
|
try! self.backgroundContext.save()
|
||||||
}
|
}
|
||||||
statuses.forEach { self.statusSubject.send($0.id) }
|
|
||||||
completion?()
|
completion?()
|
||||||
|
statuses.forEach { self.statusSubject.send($0.id) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,27 +142,4 @@ class MastodonCachePersistentStore: NSPersistentContainer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func performBatchUpdates(_ block: @escaping (_ context: NSManagedObjectContext, _ addAccounts: ([Account]) -> Void, _ addStatuses: ([Status]) -> Void) -> Void, completion: (() -> Void)? = nil) {
|
|
||||||
backgroundContext.perform {
|
|
||||||
var updatedAccounts = [String]()
|
|
||||||
var updatedStatuses = [String]()
|
|
||||||
|
|
||||||
block(self.backgroundContext, { (accounts) in
|
|
||||||
accounts.forEach { self.upsert(account: $0) }
|
|
||||||
updatedAccounts.append(contentsOf: accounts.map { $0.id })
|
|
||||||
}, { (statuses) in
|
|
||||||
statuses.forEach { self.upsert(status: $0, incrementReferenceCount: true) }
|
|
||||||
updatedStatuses.append(contentsOf: statuses.map { $0.id })
|
|
||||||
})
|
|
||||||
|
|
||||||
updatedAccounts.forEach(self.accountSubject.send)
|
|
||||||
updatedStatuses.forEach(self.statusSubject.send)
|
|
||||||
|
|
||||||
if self.backgroundContext.hasChanges {
|
|
||||||
try! self.backgroundContext.save()
|
|
||||||
}
|
|
||||||
completion?()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ class ConversationTableViewController: EnhancedTableViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
|
mastodonController.persistentContainer.status(for: mainStatusID)?.decrementReferenceCount()
|
||||||
for (id, _) in statuses {
|
for (id, _) in statuses {
|
||||||
mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount()
|
mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount()
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,8 +116,10 @@ class SearchResultsViewController: EnhancedTableViewController {
|
||||||
}
|
}
|
||||||
self.currentQuery = query
|
self.currentQuery = query
|
||||||
|
|
||||||
|
if self.dataSource.snapshot().numberOfItems == 0 {
|
||||||
activityIndicator.isHidden = false
|
activityIndicator.isHidden = false
|
||||||
activityIndicator.startAnimating()
|
activityIndicator.startAnimating()
|
||||||
|
}
|
||||||
|
|
||||||
let request = Client.search(query: query, resolve: true, limit: 10)
|
let request = Client.search(query: query, resolve: true, limit: 10)
|
||||||
mastodonController.run(request) { (response) in
|
mastodonController.run(request) { (response) in
|
||||||
|
@ -130,25 +132,11 @@ class SearchResultsViewController: EnhancedTableViewController {
|
||||||
|
|
||||||
guard self.currentQuery == query else { return }
|
guard self.currentQuery == query else { return }
|
||||||
|
|
||||||
let oldSnapshot = self.dataSource.snapshot()
|
|
||||||
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
||||||
|
|
||||||
self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in
|
|
||||||
// todo: reference count accounts
|
|
||||||
// oldSnapshot.itemIdentifiers(inSection: .accounts).forEach { (item) in
|
|
||||||
// guard case let .account(id) = item else { return }
|
|
||||||
// self.mastodonController.persistentContainer.account(for: id, in: context)?.decrementReferenceCount()
|
|
||||||
// }
|
|
||||||
|
|
||||||
oldSnapshot.itemIdentifiers(inSection: .statuses).forEach { (item) in
|
|
||||||
guard case let .status(id, _) = item else { return }
|
|
||||||
self.mastodonController.persistentContainer.status(for: id, in: context)?.decrementReferenceCount()
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.onlySections.contains(.accounts) && !results.accounts.isEmpty {
|
if self.onlySections.contains(.accounts) && !results.accounts.isEmpty {
|
||||||
snapshot.appendSections([.accounts])
|
snapshot.appendSections([.accounts])
|
||||||
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
|
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
|
||||||
addAccounts(results.accounts)
|
self.mastodonController.persistentContainer.addAll(accounts: results.accounts)
|
||||||
}
|
}
|
||||||
if self.onlySections.contains(.hashtags) && !results.hashtags.isEmpty {
|
if self.onlySections.contains(.hashtags) && !results.hashtags.isEmpty {
|
||||||
snapshot.appendSections([.hashtags])
|
snapshot.appendSections([.hashtags])
|
||||||
|
@ -157,15 +145,11 @@ class SearchResultsViewController: EnhancedTableViewController {
|
||||||
if self.onlySections.contains(.statuses) && !results.statuses.isEmpty {
|
if self.onlySections.contains(.statuses) && !results.statuses.isEmpty {
|
||||||
snapshot.appendSections([.statuses])
|
snapshot.appendSections([.statuses])
|
||||||
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
|
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
|
||||||
addStatuses(results.statuses)
|
self.mastodonController.persistentContainer.addAll(statuses: results.statuses)
|
||||||
addAccounts(results.statuses.map { $0.account })
|
self.mastodonController.persistentContainer.addAll(accounts: results.statuses.map { $0.account })
|
||||||
}
|
}
|
||||||
}, completion: {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.dataSource.apply(snapshot)
|
self.dataSource.apply(snapshot)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Table view delegate
|
// MARK: - Table view delegate
|
||||||
|
|
Loading…
Reference in New Issue