diff --git a/Mastodon/Diffable/Discovery/DiscoverySection.swift b/Mastodon/Diffable/Discovery/DiscoverySection.swift index 53726292b..13d44f0fb 100644 --- a/Mastodon/Diffable/Discovery/DiscoverySection.swift +++ b/Mastodon/Diffable/Discovery/DiscoverySection.swift @@ -60,8 +60,8 @@ extension DiscoverySection { return cell case .user(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ProfileCardTableViewCell.self), for: indexPath) as! ProfileCardTableViewCell - context.managedObjectContext.performAndWait { - guard let user = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let user = record.object(in: context.cacheManagedObjectContext) else { return } cell.configure( tableView: tableView, user: user, @@ -77,7 +77,7 @@ extension DiscoverySection { cell.profileCardView.viewModel.familiarFollowers = nil } // bind me - cell.profileCardView.viewModel.relationshipViewModel.me = configuration.authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) + cell.profileCardView.viewModel.relationshipViewModel.me = configuration.authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext) } return cell case .bottomLoader: diff --git a/Mastodon/Diffable/Notification/NotificationSection.swift b/Mastodon/Diffable/Notification/NotificationSection.swift index 0271aac20..8f6fd4ea2 100644 --- a/Mastodon/Diffable/Notification/NotificationSection.swift +++ b/Mastodon/Diffable/Notification/NotificationSection.swift @@ -43,8 +43,8 @@ extension NotificationSection { switch item { case .feed(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: NotificationTableViewCell.self), for: indexPath) as! NotificationTableViewCell - context.managedObjectContext.performAndWait { - guard let feed = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let feed = record.object(in: context.cacheManagedObjectContext) else { return } configure( context: context, tableView: tableView, diff --git a/Mastodon/Diffable/Report/ReportSection.swift b/Mastodon/Diffable/Report/ReportSection.swift index 4c8fd4345..16e71cf6c 100644 --- a/Mastodon/Diffable/Report/ReportSection.swift +++ b/Mastodon/Diffable/Report/ReportSection.swift @@ -48,8 +48,8 @@ extension ReportSection { return cell case .status(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ReportStatusTableViewCell.self), for: indexPath) as! ReportStatusTableViewCell - context.managedObjectContext.performAndWait { - guard let status = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let status = record.object(in: context.cacheManagedObjectContext) else { return } configure( context: context, tableView: tableView, @@ -78,8 +78,8 @@ extension ReportSection { return cell case .result(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ReportResultActionTableViewCell.self), for: indexPath) as! ReportResultActionTableViewCell - context.managedObjectContext.performAndWait { - guard let user = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let user = record.object(in: context.cacheManagedObjectContext) else { return } cell.avatarImageView.configure(configuration: .init(url: user.avatarImageURL())) } return cell diff --git a/Mastodon/Diffable/Search/SearchHistorySection.swift b/Mastodon/Diffable/Search/SearchHistorySection.swift index 1cd17da78..3e21c78c6 100644 --- a/Mastodon/Diffable/Search/SearchHistorySection.swift +++ b/Mastodon/Diffable/Search/SearchHistorySection.swift @@ -28,10 +28,10 @@ extension SearchHistorySection { ) -> UICollectionViewDiffableDataSource { let userCellRegister = UICollectionView.CellRegistration> { cell, indexPath, item in - context.managedObjectContext.performAndWait { - guard let user = item.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let user = item.object(in: context.cacheManagedObjectContext) else { return } cell.configure( - me: authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext), + me: authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext), viewModel: SearchHistoryUserCollectionViewCell.ViewModel( value: user, followedUsers: authContext.mastodonAuthenticationBox.inMemoryCache.$followingUserIds.eraseToAnyPublisher(), @@ -44,8 +44,8 @@ extension SearchHistorySection { } let hashtagCellRegister = UICollectionView.CellRegistration> { cell, indexPath, item in - context.managedObjectContext.performAndWait { - guard let hashtag = item.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let hashtag = item.object(in: context.cacheManagedObjectContext) else { return } var contentConfiguration = cell.defaultContentConfiguration() contentConfiguration.text = "#" + hashtag.name cell.contentConfiguration = contentConfiguration diff --git a/Mastodon/Diffable/Search/SearchResultSection.swift b/Mastodon/Diffable/Search/SearchResultSection.swift index 06ea1b7fe..6dc3323d6 100644 --- a/Mastodon/Diffable/Search/SearchResultSection.swift +++ b/Mastodon/Diffable/Search/SearchResultSection.swift @@ -46,8 +46,8 @@ extension SearchResultSection { switch item { case .user(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UserTableViewCell.self), for: indexPath) as! UserTableViewCell - context.managedObjectContext.performAndWait { - guard let user = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let user = record.object(in: context.cacheManagedObjectContext) else { return } configure( context: context, authContext: authContext, @@ -63,8 +63,8 @@ extension SearchResultSection { return cell case .status(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusTableViewCell.self), for: indexPath) as! StatusTableViewCell - context.managedObjectContext.performAndWait { - guard let status = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let status = record.object(in: context.cacheManagedObjectContext) else { return } configure( context: context, tableView: tableView, @@ -129,7 +129,7 @@ extension SearchResultSection { configuration: Configuration ) { cell.configure( - me: authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext), + me: authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext), tableView: tableView, viewModel: viewModel, delegate: configuration.userTableViewCellDelegate diff --git a/Mastodon/Diffable/Status/StatusSection.swift b/Mastodon/Diffable/Status/StatusSection.swift index e134a17cb..9ffeb4f0d 100644 --- a/Mastodon/Diffable/Status/StatusSection.swift +++ b/Mastodon/Diffable/Status/StatusSection.swift @@ -49,8 +49,8 @@ extension StatusSection { switch item { case .feed(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusTableViewCell.self), for: indexPath) as! StatusTableViewCell - context.managedObjectContext.performAndWait { - guard let feed = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let feed = record.object(in: context.cacheManagedObjectContext) else { return } configure( context: context, tableView: tableView, @@ -62,8 +62,8 @@ extension StatusSection { return cell case .feedLoader(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineMiddleLoaderTableViewCell.self), for: indexPath) as! TimelineMiddleLoaderTableViewCell - context.managedObjectContext.performAndWait { - guard let feed = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let feed = record.object(in: context.cacheManagedObjectContext) else { return } configure( cell: cell, feed: feed, @@ -73,8 +73,8 @@ extension StatusSection { return cell case .status(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: StatusTableViewCell.self), for: indexPath) as! StatusTableViewCell - context.managedObjectContext.performAndWait { - guard let status = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let status = record.object(in: context.cacheManagedObjectContext) else { return } configure( context: context, tableView: tableView, @@ -122,7 +122,7 @@ extension StatusSection { indexPath: IndexPath, configuration: ThreadCellRegistrationConfiguration ) -> UITableViewCell { - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext switch configuration.thread { case .root(let threadContext): @@ -164,7 +164,7 @@ extension StatusSection { authContext: AuthContext, statusView: StatusView ) { - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext statusView.pollTableViewDiffableDataSource = UITableViewDiffableDataSource(tableView: statusView.pollTableView) { tableView, indexPath, item in switch item { case .history: diff --git a/Mastodon/Diffable/User/UserSection.swift b/Mastodon/Diffable/User/UserSection.swift index 8a0563db4..18de67e35 100644 --- a/Mastodon/Diffable/User/UserSection.swift +++ b/Mastodon/Diffable/User/UserSection.swift @@ -41,8 +41,8 @@ extension UserSection { switch item { case .user(let record): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UserTableViewCell.self), for: indexPath) as! UserTableViewCell - context.managedObjectContext.performAndWait { - guard let user = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let user = record.object(in: context.cacheManagedObjectContext) else { return } configure( context: context, authContext: authContext, @@ -83,7 +83,7 @@ extension UserSection { configuration: Configuration ) { cell.configure( - me: authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext), + me: authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext), tableView: tableView, viewModel: viewModel, delegate: configuration.userTableViewCellDelegate diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift index 88503ae7b..939a4e536 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift @@ -37,7 +37,7 @@ extension DataSourceFacade { let selectionFeedbackGenerator = await UISelectionFeedbackGenerator() await selectionFeedbackGenerator.selectionChanged() - let managedObjectContext = dependency.context.managedObjectContext + let managedObjectContext = dependency.context.cacheManagedObjectContext let _userID: MastodonUser.ID? = try await managedObjectContext.perform { guard let notification = notification.object(in: managedObjectContext) else { return nil } return notification.account.id diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Hashtag.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Hashtag.swift index 6135c904a..c7830355e 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Hashtag.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Hashtag.swift @@ -47,7 +47,7 @@ extension DataSourceFacade { provider: DataSourceProvider & AuthContextProvider, tag: ManagedObjectRecord ) async { - let managedObjectContext = provider.context.managedObjectContext + let managedObjectContext = provider.context.cacheManagedObjectContext let _name: String? = try? await managedObjectContext.perform { guard let tag = tag.object(in: managedObjectContext) else { return nil } return tag.name diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Media.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Media.swift index 9dd97f38a..af5623aeb 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Media.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Media.swift @@ -65,7 +65,7 @@ extension DataSourceFacade { status: ManagedObjectRecord, previewContext: AttachmentPreviewContext ) async throws { - let managedObjectContext = dependency.context.managedObjectContext + let managedObjectContext = dependency.context.cacheManagedObjectContext let attachments: [MastodonAttachment] = try await managedObjectContext.perform { guard let _status = status.object(in: managedObjectContext) else { return [] } let status = _status.reblog ?? _status @@ -156,7 +156,7 @@ extension DataSourceFacade { ) async throws { let logger = Logger(subsystem: "DataSourceFacade", category: "Media") - let managedObjectContext = dependency.context.managedObjectContext + let managedObjectContext = dependency.context.cacheManagedObjectContext var _avatarAssetURL: String? var _headerAssetURL: String? diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Meta.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Meta.swift index ca3bbd474..e00b5de9e 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Meta.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Meta.swift @@ -19,7 +19,7 @@ extension DataSourceFacade { meta: Meta ) async throws { let _redirectRecord = await DataSourceFacade.status( - managedObjectContext: provider.context.managedObjectContext, + managedObjectContext: provider.context.cacheManagedObjectContext, status: status, target: target ) diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Profile.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Profile.swift index f434bc6ac..ab34d83bd 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Profile.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Profile.swift @@ -17,7 +17,7 @@ extension DataSourceFacade { status: ManagedObjectRecord ) async { let _redirectRecord = await DataSourceFacade.author( - managedObjectContext: provider.context.managedObjectContext, + managedObjectContext: provider.context.cacheManagedObjectContext, status: status, target: target ) @@ -36,7 +36,7 @@ extension DataSourceFacade { provider: DataSourceProvider & AuthContextProvider, user: ManagedObjectRecord ) async { - guard let user = user.object(in: provider.context.managedObjectContext) else { + guard let user = user.object(in: provider.context.cacheManagedObjectContext) else { assertionFailure() return } @@ -73,7 +73,7 @@ extension DataSourceFacade { return } - let managedObjectContext = provider.context.managedObjectContext + let managedObjectContext = provider.context.cacheManagedObjectContext let mentions = try? await managedObjectContext.perform { return status.object(in: managedObjectContext)?.mentions ?? [] } @@ -97,7 +97,7 @@ extension DataSourceFacade { let request = MastodonUser.sortedFetchRequest request.fetchLimit = 1 request.predicate = MastodonUser.predicate(domain: domain, id: userID) - let _user = provider.context.managedObjectContext.safeFetch(request).first + let _user = provider.context.cacheManagedObjectContext.safeFetch(request).first if let user = _user { return CachedProfileViewModel(context: provider.context, authContext: provider.authContext, mastodonUser: user) @@ -352,7 +352,7 @@ extension DataSourceFacade { dependency: NeedsDependency, user: ManagedObjectRecord ) async throws -> UIActivityViewController? { - let managedObjectContext = dependency.context.managedObjectContext + let managedObjectContext = dependency.context.cacheManagedObjectContext let activityItems: [Any] = try await managedObjectContext.perform { guard let user = user.object(in: managedObjectContext) else { return [] } return user.activityItems diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift index dacec8381..05718b7ae 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift @@ -58,8 +58,8 @@ extension DataSourceFacade { dependency: NeedsDependency, status: ManagedObjectRecord ) async throws -> UIActivityViewController { - var activityItems: [Any] = try await dependency.context.managedObjectContext.perform { - guard let status = status.object(in: dependency.context.managedObjectContext), + var activityItems: [Any] = try await dependency.context.cacheManagedObjectContext.perform { + guard let status = status.object(in: dependency.context.cacheManagedObjectContext), let url = URL(string: status.url ?? status.uri) else { return [] } return [ @@ -98,7 +98,7 @@ extension DataSourceFacade { action: ActionToolbarContainer.Action, sender: UIButton ) async throws { - let managedObjectContext = provider.context.managedObjectContext + let managedObjectContext = provider.context.cacheManagedObjectContext let _status: ManagedObjectRecord? = try? await managedObjectContext.perform { guard let object = status.object(in: managedObjectContext) else { return nil } let objectID = (object.reblog ?? object).objectID @@ -181,7 +181,7 @@ extension DataSourceFacade { guard let dependency else { return } Task { - let managedObjectContext = dependency.context.managedObjectContext + let managedObjectContext = dependency.context.cacheManagedObjectContext let _user: ManagedObjectRecord? = try? await managedObjectContext.perform { guard let user = menuContext.author?.object(in: managedObjectContext) else { return nil } return ManagedObjectRecord(objectID: user.objectID) @@ -214,7 +214,7 @@ extension DataSourceFacade { ) { [weak dependency] _ in guard let dependency = dependency else { return } Task { - let managedObjectContext = dependency.context.managedObjectContext + let managedObjectContext = dependency.context.cacheManagedObjectContext let _user: ManagedObjectRecord? = try? await managedObjectContext.perform { guard let user = menuContext.author?.object(in: managedObjectContext) else { return nil } return ManagedObjectRecord(objectID: user.objectID) @@ -242,7 +242,7 @@ extension DataSourceFacade { ) { [weak dependency] _ in guard let dependency = dependency else { return } Task { - let managedObjectContext = dependency.context.managedObjectContext + let managedObjectContext = dependency.context.cacheManagedObjectContext let _user: ManagedObjectRecord? = try? await managedObjectContext.perform { guard let user = menuContext.author?.object(in: managedObjectContext) else { return nil } return ManagedObjectRecord(objectID: user.objectID) @@ -308,7 +308,7 @@ extension DataSourceFacade { } // end Task case .shareStatus: Task { - let managedObjectContext = dependency.context.managedObjectContext + let managedObjectContext = dependency.context.cacheManagedObjectContext guard let status: ManagedObjectRecord = try? await managedObjectContext.perform(block: { guard let object = menuContext.status?.object(in: managedObjectContext) else { return nil } let objectID = (object.reblog ?? object).objectID @@ -371,7 +371,7 @@ extension DataSourceFacade { } case .editStatus: - guard let status = menuContext.status?.object(in: dependency.context.managedObjectContext) else { return } + guard let status = menuContext.status?.object(in: dependency.context.cacheManagedObjectContext) else { return } let statusSource = try await dependency.context.apiService.getStatusSource( forStatusID: status.id, @@ -394,8 +394,8 @@ extension DataSourceFacade { dependency: NeedsDependency, status: ManagedObjectRecord ) async throws { - try await dependency.context.managedObjectContext.perform { - guard let _status = status.object(in: dependency.context.managedObjectContext) else { return } + try await dependency.context.cacheManagedObjectContext.perform { + guard let _status = status.object(in: dependency.context.cacheManagedObjectContext) else { return } let status = _status.reblog ?? _status status.update(isSensitiveToggled: !status.isSensitiveToggled) } diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Thread.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Thread.swift index 41f5d58de..5955ed3a9 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Thread.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Thread.swift @@ -18,7 +18,7 @@ extension DataSourceFacade { ) async { let _root: StatusItem.Thread? = await { let _redirectRecord = await DataSourceFacade.status( - managedObjectContext: provider.context.managedObjectContext, + managedObjectContext: provider.context.cacheManagedObjectContext, status: status, target: target ) diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Translate.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Translate.swift index 8ce9c2447..fa23cae5b 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Translate.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Translate.swift @@ -25,7 +25,7 @@ extension DataSourceFacade { await selectionFeedbackGenerator.selectionChanged() guard - let status = status.object(in: provider.context.managedObjectContext) + let status = status.object(in: provider.context.cacheManagedObjectContext) else { return } diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+UserView.swift b/Mastodon/Protocol/Provider/DataSourceFacade+UserView.swift index 8b1a5c84d..67ba195ce 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+UserView.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+UserView.swift @@ -19,7 +19,7 @@ extension DataSourceFacade { user: user ) - if let userObject = user.object(in: dependency.context.managedObjectContext) { + if let userObject = user.object(in: dependency.context.cacheManagedObjectContext) { dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followingUserIds.append(userObject.id) } @@ -29,7 +29,7 @@ extension DataSourceFacade { user: user ) - if let userObject = user.object(in: dependency.context.managedObjectContext) { + if let userObject = user.object(in: dependency.context.cacheManagedObjectContext) { dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followRequestedUserIDs.append(userObject.id) } @@ -38,7 +38,7 @@ extension DataSourceFacade { dependency: dependency, user: user ) - if let userObject = user.object(in: dependency.context.managedObjectContext) { + if let userObject = user.object(in: dependency.context.cacheManagedObjectContext) { dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followingUserIds.removeAll(where: { $0 == userObject.id }) } case .blocked: @@ -47,7 +47,7 @@ extension DataSourceFacade { user: user ) - if let userObject = user.object(in: dependency.context.managedObjectContext) { + if let userObject = user.object(in: dependency.context.cacheManagedObjectContext) { dependency.authContext.mastodonAuthenticationBox.inMemoryCache.blockedUserIds.append(userObject.id) } @@ -57,7 +57,7 @@ extension DataSourceFacade { user: user ) - if let userObject = user.object(in: dependency.context.managedObjectContext) { + if let userObject = user.object(in: dependency.context.cacheManagedObjectContext) { dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followRequestedUserIDs.removeAll(where: { $0 == userObject.id }) } case .none, .loading: diff --git a/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift b/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift index 019f780bb..20048b5b6 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift @@ -30,8 +30,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut return } - let _author: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } + let _author: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } return .init(objectID: notification.account.objectID) } guard let author = _author else { @@ -70,8 +70,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut assertionFailure("only works for status data provider") return } - let _author: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } + let _author: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } return .init(objectID: notification.account.objectID) } guard let author = _author else { @@ -180,7 +180,7 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Med return } - let managedObjectContext = self.context.managedObjectContext + let managedObjectContext = self.context.cacheManagedObjectContext let _mediaTransitionContext: NotificationMediaTransitionContext? = try await managedObjectContext.perform { guard let notification = record.object(in: managedObjectContext) else { return nil } guard let _status = notification.status else { return nil } @@ -232,7 +232,7 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Med return } - let managedObjectContext = self.context.managedObjectContext + let managedObjectContext = self.context.cacheManagedObjectContext let _mediaTransitionContext: NotificationMediaTransitionContext? = try await managedObjectContext.perform { guard let notification = record.object(in: managedObjectContext) else { return nil } guard let _status = notification.status else { return nil } @@ -286,8 +286,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut assertionFailure("only works for status data provider") return } - let _status: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } + let _status: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } guard let status = notification.status else { return nil } return .init(objectID: status.objectID) } @@ -323,8 +323,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut assertionFailure("only works for status data provider") return } - let _author: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } + let _author: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } guard let status = notification.status else { return nil } return .init(objectID: status.author.objectID) } @@ -367,8 +367,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut assertionFailure("only works for notification item") return } - let _status: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } + let _status: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } guard let status = notification.status else { return nil } return .init(objectID: status.objectID) } @@ -400,8 +400,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut assertionFailure("only works for notification item") return } - let _status: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } + let _status: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } guard let status = notification.status else { return nil } return .init(objectID: status.objectID) } @@ -432,8 +432,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut // assertionFailure("only works for notification item") // return // } -// let _status: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { -// guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } +// let _status: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { +// guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } // guard let status = notification.status else { return nil } // return .init(objectID: status.objectID) // } @@ -465,8 +465,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut assertionFailure("only works for notification item") return } - let _status: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } + let _status: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } guard let status = notification.status else { return nil } return .init(objectID: status.objectID) } @@ -497,8 +497,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut assertionFailure("only works for notification item") return } - let _status: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil } + let _status: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let notification = notification.object(in: self.context.cacheManagedObjectContext) else { return nil } guard let status = notification.status else { return nil } return .init(objectID: status.objectID) } diff --git a/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewCellDelegate.swift b/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewCellDelegate.swift index 230608e58..9a8fe2994 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewCellDelegate.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewCellDelegate.swift @@ -37,13 +37,13 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte case .none: break case .reply: - let _replyToAuthor: ManagedObjectRecord? = try? await context.managedObjectContext.perform { - guard let status = status.object(in: self.context.managedObjectContext) else { return nil } + let _replyToAuthor: ManagedObjectRecord? = try? await context.cacheManagedObjectContext.perform { + guard let status = status.object(in: self.context.cacheManagedObjectContext) else { return nil } guard let inReplyToAccountID = status.inReplyToAccountID else { return nil } let request = MastodonUser.sortedFetchRequest request.predicate = MastodonUser.predicate(domain: status.author.domain, id: inReplyToAccountID) request.fetchLimit = 1 - guard let author = self.context.managedObjectContext.safeFetch(request).first else { return nil } + guard let author = self.context.cacheManagedObjectContext.safeFetch(request).first else { return nil } return .init(objectID: author.objectID) } guard let replyToAuthor = _replyToAuthor else { @@ -305,7 +305,7 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte guard let pollTableViewDiffableDataSource = statusView.pollTableViewDiffableDataSource else { return } guard let pollItem = pollTableViewDiffableDataSource.itemIdentifier(for: indexPath) else { return } - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext Task { guard case let .option(pollOption) = pollItem else { @@ -378,7 +378,7 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte guard let firstPollItem = pollTableViewDiffableDataSource.snapshot().itemIdentifiers.first else { return } guard case let .option(firstPollOption) = firstPollItem else { return } - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext Task { var _poll: ManagedObjectRecord? @@ -476,8 +476,8 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte assertionFailure("only works for status data provider") return } - let _author: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - guard let _status = status.object(in: self.context.managedObjectContext) else { return nil } + let _author: ManagedObjectRecord? = try await self.context.cacheManagedObjectContext.perform { + guard let _status = status.object(in: self.context.cacheManagedObjectContext) else { return nil } let author = (_status.reblog ?? _status).author return .init(objectID: author.objectID) } @@ -666,7 +666,7 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte return } - guard let status = status.object(in: context.managedObjectContext), + guard let status = status.object(in: context.cacheManagedObjectContext), let edits = status.editHistory?.sorted(by: { $0.createdAt > $1.createdAt }) else { return } let viewModel = StatusEditHistoryViewModel(status: status, edits: edits, appContext: context, authContext: authContext) diff --git a/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewControllerNavigateable.swift b/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewControllerNavigateable.swift index b8cf026d4..29b297438 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewControllerNavigateable.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewControllerNavigateable.swift @@ -66,8 +66,8 @@ extension StatusTableViewControllerNavigateableCore where Self: DataSourceProvid case .status(let record): return record case .notification(let record): - let _statusRecord: ManagedObjectRecord? = try? await context.managedObjectContext.perform { - guard let notification = record.object(in: self.context.managedObjectContext) else { return nil } + let _statusRecord: ManagedObjectRecord? = try? await context.cacheManagedObjectContext.perform { + guard let notification = record.object(in: self.context.cacheManagedObjectContext) else { return nil } guard let status = notification.status else { return nil } return .init(objectID: status.objectID) } diff --git a/Mastodon/Protocol/Provider/DataSourceProvider+UITableViewDelegate.swift b/Mastodon/Protocol/Provider/DataSourceProvider+UITableViewDelegate.swift index 6ced42601..9786f56ff 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider+UITableViewDelegate.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider+UITableViewDelegate.swift @@ -39,7 +39,7 @@ extension UITableViewDelegate where Self: DataSourceProvider & AuthContextProvid tag: tag ) case .notification(let notification): - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let _status: ManagedObjectRecord? = try await managedObjectContext.perform { guard let notification = notification.object(in: managedObjectContext) else { return nil } diff --git a/Mastodon/Scene/Account/AccountViewController.swift b/Mastodon/Scene/Account/AccountViewController.swift index 4eab3565c..96e3c2553 100644 --- a/Mastodon/Scene/Account/AccountViewController.swift +++ b/Mastodon/Scene/Account/AccountViewController.swift @@ -116,7 +116,7 @@ extension AccountListViewController { tableView.delegate = self viewModel.setupDiffableDataSource( tableView: tableView, - managedObjectContext: context.managedObjectContext + managedObjectContext: context.cacheManagedObjectContext ) viewModel.dataSourceDidUpdate diff --git a/Mastodon/Scene/Discovery/Community/DiscoveryCommunityViewModel.swift b/Mastodon/Scene/Discovery/Community/DiscoveryCommunityViewModel.swift index eaf8646c4..4c786bd28 100644 --- a/Mastodon/Scene/Discovery/Community/DiscoveryCommunityViewModel.swift +++ b/Mastodon/Scene/Discovery/Community/DiscoveryCommunityViewModel.swift @@ -48,7 +48,7 @@ final class DiscoveryCommunityViewModel { self.context = context self.authContext = authContext self.statusFetchedResultsController = StatusFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalTweetPredicate: nil ) diff --git a/Mastodon/Scene/Discovery/ForYou/DiscoveryForYouViewController.swift b/Mastodon/Scene/Discovery/ForYou/DiscoveryForYouViewController.swift index 007abd901..e059f821f 100644 --- a/Mastodon/Scene/Discovery/ForYou/DiscoveryForYouViewController.swift +++ b/Mastodon/Scene/Discovery/ForYou/DiscoveryForYouViewController.swift @@ -113,7 +113,7 @@ extension DiscoveryForYouViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(indexPath)") guard case let .user(record) = viewModel.diffableDataSource?.itemIdentifier(for: indexPath) else { return } - guard let user = record.object(in: context.managedObjectContext) else { return } + guard let user = record.object(in: context.cacheManagedObjectContext) else { return } let profileViewModel = CachedProfileViewModel( context: context, authContext: viewModel.authContext, @@ -153,7 +153,7 @@ extension DiscoveryForYouViewController: ProfileCardTableViewCellDelegate { ) { guard let indexPath = tableView.indexPath(for: cell) else { return } guard case let .user(record) = viewModel.diffableDataSource?.itemIdentifier(for: indexPath) else { return } - guard let user = record.object(in: context.managedObjectContext) else { return } + guard let user = record.object(in: context.cacheManagedObjectContext) else { return } let userID = user.id let _familiarFollowers = viewModel.familiarFollowers.first(where: { $0.id == userID }) diff --git a/Mastodon/Scene/Discovery/ForYou/DiscoveryForYouViewModel.swift b/Mastodon/Scene/Discovery/ForYou/DiscoveryForYouViewModel.swift index 89122c06e..e3be712ac 100644 --- a/Mastodon/Scene/Discovery/ForYou/DiscoveryForYouViewModel.swift +++ b/Mastodon/Scene/Discovery/ForYou/DiscoveryForYouViewModel.swift @@ -35,7 +35,7 @@ final class DiscoveryForYouViewModel { self.context = context self.authContext = authContext self.userFetchedResultsController = UserFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalPredicate: nil ) diff --git a/Mastodon/Scene/Discovery/Posts/DiscoveryPostsViewModel.swift b/Mastodon/Scene/Discovery/Posts/DiscoveryPostsViewModel.swift index 7a1b044fd..f94d35d8e 100644 --- a/Mastodon/Scene/Discovery/Posts/DiscoveryPostsViewModel.swift +++ b/Mastodon/Scene/Discovery/Posts/DiscoveryPostsViewModel.swift @@ -46,7 +46,7 @@ final class DiscoveryPostsViewModel { self.context = context self.authContext = authContext self.statusFetchedResultsController = StatusFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalTweetPredicate: nil ) diff --git a/Mastodon/Scene/HashtagTimeline/HashtagTimelineViewModel.swift b/Mastodon/Scene/HashtagTimeline/HashtagTimelineViewModel.swift index 888bc720c..bbee3f940 100644 --- a/Mastodon/Scene/HashtagTimeline/HashtagTimelineViewModel.swift +++ b/Mastodon/Scene/HashtagTimeline/HashtagTimelineViewModel.swift @@ -58,7 +58,7 @@ final class HashtagTimelineViewModel { self.authContext = authContext self.hashtag = hashtag self.fetchedResultsController = StatusFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalTweetPredicate: nil ) @@ -77,7 +77,7 @@ final class HashtagTimelineViewModel { ) guard - let object = Tag.findOrFetch(in: context.managedObjectContext, matching: predicate) + let object = Tag.findOrFetch(in: context.cacheManagedObjectContext, matching: predicate) else { return hashtagDetails.send(hashtagDetails.value?.copy(following: false)) } diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController+DataSourceProvider.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController+DataSourceProvider.swift index b141d386a..fb8f32369 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController+DataSourceProvider.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController+DataSourceProvider.swift @@ -21,7 +21,7 @@ extension HomeTimelineViewController: DataSourceProvider { switch item { case .feed(let record): - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let item: DataSourceItem? = try? await managedObjectContext.perform { guard let feed = record.object(in: managedObjectContext) else { return nil } guard feed.kind == .home else { return nil } diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController+DebugAction.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController+DebugAction.swift index 02ade1608..411421a94 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController+DebugAction.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController+DebugAction.swift @@ -190,7 +190,7 @@ extension HomeTimelineViewController { func match(item: StatusItem, context: AppContext) -> Bool { switch item { case .feed(let record): - guard let feed = record.object(in: context.managedObjectContext) else { return false } + guard let feed = record.object(in: context.cacheManagedObjectContext) else { return false } if let status = feed.status { switch self { case .gap: diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift index 049683836..33adf0303 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift @@ -242,7 +242,7 @@ extension HomeTimelineViewController { self?.showEmptyView() let userDoesntFollowPeople: Bool - if let managedObjectContext = self?.context.managedObjectContext, + if let managedObjectContext = self?.context.cacheManagedObjectContext, let authContext = self?.authContext, let me = authContext.mastodonAuthenticationBox.authentication.user(in: managedObjectContext){ userDoesntFollowPeople = me.followersCount == 0 diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+Diffable.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+Diffable.swift index 24cf2258a..9ceb1c43d 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+Diffable.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+Diffable.swift @@ -59,7 +59,7 @@ extension HomeTimelineViewModel { return snapshot }() - let parentManagedObjectContext = self.context.managedObjectContext + let parentManagedObjectContext = self.context.cacheManagedObjectContext let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) managedObjectContext.parent = parentManagedObjectContext try? await managedObjectContext.perform { diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift index 663a38724..e6a6716e6 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift @@ -83,7 +83,7 @@ final class HomeTimelineViewModel: NSObject { init(context: AppContext, authContext: AuthContext) { self.context = context self.authContext = authContext - self.fetchedResultsController = FeedFetchedResultsController(managedObjectContext: context.managedObjectContext) + self.fetchedResultsController = FeedFetchedResultsController(managedObjectContext: context.cacheManagedObjectContext) self.homeTimelineNavigationBarTitleViewModel = HomeTimelineNavigationBarTitleViewModel(context: context) super.init() @@ -136,7 +136,7 @@ extension HomeTimelineViewModel { guard let diffableDataSource = diffableDataSource else { return } var snapshot = diffableDataSource.snapshot() - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let key = "LoadMore@\(record.objectID)" guard let feed = record.object(in: managedObjectContext) else { return } diff --git a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewController+DataSourceProvider.swift b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewController+DataSourceProvider.swift index c058ee921..f535b1231 100644 --- a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewController+DataSourceProvider.swift +++ b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewController+DataSourceProvider.swift @@ -21,7 +21,7 @@ extension NotificationTimelineViewController: DataSourceProvider { switch item { case .feed(let record): - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let item: DataSourceItem? = try? await managedObjectContext.perform { guard let feed = record.object(in: managedObjectContext) else { return nil } guard feed.kind == .notificationAll || feed.kind == .notificationMentions else { return nil } diff --git a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewController.swift b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewController.swift index 9b7416afc..dba773adc 100644 --- a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewController.swift +++ b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewController.swift @@ -291,7 +291,7 @@ extension NotificationTimelineViewController: TableViewControllerNavigateable { Task { @MainActor in switch item { case .feed(let record): - guard let feed = record.object(in: self.context.managedObjectContext) else { return } + guard let feed = record.object(in: self.context.cacheManagedObjectContext) else { return } guard let notification = feed.notification else { return } if let stauts = notification.status { diff --git a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel+Diffable.swift b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel+Diffable.swift index 0331f401e..0ba5ee2ef 100644 --- a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel+Diffable.swift +++ b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel+Diffable.swift @@ -55,7 +55,7 @@ extension NotificationTimelineViewModel { return snapshot }() - let parentManagedObjectContext = self.context.managedObjectContext + let parentManagedObjectContext = self.context.cacheManagedObjectContext let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) managedObjectContext.parent = parentManagedObjectContext try? await managedObjectContext.perform { diff --git a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel+LoadOldestState.swift b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel+LoadOldestState.swift index ff23d8d6e..24eca90e6 100644 --- a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel+LoadOldestState.swift +++ b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel+LoadOldestState.swift @@ -69,7 +69,7 @@ extension NotificationTimelineViewModel.LoadOldestState { let scope = viewModel.scope Task { - let managedObjectContext = viewModel.context.managedObjectContext + let managedObjectContext = viewModel.context.cacheManagedObjectContext let _maxID: Mastodon.Entity.Notification.ID? = try await managedObjectContext.perform { guard let feed = lastFeedRecord.object(in: managedObjectContext), let notification = feed.notification diff --git a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel.swift b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel.swift index f4ce7c2d7..92709de7f 100644 --- a/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel.swift +++ b/Mastodon/Scene/Notification/NotificationTimeline/NotificationTimelineViewModel.swift @@ -54,7 +54,7 @@ final class NotificationTimelineViewModel { self.context = context self.authContext = authContext self.scope = scope - self.feedFetchedResultsController = FeedFetchedResultsController(managedObjectContext: context.managedObjectContext) + self.feedFetchedResultsController = FeedFetchedResultsController(managedObjectContext: context.cacheManagedObjectContext) // end init feedFetchedResultsController.predicate = NotificationTimelineViewModel.feedPredicate( @@ -133,7 +133,7 @@ extension NotificationTimelineViewModel { func loadMore(item: NotificationItem) async { guard case let .feedLoader(record) = item else { return } - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let key = "LoadMore@\(record.objectID)" // return when already loading state diff --git a/Mastodon/Scene/Profile/Bookmark/BookmarkViewModel.swift b/Mastodon/Scene/Profile/Bookmark/BookmarkViewModel.swift index f56e65526..fb925f935 100644 --- a/Mastodon/Scene/Profile/Bookmark/BookmarkViewModel.swift +++ b/Mastodon/Scene/Profile/Bookmark/BookmarkViewModel.swift @@ -42,7 +42,7 @@ final class BookmarkViewModel { self.context = context self.authContext = authContext self.statusFetchedResultsController = StatusFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalTweetPredicate: nil ) diff --git a/Mastodon/Scene/Profile/FamiliarFollowers/FamiliarFollowersViewModel.swift b/Mastodon/Scene/Profile/FamiliarFollowers/FamiliarFollowersViewModel.swift index 3e4a17e5e..a10cd10f7 100644 --- a/Mastodon/Scene/Profile/FamiliarFollowers/FamiliarFollowersViewModel.swift +++ b/Mastodon/Scene/Profile/FamiliarFollowers/FamiliarFollowersViewModel.swift @@ -28,7 +28,7 @@ final class FamiliarFollowersViewModel { self.context = context self.authContext = authContext self.userFetchedResultsController = UserFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalPredicate: nil ) diff --git a/Mastodon/Scene/Profile/Favorite/FavoriteViewModel.swift b/Mastodon/Scene/Profile/Favorite/FavoriteViewModel.swift index 0dd3c7203..94e57834e 100644 --- a/Mastodon/Scene/Profile/Favorite/FavoriteViewModel.swift +++ b/Mastodon/Scene/Profile/Favorite/FavoriteViewModel.swift @@ -41,7 +41,7 @@ final class FavoriteViewModel { self.context = context self.authContext = authContext self.statusFetchedResultsController = StatusFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalTweetPredicate: nil ) diff --git a/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewModel.swift b/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewModel.swift index 91e6c36ca..c09660124 100644 --- a/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewModel.swift +++ b/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewModel.swift @@ -32,9 +32,9 @@ final class FollowedTagsViewModel: NSObject { self.context = context self.authContext = authContext self.fetchedResultsController = FollowedTagsFetchedResultController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, - user: authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext)! // fixme: + user: authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext)! // fixme: ) super.init() diff --git a/Mastodon/Scene/Profile/Follower/FollowerListViewModel.swift b/Mastodon/Scene/Profile/Follower/FollowerListViewModel.swift index ab1144e54..7be92e9e2 100644 --- a/Mastodon/Scene/Profile/Follower/FollowerListViewModel.swift +++ b/Mastodon/Scene/Profile/Follower/FollowerListViewModel.swift @@ -49,7 +49,7 @@ final class FollowerListViewModel { self.context = context self.authContext = authContext self.userFetchedResultsController = UserFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: domain, additionalPredicate: nil ) diff --git a/Mastodon/Scene/Profile/Following/FollowingListViewModel.swift b/Mastodon/Scene/Profile/Following/FollowingListViewModel.swift index e8758e645..7693e8d56 100644 --- a/Mastodon/Scene/Profile/Following/FollowingListViewModel.swift +++ b/Mastodon/Scene/Profile/Following/FollowingListViewModel.swift @@ -50,7 +50,7 @@ final class FollowingListViewModel { self.context = context self.authContext = authContext self.userFetchedResultsController = UserFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: domain, additionalPredicate: nil ) diff --git a/Mastodon/Scene/Profile/MeProfileViewModel.swift b/Mastodon/Scene/Profile/MeProfileViewModel.swift index 35da62eb7..458965579 100644 --- a/Mastodon/Scene/Profile/MeProfileViewModel.swift +++ b/Mastodon/Scene/Profile/MeProfileViewModel.swift @@ -16,7 +16,7 @@ import MastodonSDK final class MeProfileViewModel: ProfileViewModel { init(context: AppContext, authContext: AuthContext) { - let user = authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) + let user = authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext) super.init( context: context, authContext: authContext, @@ -42,8 +42,8 @@ final class MeProfileViewModel: ProfileViewModel { _ = try await context.apiService.authenticatedUserInfo(authenticationBox: authContext.mastodonAuthenticationBox).value - try await context.managedObjectContext.performChanges { - guard let me = self.authContext.mastodonAuthenticationBox.authentication.user(in: self.context.managedObjectContext) else { + try await context.cacheManagedObjectContext.performChanges { + guard let me = self.authContext.mastodonAuthenticationBox.authentication.user(in: self.context.cacheManagedObjectContext) else { assertionFailure() return } diff --git a/Mastodon/Scene/Profile/ProfileViewController.swift b/Mastodon/Scene/Profile/ProfileViewController.swift index 8c99ffc50..30da21aed 100644 --- a/Mastodon/Scene/Profile/ProfileViewController.swift +++ b/Mastodon/Scene/Profile/ProfileViewController.swift @@ -965,6 +965,6 @@ extension ProfileViewController: PagerTabStripNavigateable { private extension ProfileViewController { var currentInstance: Instance? { - authContext.mastodonAuthenticationBox.authentication.instance(in: context.managedObjectContext) + authContext.mastodonAuthenticationBox.authentication.instance(in: context.cacheManagedObjectContext) } } diff --git a/Mastodon/Scene/Profile/ProfileViewModel.swift b/Mastodon/Scene/Profile/ProfileViewModel.swift index 063e20c31..944510e23 100644 --- a/Mastodon/Scene/Profile/ProfileViewModel.swift +++ b/Mastodon/Scene/Profile/ProfileViewModel.swift @@ -85,7 +85,7 @@ class ProfileViewModel: NSObject { super.init() // bind me - self.me = authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) + self.me = authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext) $me .assign(to: \.me, on: relationshipViewModel) .store(in: &disposeBag) diff --git a/Mastodon/Scene/Profile/RemoteProfileViewModel.swift b/Mastodon/Scene/Profile/RemoteProfileViewModel.swift index 705d2806f..e74a6debb 100644 --- a/Mastodon/Scene/Profile/RemoteProfileViewModel.swift +++ b/Mastodon/Scene/Profile/RemoteProfileViewModel.swift @@ -39,7 +39,7 @@ final class RemoteProfileViewModel: ProfileViewModel { } } receiveValue: { [weak self] response in guard let self = self else { return } - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let request = MastodonUser.sortedFetchRequest request.fetchLimit = 1 request.predicate = MastodonUser.predicate(domain: domain, id: response.value.id) @@ -62,11 +62,11 @@ final class RemoteProfileViewModel: ProfileViewModel { ) let userID = response.value.account.id - let _user: MastodonUser? = try await context.managedObjectContext.perform { + let _user: MastodonUser? = try await context.cacheManagedObjectContext.perform { let request = MastodonUser.sortedFetchRequest request.predicate = MastodonUser.predicate(domain: authContext.mastodonAuthenticationBox.domain, id: userID) request.fetchLimit = 1 - return context.managedObjectContext.safeFetch(request).first + return context.cacheManagedObjectContext.safeFetch(request).first } if let user = _user { @@ -78,11 +78,11 @@ final class RemoteProfileViewModel: ProfileViewModel { authorization: authContext.mastodonAuthenticationBox.userAuthorization ) - let _user: MastodonUser? = try await context.managedObjectContext.perform { + let _user: MastodonUser? = try await context.cacheManagedObjectContext.perform { let request = MastodonUser.sortedFetchRequest request.predicate = MastodonUser.predicate(domain: authContext.mastodonAuthenticationBox.domain, id: userID) request.fetchLimit = 1 - return context.managedObjectContext.safeFetch(request).first + return context.cacheManagedObjectContext.safeFetch(request).first } self.user = _user @@ -115,7 +115,7 @@ final class RemoteProfileViewModel: ProfileViewModel { } } receiveValue: { [weak self] response in guard let self = self, let value = response.value else { return } - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let request = MastodonUser.sortedFetchRequest request.fetchLimit = 1 request.predicate = MastodonUser.predicate(domain: domain, id: value.id) diff --git a/Mastodon/Scene/Profile/Timeline/UserTimelineViewModel.swift b/Mastodon/Scene/Profile/Timeline/UserTimelineViewModel.swift index 0d85b6807..007a1dfc6 100644 --- a/Mastodon/Scene/Profile/Timeline/UserTimelineViewModel.swift +++ b/Mastodon/Scene/Profile/Timeline/UserTimelineViewModel.swift @@ -59,7 +59,7 @@ final class UserTimelineViewModel { self.authContext = authContext self.title = title self.statusFetchedResultsController = StatusFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalTweetPredicate: nil ) diff --git a/Mastodon/Scene/Profile/UserLIst/UserListViewModel.swift b/Mastodon/Scene/Profile/UserLIst/UserListViewModel.swift index 481ac8ea0..0e35b1508 100644 --- a/Mastodon/Scene/Profile/UserLIst/UserListViewModel.swift +++ b/Mastodon/Scene/Profile/UserLIst/UserListViewModel.swift @@ -46,7 +46,7 @@ final class UserListViewModel { self.authContext = authContext self.kind = kind self.userFetchedResultsController = UserFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalPredicate: nil ) diff --git a/Mastodon/Scene/Report/Report/ReportViewModel.swift b/Mastodon/Scene/Report/Report/ReportViewModel.swift index 05f1cfef8..36636bf84 100644 --- a/Mastodon/Scene/Report/Report/ReportViewModel.swift +++ b/Mastodon/Scene/Report/Report/ReportViewModel.swift @@ -57,7 +57,7 @@ class ReportViewModel { reportReasonViewModel.headline = L10n.Scene.Report.StepOne.whatsWrongWithThisPost } else { Task { @MainActor in - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let _username: String? = try? await managedObjectContext.perform { let user = user.object(in: managedObjectContext) return user?.acctWithDomain @@ -96,7 +96,7 @@ extension ReportViewModel { func report() async throws { guard !isReporting else { return } - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let _query: Mastodon.API.Reports.FileReportQuery? = try await managedObjectContext.perform { guard let user = self.user.object(in: managedObjectContext) else { return nil } diff --git a/Mastodon/Scene/Report/ReportResult/ReportResultView.swift b/Mastodon/Scene/Report/ReportResult/ReportResultView.swift index 361f5db24..948f91cf4 100644 --- a/Mastodon/Scene/Report/ReportResult/ReportResultView.swift +++ b/Mastodon/Scene/Report/ReportResult/ReportResultView.swift @@ -188,7 +188,7 @@ struct ReportActionButton: View { // emojis: [], // fields: [] // ) -// let user = try! context.managedObjectContext.fetch(request).first ?? MastodonUser.insert(into: context.managedObjectContext, property: property) +// let user = try! context.cacheManagedObjectContext.fetch(request).first ?? MastodonUser.insert(into: context.cacheManagedObjectContext, property: property) // // return ReportResultViewModel( // context: context, diff --git a/Mastodon/Scene/Report/ReportResult/ReportResultViewModel.swift b/Mastodon/Scene/Report/ReportResult/ReportResultViewModel.swift index 9caaeeb59..b6e89efe0 100644 --- a/Mastodon/Scene/Report/ReportResult/ReportResultViewModel.swift +++ b/Mastodon/Scene/Report/ReportResult/ReportResultViewModel.swift @@ -59,8 +59,8 @@ class ReportResultViewModel: ObservableObject { // end init Task { @MainActor in - guard let user = user.object(in: context.managedObjectContext) else { return } - guard let me = authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) else { return } + guard let user = user.object(in: context.cacheManagedObjectContext) else { return } + guard let me = authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext) else { return } self.relationshipViewModel.user = user self.relationshipViewModel.me = me diff --git a/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel+State.swift b/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel+State.swift index 79807cf0f..bc41cc2d5 100644 --- a/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel+State.swift +++ b/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel+State.swift @@ -80,7 +80,7 @@ extension ReportStatusViewModel.State { let maxID = viewModel.statusFetchedResultsController.statusIDs.last Task { - let managedObjectContext = viewModel.context.managedObjectContext + let managedObjectContext = viewModel.context.cacheManagedObjectContext let _userID: MastodonUser.ID? = try await managedObjectContext.perform { guard let user = viewModel.user.object(in: managedObjectContext) else { return nil } return user.id diff --git a/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel.swift b/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel.swift index c1c79af48..584144b34 100644 --- a/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel.swift +++ b/Mastodon/Scene/Report/ReportStatus/ReportStatusViewModel.swift @@ -60,7 +60,7 @@ class ReportStatusViewModel { self.user = user self.status = status self.statusFetchedResultsController = StatusFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalTweetPredicate: nil ) diff --git a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift index 4f637c68d..51b62380c 100644 --- a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift +++ b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift @@ -296,7 +296,7 @@ extension MainTabBarController { .receive(on: DispatchQueue.main) .sink { [weak self] _ in guard let self = self else { return } - if let user = self.authContext?.mastodonAuthenticationBox.authentication.user(in: self.context.managedObjectContext) { + if let user = self.authContext?.mastodonAuthenticationBox.authentication.user(in: self.context.cacheManagedObjectContext) { self.avatarURLObserver = user.publisher(for: \.avatar) .sink { [weak self, weak user] _ in guard let self = self else { return } @@ -493,7 +493,7 @@ extension MainTabBarController { ) if let user = authContext.mastodonAuthenticationBox.authentication.user( - in: context.managedObjectContext + in: context.cacheManagedObjectContext ) { user.update( property: .init( diff --git a/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift b/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift index fd3e3e45e..d398f397f 100644 --- a/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift +++ b/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift @@ -75,7 +75,7 @@ extension SidebarViewModel { let imageURL: URL? = { switch item { case .me: - let user = self.authContext?.mastodonAuthenticationBox.authentication.user(in: self.context.managedObjectContext) + let user = self.authContext?.mastodonAuthenticationBox.authentication.user(in: self.context.cacheManagedObjectContext) return user?.avatarImageURL() default: return nil @@ -132,7 +132,7 @@ extension SidebarViewModel { } .store(in: &cell.disposeBag) case .me: - guard let user = self.authContext?.mastodonAuthenticationBox.authentication.user(in: self.context.managedObjectContext) else { return } + guard let user = self.authContext?.mastodonAuthenticationBox.authentication.user(in: self.context.cacheManagedObjectContext) else { return } let currentUserDisplayName = user.displayNameWithFallback cell.accessibilityHint = L10n.Scene.AccountList.tabBarHint(currentUserDisplayName) default: diff --git a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel+Diffable.swift b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel+Diffable.swift index 92b70bf99..4622d2d98 100644 --- a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel+Diffable.swift +++ b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel+Diffable.swift @@ -35,7 +35,7 @@ extension SearchHistoryViewModel { Task { do { - let managedObjectContext = self.context.managedObjectContext + let managedObjectContext = self.context.cacheManagedObjectContext let items: [SearchHistoryItem] = try await managedObjectContext.perform { var users: [SearchHistoryItem] = [] var hashtags: [SearchHistoryItem] = [] diff --git a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift index d20f2d495..96c7975b8 100644 --- a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift +++ b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift @@ -25,7 +25,7 @@ final class SearchHistoryViewModel { init(context: AppContext, authContext: AuthContext) { self.context = context self.authContext = authContext - self.searchHistoryFetchedResultController = SearchHistoryFetchedResultController(managedObjectContext: context.managedObjectContext) + self.searchHistoryFetchedResultController = SearchHistoryFetchedResultController(managedObjectContext: context.cacheManagedObjectContext) searchHistoryFetchedResultController.domain.value = authContext.mastodonAuthenticationBox.domain searchHistoryFetchedResultController.userID.value = authContext.mastodonAuthenticationBox.userID diff --git a/Mastodon/Scene/Search/SearchDetail/SearchResult/SearchResultViewModel.swift b/Mastodon/Scene/Search/SearchDetail/SearchResult/SearchResultViewModel.swift index c4cfa0f54..d612ea59f 100644 --- a/Mastodon/Scene/Search/SearchDetail/SearchResult/SearchResultViewModel.swift +++ b/Mastodon/Scene/Search/SearchDetail/SearchResult/SearchResultViewModel.swift @@ -53,12 +53,12 @@ final class SearchResultViewModel { self.authContext = authContext self.searchScope = searchScope self.userFetchedResultsController = UserFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalPredicate: nil ) self.statusFetchedResultsController = StatusFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: authContext.mastodonAuthenticationBox.domain, additionalTweetPredicate: nil ) diff --git a/Mastodon/Scene/Settings/SettingsViewModel.swift b/Mastodon/Scene/Settings/SettingsViewModel.swift index c8eb4b365..39711217a 100644 --- a/Mastodon/Scene/Settings/SettingsViewModel.swift +++ b/Mastodon/Scene/Settings/SettingsViewModel.swift @@ -163,7 +163,7 @@ extension SettingsViewModel { ) { dataSource = SettingsSection.tableViewDiffableDataSource( for: tableView, - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, settingsAppearanceTableViewCellDelegate: settingsAppearanceTableViewCellDelegate, settingsToggleCellDelegate: settingsToggleCellDelegate ) diff --git a/Mastodon/Scene/SuggestionAccount/RecommendAccountSection.swift b/Mastodon/Scene/SuggestionAccount/RecommendAccountSection.swift index c3f477a96..5aede1313 100644 --- a/Mastodon/Scene/SuggestionAccount/RecommendAccountSection.swift +++ b/Mastodon/Scene/SuggestionAccount/RecommendAccountSection.swift @@ -36,8 +36,8 @@ extension RecommendAccountSection { switch item { case .account(let record): cell.delegate = configuration.suggestionAccountTableViewCellDelegate - context.managedObjectContext.performAndWait { - guard let user = record.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let user = record.object(in: context.cacheManagedObjectContext) else { return } cell.configure(viewModel: SuggestionAccountTableViewCell.ViewModel( user: user, diff --git a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift index 4962d3e71..381af1527 100644 --- a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift +++ b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewController.swift @@ -89,7 +89,7 @@ extension SuggestionAccountViewController: UITableViewDelegate { guard let item = tableViewDiffableDataSource.itemIdentifier(for: indexPath) else { return } switch item { case .account(let record): - guard let account = record.object(in: context.managedObjectContext) else { return } + guard let account = record.object(in: context.cacheManagedObjectContext) else { return } let cachedProfileViewModel = CachedProfileViewModel(context: context, authContext: viewModel.authContext, mastodonUser: account) _ = coordinator.present( scene: .profile(viewModel: cachedProfileViewModel), diff --git a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift index a57d71df0..f82a04726 100644 --- a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift +++ b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift @@ -39,7 +39,7 @@ final class SuggestionAccountViewModel: NSObject { self.context = context self.authContext = authContext self.userFetchedResultsController = UserFetchedResultsController( - managedObjectContext: context.managedObjectContext, + managedObjectContext: context.cacheManagedObjectContext, domain: nil, additionalPredicate: nil ) @@ -117,7 +117,7 @@ final class SuggestionAccountViewModel: NSObject { func followAllSuggestedAccounts(_ dependency: NeedsDependency & AuthContextProvider, completion: (() -> Void)? = nil) { let userRecords = userFetchedResultsController.records.compactMap { - $0.object(in: dependency.context.managedObjectContext)?.asRecord + $0.object(in: dependency.context.cacheManagedObjectContext)?.asRecord } Task { diff --git a/Mastodon/Scene/Thread/MastodonStatusThreadViewModel.swift b/Mastodon/Scene/Thread/MastodonStatusThreadViewModel.swift index 97998fd73..3de7f6243 100644 --- a/Mastodon/Scene/Thread/MastodonStatusThreadViewModel.swift +++ b/Mastodon/Scene/Thread/MastodonStatusThreadViewModel.swift @@ -89,7 +89,7 @@ extension MastodonStatusThreadViewModel { do { let request = Status.sortedFetchRequest request.predicate = Status.predicate(domain: domain, ids: ids) - let statuses = try self.context.managedObjectContext.fetch(request) + let statuses = try self.context.cacheManagedObjectContext.fetch(request) for status in statuses { dictionary[status.id] = status } @@ -128,7 +128,7 @@ extension MastodonStatusThreadViewModel { do { let request = Status.sortedFetchRequest request.predicate = Status.predicate(domain: domain, ids: childrenIDs) - let statuses = try self.context.managedObjectContext.fetch(request) + let statuses = try self.context.cacheManagedObjectContext.fetch(request) for status in statuses { dictionary[status.id] = status } diff --git a/Mastodon/Scene/Thread/RemoteThreadViewModel.swift b/Mastodon/Scene/Thread/RemoteThreadViewModel.swift index e22b11961..1226f1755 100644 --- a/Mastodon/Scene/Thread/RemoteThreadViewModel.swift +++ b/Mastodon/Scene/Thread/RemoteThreadViewModel.swift @@ -31,7 +31,7 @@ final class RemoteThreadViewModel: ThreadViewModel { authenticationBox: authContext.mastodonAuthenticationBox ) - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let request = Status.sortedFetchRequest request.fetchLimit = 1 request.predicate = Status.predicate(domain: domain, id: response.value.id) @@ -65,7 +65,7 @@ final class RemoteThreadViewModel: ThreadViewModel { guard let statusID = response.value.status?.id else { return } - let managedObjectContext = context.managedObjectContext + let managedObjectContext = context.cacheManagedObjectContext let request = Status.sortedFetchRequest request.fetchLimit = 1 request.predicate = Status.predicate(domain: domain, id: statusID) diff --git a/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift b/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift index d1814436f..4239bf9ab 100644 --- a/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift +++ b/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift @@ -38,7 +38,7 @@ extension ThreadViewModel { snapshot.appendSections([.main]) if let root = self.root { if case let .root(threadContext) = root, - let status = threadContext.status.object(in: context.managedObjectContext), + let status = threadContext.status.object(in: context.cacheManagedObjectContext), status.inReplyToID != nil { snapshot.appendItems([.topLoader], toSection: .main) @@ -79,9 +79,9 @@ extension ThreadViewModel { newSnapshot.appendSections([.main]) // top loader - let _hasReplyTo: Bool? = try? await self.context.managedObjectContext.perform { + let _hasReplyTo: Bool? = try? await self.context.cacheManagedObjectContext.perform { guard case let .root(threadContext) = root else { return nil } - guard let status = threadContext.status.object(in: self.context.managedObjectContext) else { return nil } + guard let status = threadContext.status.object(in: self.context.cacheManagedObjectContext) else { return nil } return status.inReplyToID != nil } if let hasReplyTo = _hasReplyTo, hasReplyTo { diff --git a/Mastodon/Scene/Thread/ThreadViewModel.swift b/Mastodon/Scene/Thread/ThreadViewModel.swift index 74afa44dd..7157ad341 100644 --- a/Mastodon/Scene/Thread/ThreadViewModel.swift +++ b/Mastodon/Scene/Thread/ThreadViewModel.swift @@ -59,7 +59,7 @@ class ThreadViewModel { self.mastodonStatusThreadViewModel = MastodonStatusThreadViewModel(context: context) // end init - ManagedObjectObserver.observe(context: context.managedObjectContext) + ManagedObjectObserver.observe(context: context.cacheManagedObjectContext) .sink(receiveCompletion: { completion in // do nohting }, receiveValue: { [weak self] changes in @@ -79,7 +79,7 @@ class ThreadViewModel { .sink { [weak self] root in guard let self = self else { return } guard case let .root(threadContext) = root else { return } - guard let status = threadContext.status.object(in: self.context.managedObjectContext) else { return } + guard let status = threadContext.status.object(in: self.context.cacheManagedObjectContext) else { return } // bind threadContext self.threadContext = .init( diff --git a/MastodonSDK/Sources/MastodonCore/AppContext.swift b/MastodonSDK/Sources/MastodonCore/AppContext.swift index 02966e2f8..41a9e70ce 100644 --- a/MastodonSDK/Sources/MastodonCore/AppContext.swift +++ b/MastodonSDK/Sources/MastodonCore/AppContext.swift @@ -18,7 +18,9 @@ public class AppContext: ObservableObject { public var disposeBag = Set() public let coreDataStack: CoreDataStack - public let managedObjectContext: NSManagedObjectContext + + //TODO: Explain why we have multiple contexts + public let cacheManagedObjectContext: NSManagedObjectContext public let backgroundManagedObjectContext: NSManagedObjectContext public let apiService: APIService @@ -49,30 +51,30 @@ public class AppContext: ObservableObject { public init() { let authProvider = AuthenticationServiceProvider.shared - let _coreDataStack: CoreDataStack + let coreDataStack: CoreDataStack if authProvider.authenticationMigrationRequired { - _coreDataStack = CoreDataStack(isInMemory: false) + coreDataStack = CoreDataStack(isInMemory: false) authProvider.migrateLegacyAuthentications( - in: _coreDataStack.persistentContainer.viewContext + in: coreDataStack.persistentContainer.viewContext ) } else { - _coreDataStack = CoreDataStack(isInMemory: true) + coreDataStack = CoreDataStack(isInMemory: true) } - let _managedObjectContext = _coreDataStack.persistentContainer.viewContext - _coreDataStack.persistentContainer.persistentStoreDescriptions.forEach { + let managedObjectContext = coreDataStack.persistentContainer.viewContext + coreDataStack.persistentContainer.persistentStoreDescriptions.forEach { $0.url = URL(fileURLWithPath: "/dev/null") } - let _backgroundManagedObjectContext = _coreDataStack.persistentContainer.newBackgroundContext() - coreDataStack = _coreDataStack - managedObjectContext = _managedObjectContext + let _backgroundManagedObjectContext = coreDataStack.persistentContainer.newBackgroundContext() + self.coreDataStack = coreDataStack + self.cacheManagedObjectContext = managedObjectContext backgroundManagedObjectContext = _backgroundManagedObjectContext let _apiService = APIService(backgroundManagedObjectContext: _backgroundManagedObjectContext) apiService = _apiService let _authenticationService = AuthenticationService( - managedObjectContext: _managedObjectContext, + managedObjectContext: managedObjectContext, backgroundManagedObjectContext: _backgroundManagedObjectContext, apiService: _apiService ) @@ -122,8 +124,10 @@ public class AppContext: ObservableObject { NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave, object: backgroundManagedObjectContext) .sink { [weak self] notification in guard let self = self else { return } - self.managedObjectContext.perform { - self.managedObjectContext.mergeChanges(fromContextDidSave: notification) + + //TODO: What about persistence store? Should we also merge changes there? Probably a good idea? + self.cacheManagedObjectContext.perform { + self.cacheManagedObjectContext.mergeChanges(fromContextDidSave: notification) } } .store(in: &disposeBag) diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewModel+DataSource.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewModel+DataSource.swift index 0cf093339..58dff2a45 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewModel+DataSource.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewModel+DataSource.swift @@ -60,8 +60,8 @@ extension ComposeContentViewModel { cell.statusView.frame.size.width = tableView.frame.width // configure status - context.managedObjectContext.performAndWait { - guard let replyTo = status.object(in: context.managedObjectContext) else { return } + context.cacheManagedObjectContext.performAndWait { + guard let replyTo = status.object(in: context.cacheManagedObjectContext) else { return } cell.statusView.configure(status: replyTo) } } diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewModel.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewModel.swift index dac67b607..79611c838 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/ComposeContentViewModel.swift @@ -159,15 +159,15 @@ public final class ComposeContentViewModel: NSObject, ObservableObject { self.visibility = { // default private when user locked var visibility: Mastodon.Entity.Status.Visibility = { - guard let author = authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) else { + guard let author = authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext) else { return .public } return author.locked ? .private : .public }() // set visibility for reply post if case .reply(let record) = destination { - context.managedObjectContext.performAndWait { - guard let status = record.object(in: context.managedObjectContext) else { + context.cacheManagedObjectContext.performAndWait { + guard let status = record.object(in: context.cacheManagedObjectContext) else { assertionFailure() return } @@ -222,12 +222,12 @@ public final class ComposeContentViewModel: NSObject, ObservableObject { let initialContentWithSpace = initialContent.isEmpty ? "" : initialContent + " " switch destination { case .reply(let record): - context.managedObjectContext.performAndWait { - guard let status = record.object(in: context.managedObjectContext) else { + context.cacheManagedObjectContext.performAndWait { + guard let status = record.object(in: context.cacheManagedObjectContext) else { assertionFailure() return } - let author = authContext.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) + let author = authContext.mastodonAuthenticationBox.authentication.user(in: context.cacheManagedObjectContext) var mentionAccts: [String] = [] if author?.id != status.author.id { @@ -261,9 +261,9 @@ public final class ComposeContentViewModel: NSObject, ObservableObject { // set limit let _configuration: Mastodon.Entity.Instance.Configuration? = { var configuration: Mastodon.Entity.Instance.Configuration? = nil - context.managedObjectContext.performAndWait { + context.cacheManagedObjectContext.performAndWait { let authentication = authContext.mastodonAuthenticationBox.authentication - configuration = authentication.instance(in: context.managedObjectContext)?.configuration + configuration = authentication.instance(in: context.cacheManagedObjectContext)?.configuration } return configuration }() @@ -324,7 +324,7 @@ extension ComposeContentViewModel { $authContext .sink { [weak self] authContext in guard let self = self else { return } - guard let user = authContext.mastodonAuthenticationBox.authentication.user(in: self.context.managedObjectContext) else { return } + guard let user = authContext.mastodonAuthenticationBox.authentication.user(in: self.context.cacheManagedObjectContext) else { return } self.avatarURL = user.avatarImageURL() self.name = user.nameMetaContent ?? PlaintextMetaContent(string: user.displayNameWithFallback) self.username = user.acctWithDomain @@ -560,7 +560,7 @@ extension ComposeContentViewModel { let authContext = self.authContext // author - let managedObjectContext = self.context.managedObjectContext + let managedObjectContext = self.context.cacheManagedObjectContext var _author: ManagedObjectRecord? managedObjectContext.performAndWait { _author = authContext.mastodonAuthenticationBox.authentication.user(in: managedObjectContext)?.asRecord @@ -616,7 +616,7 @@ extension ComposeContentViewModel { guard case let .editStatus(status, _) = composeContext else { return nil } // author - let managedObjectContext = self.context.managedObjectContext + let managedObjectContext = self.context.cacheManagedObjectContext var _author: ManagedObjectRecord? managedObjectContext.performAndWait { _author = authContext.mastodonAuthenticationBox.authentication.user(in: managedObjectContext)?.asRecord diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift b/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift index e887e540f..85f3f0bb1 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift @@ -229,9 +229,9 @@ extension NotificationView.ViewModel { else { return nil } var configuration: Mastodon.Entity.V2.Instance.Configuration? = nil - context.managedObjectContext.performAndWait { + context.cacheManagedObjectContext.performAndWait { let authentication = authContext.mastodonAuthenticationBox.authentication - configuration = authentication.instance(in: context.managedObjectContext)?.configurationV2 + configuration = authentication.instance(in: context.cacheManagedObjectContext)?.configurationV2 } return configuration }() diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/StatusView+ViewModel.swift b/MastodonSDK/Sources/MastodonUI/View/Content/StatusView+ViewModel.swift index c3b0caf7a..9723e0369 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/StatusView+ViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/StatusView+ViewModel.swift @@ -690,9 +690,9 @@ extension StatusView.ViewModel { } var configuration: Mastodon.Entity.V2.Instance.Configuration? = nil - context.managedObjectContext.performAndWait { + context.cacheManagedObjectContext.performAndWait { let authentication = authContext.mastodonAuthenticationBox.authentication - configuration = authentication.instance(in: context.managedObjectContext)?.configurationV2 + configuration = authentication.instance(in: context.cacheManagedObjectContext)?.configurationV2 } return configuration }() diff --git a/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift b/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift index bef484e39..0fb9c60e7 100644 --- a/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift +++ b/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift @@ -87,7 +87,7 @@ private extension FollowersCountWidgetProvider { guard let desiredAccount = configuration.account ?? authBox.authentication.user( - in: WidgetExtension.appContext.managedObjectContext + in: WidgetExtension.appContext.cacheManagedObjectContext )?.acctWithDomain else { return completion(.unconfigured) diff --git a/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift b/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift index 15c1c4d14..4a7b31597 100644 --- a/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift +++ b/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift @@ -87,7 +87,7 @@ private extension MultiFollowersCountWidgetProvider { if let configuredAccounts = configuration.accounts?.compactMap({ $0 }) { desiredAccounts = configuredAccounts } else if let currentlyLoggedInAccount = authBox.authentication.user( - in: WidgetExtension.appContext.managedObjectContext + in: WidgetExtension.appContext.cacheManagedObjectContext )?.acctWithDomain { desiredAccounts = [currentlyLoggedInAccount] } else {