WidgetKit - Should I access my SwiftData cloud kit synced DB from TimelineProvider or the Widget's view?

Hi there,

When using WidgetKit, should I should fetch my SwiftData main app's database in the TimelineProvider or directly on the widget's view?

Also, my understanding is that the widget view's code is run during the archiving process and not when the view first appears on the user's device. So, is it true that accessing in the TimelineProvider vs. accessing in the view will both run at approximately the same time, and thus will return the same DB results?

Best, Taylor

I have the same question! I am accessing SwiftData from both the TimelineProvider and also the widget's view. However, the widget's view sometimes does not update anymore and attaching Xcode to the process shows a deadlock when the widget's view is trying to access one of my model in SwiftData.

Here are two snippets of the two threads in the widget being stuck:

#0	0x00000001fc714c2c in kevent_id ()
#1	0x00000001bda85b10 in _dispatch_kq_poll ()
#2	0x00000001bda864e0 in _dispatch_event_loop_wait_for_ownership ()
#3	0x00000001bda72984 in __DISPATCH_WAIT_FOR_QUEUE__ ()
#4	0x00000001bda7254c in _dispatch_sync_f_slow ()
#5	0x00000001bdc997f8 in -[NSManagedObjectContext performBlockAndWait:] ()
#6	0x00000001bdd13e70 in NSManagedObjectContext.performAndWait<τ_0_0>(_:) ()
#7	0x000000024f2b36f8 in static _DefaultBackingData.getValue<τ_0_0>(key:managedObject:for:) ()
#8	0x000000024f2b7150 in _DefaultBackingData.getValue<τ_0_0>(forKey:) ()
#9	0x000000024f2bb870 in protocol witness for BackingData.getValue<τ_0_0>(forKey:) in conformance _DefaultBackingData<τ_0_0> ()
#10	0x000000024f26ead0 in PersistentModel.getValue<τ_0_0>(forKey:) ()
#11	0x0000000104328534 in StopDepartureTime.id.getter at /var/folders/p1/1xbpjpjd4r105gb3srs3y1g00000gn/T/swift-generated-sources/@__swiftmacro_15DepartureWidget04StopA4TimeC2id18_PersistedPropertyfMa_.swift:10
#12	0x000000010432bd20 in protocol witness for Identifiable.id.getter in conformance StopDepartureTime ()
#13	0x00000001baaa8c2c in ___lldb_unnamed_symbol181142 ()
#14	0x00000001af13895c in RawKeyPathComponent._projectReadOnly<τ_0_0, τ_0_1, τ_0_2>(_:to:endingWith:) ()
#15	0x00000001af1380b8 in closure #2 in KeyPath._projectReadOnly(from:) ()
#16	0x00000001af31cffc in partial apply for closure #2 in KeyPath._projectReadOnly(from:) ()
#17	0x00000001af1374c4 in AnyKeyPath.withBuffer<τ_0_0>(_:) ()
#18	0x00000001af137d70 in KeyPath._projectReadOnly(from:) ()
#19	0x00000001af13bd0c in swift_getAtKeyPath ()
#20	0x00000001baaa7ec4 in ___lldb_unnamed_symbol181113 ()

And thread 2:

#0	0x00000001fc714d08 in __psynch_mutexwait ()
#1	0x000000021ed1ed68 in _pthread_mutex_firstfit_lock_wait ()
#2	0x000000021ed1e7f0 in _pthread_mutex_firstfit_lock_slow ()
#3	0x00000001b97f84bc in _MovableLockLock ()
#4	0x00000001ba34d348 in ___lldb_unnamed_symbol123836 ()
#5	0x0000000258bd0568 in partial apply for closure #4 in closure #1 in static ObservationTracking._installTracking(_:willSet:didSet:) ()
#6	0x0000000258bd0680 in partial apply for thunk for @escaping @callee_guaranteed @Sendable () -> () ()
#7	0x0000000258bce878 in specialized ObservationRegistrar.Context.willSet<τ_0_0, τ_0_1>(_:keyPath:) ()
#8	0x0000000258bcf26c in specialized ObservationRegistrar.willSet<τ_0_0, τ_0_1>(_:keyPath:) ()
#9	0x0000000258bca1b8 in ObservationRegistrar.withMutation<τ_0_0, τ_0_1, τ_0_2>(of:keyPath:_:) ()
#10	0x000000024f560ac0 in ___lldb_unnamed_symbol494 ()
#11	0x000000024f560960 in ___lldb_unnamed_symbol492 ()
#12	0x000000024f561798 in ___lldb_unnamed_symbol513 ()
#13	0x0000000258bca1cc in ObservationRegistrar.withMutation<τ_0_0, τ_0_1, τ_0_2>(of:keyPath:_:) ()
#14	0x000000024f560ac0 in ___lldb_unnamed_symbol494 ()
#15	0x000000024f5610b8 in ___lldb_unnamed_symbol500 ()
#16	0x000000024f5611c4 in ___lldb_unnamed_symbol501 ()
#17	0x00000001b5b64c78 in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
#18	0x00000001b5b64640 in ___CFXRegistrationPost_block_invoke ()
#19	0x00000001b5b64588 in _CFXRegistrationPost ()
#20	0x00000001b5b63ad8 in _CFXNotificationPost ()
#21	0x00000001b4afb7e4 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
WidgetKit - Should I access my SwiftData cloud kit synced DB from TimelineProvider or the Widget's view?
 
 
Q