Skip to content

Commit

Permalink
Fix UI not updating properly, closes (hopefully) binwiederhier/ntfy#267
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed May 24, 2022
1 parent 905fba3 commit 9060af5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 5 additions & 2 deletions ntfy/App/AppMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Firebase
// Must have before release:
// TODO: Verify whether model version needs to be specified
// TODO: Disallow adding same topic twice!!
// TODO: When clicked, the notification does not open the topic, but instead just shows the last view
// TODO: The notification does not make a sound or vibrate on the Apple Watch
// TODO: When opening a topic, the notifications in the Apple notification center should be dismissed automatically
// TODO: Errors are not shown to the user, but instead just logged

// Nice to have
// TODO: Make notification click open detail view
Expand Down Expand Up @@ -36,8 +40,7 @@ struct AppMain: App {
// That post also explains how to start SwiftUI from AppDelegate if that's ever needed.

Log.d(tag, "App became active, refreshing objects")
store.context.refreshAllObjects()
store.objectWillChange.send()
store.hardRefresh()
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions ntfy/Persistence/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Store: ObservableObject {
let storeUrl = (inMemory) ? URL(fileURLWithPath: "/dev/null") : FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: Store.appGroup)!
.appendingPathComponent("ntfy.sqlite")
print(storeUrl)
let description = NSPersistentStoreDescription(url: storeUrl)
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)

Expand All @@ -44,14 +43,8 @@ class Store: ObservableObject {
.publisher(for: .NSPersistentStoreRemoteChange)
.sink { value in
Log.d(Store.tag, "Remote change detected, refreshing view", value)

// Hack: This is the only way I could make the UI update the subscription list.
// I'm pretty sure I got the @FetchRequest wrong, but I don
_ = try? self.context.fetch(Subscription.fetchRequest())

DispatchQueue.main.async {
self.objectWillChange.send()
self.container.viewContext.refreshAllObjects()
self.hardRefresh()
}
}
.store(in: &cancellables)
Expand Down Expand Up @@ -167,7 +160,17 @@ class Store: ObservableObject {
// UI properly when it is in the foreground and the app extension stores a notification.

context.rollback()
hardRefresh()
}

func hardRefresh() {
// `refreshAllObjects` only refreshes objects from which the cache is invalid. With a staleness intervall of -1 the cache never invalidates.
// We set the `stalenessInterval` to 0 to make sure that changes in the app extension get processed correctly.
// From: https://www.avanderlee.com/swift/core-data-app-extension-data-sharing/

context.stalenessInterval = 0
context.refreshAllObjects()
context.stalenessInterval = -1
}
}

Expand Down

0 comments on commit 9060af5

Please sign in to comment.