Skip to content

Commit

Permalink
Unsubscribe model manager when in background (#3179)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal authored Nov 20, 2024
1 parent 4b866ee commit c088157
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
5 changes: 0 additions & 5 deletions Sources/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
private func setupModels() {
// Force Realm migration to happen now
_ = Realm.live()

Current.modelManager.cleanup().cauterize()
Current.modelManager.subscribe(isAppInForeground: {
UIApplication.shared.applicationState == .active
})
Action.setupObserver()
NotificationCategory.setupObserver()
WidgetOpenPageIntent.setupObserver()
Expand Down
5 changes: 5 additions & 0 deletions Sources/App/Scenes/WebViewSceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ final class WebViewSceneDelegate: NSObject, UIWindowSceneDelegate {

func sceneDidEnterBackground(_ scene: UIScene) {
DataWidgetsUpdater.update()
Current.modelManager.unsubscribe()
}

func sceneWillEnterForeground(_ scene: UIScene) {
DataWidgetsUpdater.update()
Current.modelManager.cleanup().cauterize()
Current.modelManager.subscribe(isAppInForeground: {
UIApplication.shared.applicationState == .active
})
}

func windowScene(
Expand Down
15 changes: 8 additions & 7 deletions Sources/Shared/API/Models/ModelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,14 @@ public class ModelManager: ServerObserver {
token.cancel()
return
}

guard ModelManager.isAppInForeground() else { return }
let entities = value.all.filter { $0.domain == domain }
if entities != lastEntities {
manager.store(type: type, from: server, sourceModels: entities).cauterize()
lastEntities = entities
appEntitiesModel.updateModel(entities, server: server)
DispatchQueue.main.async {
guard ModelManager.isAppInForeground() else { return }
let entities = value.all.filter { $0.domain == domain }
if entities != lastEntities {
manager.store(type: type, from: server, sourceModels: entities).cauterize()
lastEntities = entities
appEntitiesModel.updateModel(entities, server: server)
}
}
}
},
Expand Down

0 comments on commit c088157

Please sign in to comment.