Fix potential race between adding notification to NSManagedObjectContext and displaying VC
This commit is contained in:
parent
e8a492ef7d
commit
670047af6f
|
@ -375,13 +375,14 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer, @unchecked Se
|
|||
}
|
||||
}
|
||||
|
||||
func addAll(notifications: [Pachyderm.Notification], completion: (() -> Void)? = nil) {
|
||||
backgroundContext.perform {
|
||||
func addAll(notifications: [Pachyderm.Notification], in context: NSManagedObjectContext? = nil, completion: (() -> Void)? = nil) {
|
||||
let context = context ?? backgroundContext
|
||||
context.perform {
|
||||
let statuses = notifications.compactMap { $0.status }
|
||||
let accounts = notifications.map { $0.account }
|
||||
statuses.forEach { self.upsert(status: $0, context: self.backgroundContext) }
|
||||
accounts.forEach { self.upsert(account: $0, in: self.backgroundContext) }
|
||||
self.save(context: self.backgroundContext)
|
||||
statuses.forEach { self.upsert(status: $0, context: context) }
|
||||
accounts.forEach { self.upsert(account: $0, in: context) }
|
||||
self.save(context: context)
|
||||
completion?()
|
||||
statuses.forEach { self.statusSubject.send($0.id) }
|
||||
accounts.forEach { self.accountSubject.send($0.id) }
|
||||
|
|
|
@ -48,7 +48,9 @@ class NotificationLoadingViewController: UIViewController {
|
|||
do {
|
||||
let (notification, _) = try await mastodonController.run(request)
|
||||
await withCheckedContinuation { continuation in
|
||||
mastodonController.persistentContainer.addAll(notifications: [notification]) {
|
||||
let container = mastodonController.persistentContainer
|
||||
let context = container.viewContext
|
||||
container.addAll(notifications: [notification], in: context) {
|
||||
continuation.resume()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue