Parent background context to PSC to prevent hangs when updating widget
This commit is contained in:
parent
2380eeee4a
commit
c12b9ae879
|
@ -19,8 +19,9 @@ public class PersistentContainer: NSPersistentContainer, @unchecked Sendable {
|
||||||
|
|
||||||
public private(set) lazy var backgroundContext: NSManagedObjectContext = {
|
public private(set) lazy var backgroundContext: NSManagedObjectContext = {
|
||||||
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
||||||
// todo: should the background context really be parented to the view context, or should they both be direct children of the PSC?
|
// the background context needs to be parented directly to the PSC
|
||||||
context.parent = self.viewContext
|
// if it's parented to the viewContext, it blocks the viewContext (and potentially the main thread) when it needs to look things up
|
||||||
|
context.persistentStoreCoordinator = self.persistentStoreCoordinator
|
||||||
return context
|
return context
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import Persistence
|
||||||
import OSLog
|
import OSLog
|
||||||
|
|
||||||
private let logger = Logger(subsystem: "net.shadowfacts.Reader", category: "WidgetHelper")
|
private let logger = Logger(subsystem: "net.shadowfacts.Reader", category: "WidgetHelper")
|
||||||
|
private let signposter = OSSignposter(logger: logger)
|
||||||
|
|
||||||
struct WidgetHelper {
|
struct WidgetHelper {
|
||||||
private init() {}
|
private init() {}
|
||||||
|
@ -26,7 +27,9 @@ struct WidgetHelper {
|
||||||
req.sortDescriptors = [NSSortDescriptor(key: "published", ascending: false)]
|
req.sortDescriptors = [NSSortDescriptor(key: "published", ascending: false)]
|
||||||
req.fetchLimit = 32
|
req.fetchLimit = 32
|
||||||
req.predicate = NSPredicate(format: "read = NO")
|
req.predicate = NSPredicate(format: "read = NO")
|
||||||
|
let state = signposter.beginInterval("fetch")
|
||||||
var items = (try? context.fetch(req)) ?? []
|
var items = (try? context.fetch(req)) ?? []
|
||||||
|
signposter.endInterval("fetch", state)
|
||||||
|
|
||||||
var prioritizedItems: [Item] = []
|
var prioritizedItems: [Item] = []
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue