Choose a better name for the former managedObjectContext

This commit is contained in:
Nathan Mattes 2023-06-20 20:05:39 +02:00
parent 59ee2d333d
commit e040402a58
73 changed files with 194 additions and 190 deletions

View File

@ -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:

View File

@ -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,

View File

@ -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

View File

@ -28,10 +28,10 @@ extension SearchHistorySection {
) -> UICollectionViewDiffableDataSource<SearchHistorySection, SearchHistoryItem> {
let userCellRegister = UICollectionView.CellRegistration<SearchHistoryUserCollectionViewCell, ManagedObjectRecord<MastodonUser>> { 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<UICollectionViewListCell, ManagedObjectRecord<Tag>> { 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

View File

@ -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

View File

@ -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<PollSection, PollItem>(tableView: statusView.pollTableView) { tableView, indexPath, item in
switch item {
case .history:

View File

@ -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

View File

@ -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

View File

@ -47,7 +47,7 @@ extension DataSourceFacade {
provider: DataSourceProvider & AuthContextProvider,
tag: ManagedObjectRecord<Tag>
) 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

View File

@ -65,7 +65,7 @@ extension DataSourceFacade {
status: ManagedObjectRecord<Status>,
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?

View File

@ -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
)

View File

@ -17,7 +17,7 @@ extension DataSourceFacade {
status: ManagedObjectRecord<Status>
) 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<MastodonUser>
) 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<MastodonUser>
) 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

View File

@ -58,8 +58,8 @@ extension DataSourceFacade {
dependency: NeedsDependency,
status: ManagedObjectRecord<Status>
) 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<Status>? = 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<MastodonUser>? = try? await managedObjectContext.perform {
guard let user = menuContext.author?.object(in: managedObjectContext) else { return nil }
return ManagedObjectRecord<MastodonUser>(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<MastodonUser>? = try? await managedObjectContext.perform {
guard let user = menuContext.author?.object(in: managedObjectContext) else { return nil }
return ManagedObjectRecord<MastodonUser>(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<MastodonUser>? = try? await managedObjectContext.perform {
guard let user = menuContext.author?.object(in: managedObjectContext) else { return nil }
return ManagedObjectRecord<MastodonUser>(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<Status> = 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<Status>
) 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)
}

View File

@ -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
)

View File

@ -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
}

View File

@ -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:

View File

@ -30,8 +30,8 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut
return
}
let _author: ManagedObjectRecord<MastodonUser>? = try await self.context.managedObjectContext.perform {
guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
let _author: ManagedObjectRecord<MastodonUser>? = 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<MastodonUser>? = try await self.context.managedObjectContext.perform {
guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
let _author: ManagedObjectRecord<MastodonUser>? = 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<Status>? = try await self.context.managedObjectContext.perform {
guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
let _status: ManagedObjectRecord<Status>? = 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<MastodonUser>? = try await self.context.managedObjectContext.perform {
guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
let _author: ManagedObjectRecord<MastodonUser>? = 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<Status>? = try await self.context.managedObjectContext.perform {
guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
let _status: ManagedObjectRecord<Status>? = 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<Status>? = try await self.context.managedObjectContext.perform {
guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
let _status: ManagedObjectRecord<Status>? = 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<Status>? = try await self.context.managedObjectContext.perform {
// guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
// let _status: ManagedObjectRecord<Status>? = 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<Status>? = try await self.context.managedObjectContext.perform {
guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
let _status: ManagedObjectRecord<Status>? = 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<Status>? = try await self.context.managedObjectContext.perform {
guard let notification = notification.object(in: self.context.managedObjectContext) else { return nil }
let _status: ManagedObjectRecord<Status>? = 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)
}

View File

@ -37,13 +37,13 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte
case .none:
break
case .reply:
let _replyToAuthor: ManagedObjectRecord<MastodonUser>? = try? await context.managedObjectContext.perform {
guard let status = status.object(in: self.context.managedObjectContext) else { return nil }
let _replyToAuthor: ManagedObjectRecord<MastodonUser>? = 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<Poll>?
@ -476,8 +476,8 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte
assertionFailure("only works for status data provider")
return
}
let _author: ManagedObjectRecord<MastodonUser>? = try await self.context.managedObjectContext.perform {
guard let _status = status.object(in: self.context.managedObjectContext) else { return nil }
let _author: ManagedObjectRecord<MastodonUser>? = 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)

View File

@ -66,8 +66,8 @@ extension StatusTableViewControllerNavigateableCore where Self: DataSourceProvid
case .status(let record):
return record
case .notification(let record):
let _statusRecord: ManagedObjectRecord<Status>? = try? await context.managedObjectContext.perform {
guard let notification = record.object(in: self.context.managedObjectContext) else { return nil }
let _statusRecord: ManagedObjectRecord<Status>? = 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)
}

View File

@ -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<Status>? = try await managedObjectContext.perform {
guard let notification = notification.object(in: managedObjectContext) else { return nil }

View File

@ -116,7 +116,7 @@ extension AccountListViewController {
tableView.delegate = self
viewModel.setupDiffableDataSource(
tableView: tableView,
managedObjectContext: context.managedObjectContext
managedObjectContext: context.cacheManagedObjectContext
)
viewModel.dataSourceDidUpdate

View File

@ -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
)

View File

@ -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 })

View File

@ -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
)

View File

@ -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
)

View File

@ -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))
}

View File

@ -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 }

View File

@ -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:

View File

@ -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

View File

@ -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 {

View File

@ -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 }

View File

@ -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 }

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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
)

View File

@ -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
)

View File

@ -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
)

View File

@ -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()

View File

@ -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
)

View File

@ -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
)

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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
)

View File

@ -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
)

View File

@ -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 }

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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
)

View File

@ -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(

View File

@ -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:

View File

@ -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] = []

View File

@ -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

View File

@ -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
)

View File

@ -163,7 +163,7 @@ extension SettingsViewModel {
) {
dataSource = SettingsSection.tableViewDiffableDataSource(
for: tableView,
managedObjectContext: context.managedObjectContext,
managedObjectContext: context.cacheManagedObjectContext,
settingsAppearanceTableViewCellDelegate: settingsAppearanceTableViewCellDelegate,
settingsToggleCellDelegate: settingsToggleCellDelegate
)

View File

@ -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,

View File

@ -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),

View File

@ -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 {

View File

@ -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
}

View File

@ -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)

View File

@ -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 {

View File

@ -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(

View File

@ -18,7 +18,9 @@ public class AppContext: ObservableObject {
public var disposeBag = Set<AnyCancellable>()
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)

View File

@ -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)
}
}

View File

@ -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<MastodonUser>?
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<MastodonUser>?
managedObjectContext.performAndWait {
_author = authContext.mastodonAuthenticationBox.authentication.user(in: managedObjectContext)?.asRecord

View File

@ -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
}()

View File

@ -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
}()

View File

@ -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)

View File

@ -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 {