我的应用程序不断在后台崩溃,我不知道为什么。我正在使用 SwiftData 和 SwiftUI。我正在设置一个像这样的
.backgroundTask
:
@main
struct MyAppName: App {
var sharedModelContainer: ModelContainer = {
do {
let schema = Schema([MyModel.self])
return try ModelContainer(
for: schema,
configurations: ModelConfiguration(
schema: schema,
isStoredInMemoryOnly: false
)
)
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
.backgroundTask(.appRefresh("myTaskId")) {
let container = await sharedModelContainer
await RefreshManager.handleBackgroundRefresh(container)
}
}
}
RefreshManager.handleBackgroundRefresh(...)
继续加载数据,然后通过 SwiftData 为其插入模型。问题很可能发生在某个地方,但我无法进一步缩小范围。在前台运行时似乎工作正常。
这是我收到的崩溃报告:
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000001a1bb98c0
Termination Reason: SIGNAL 5 Trace/BPT trap: 5
Terminating Process: exc handler [809]
Triggered by Thread: 0
Thread 0 name:
Thread 0 Crashed:
0 libswiftCore.dylib 0x00000001a1bb98c0 _assertionFailure(_:_:file:line:flags:) + 264 (AssertCommon.swift:144)
1 libswiftCore.dylib 0x00000001a1c27d14 swift_unexpectedError + 664 (ErrorType.swift:188)
2 _SwiftData_SwiftUI 0x000000024310cd78 one-time initialization function for empty + 300 (ModelContainer+Extensions.swift:5)
3 libdispatch.dylib 0x00000001ab16add4 _dispatch_client_callout + 20 (object.m:576)
4 libdispatch.dylib 0x00000001ab16c654 _dispatch_once_callout + 32 (once.c:52)
5 _SwiftData_SwiftUI 0x000000024310cdf8 one-time initialization function for empty + 124 (ModelContainer+Extensions.swift:12)
6 libdispatch.dylib 0x00000001ab16add4 _dispatch_client_callout + 20 (object.m:576)
7 libdispatch.dylib 0x00000001ab16c654 _dispatch_once_callout + 32 (once.c:52)
8 _SwiftData_SwiftUI 0x0000000243122170 key path getter for EnvironmentValues.modelContext : EnvironmentValues + 140 (<compiler-generated>:0)
9 libswiftCore.dylib 0x00000001a1ce4628 RawKeyPathComponent._projectReadOnly<A, B, C>(_:to:endingWith:) + 1012 (KeyPath.swift:1701)
10 libswiftCore.dylib 0x00000001a1ce3ddc KeyPath._projectReadOnly(from:) + 1036 (KeyPath.swift:331)
11 libswiftCore.dylib 0x00000001a1ce8348 swift_getAtKeyPath + 24 (KeyPath.swift:2029)
12 SwiftUI 0x00000001a7af4814 EnvironmentBox.update(property:phase:) + 872 (Environment.swift:273)
13 SwiftUI 0x00000001a782a074 static BoxVTable.update(ptr:property:phase:) + 396 (DynamicPropertyBuffer.swift:294)
14 SwiftUI 0x00000001a78297b0 _DynamicPropertyBuffer.update(container:phase:) + 104 (DynamicPropertyBuffer.swift:215)
15 SwiftUI 0x00000001a887fb78 closure #1 in closure #1 in DynamicBody.updateValue() + 104 (DynamicProperty.swift:447)
16 SwiftUI 0x00000001a887fbb8 partial apply for closure #1 in closure #1 in DynamicBody.updateValue() + 28 (<compiler-generated>:0)
17 libswiftCore.dylib 0x00000001a1bcc068 withUnsafeMutablePointer<A, B>(to:_:) + 28 (LifetimeManager.swift:82)
18 SwiftUI 0x00000001a887f9dc closure #1 in DynamicBody.updateValue() + 408 (DynamicProperty.swift:446)
19 SwiftUI 0x00000001a887f5c0 DynamicBody.updateValue() + 712 (DynamicProperty.swift:445)
20 SwiftUI 0x00000001a71e8bf8 partial apply for implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
21 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
22 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
23 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
24 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
25 SwiftUI 0x00000001a887f548 DynamicBody.updateValue() + 592 (DynamicProperty.swift:444)
26 SwiftUI 0x00000001a71e8bf8 partial apply for implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
27 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
28 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
29 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
30 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
31 SwiftUI 0x00000001a830f3ac Attribute.syncMainIfReferences<A>(do:) + 236 (AttributeGraphAdditions.swift:84)
32 SwiftUI 0x00000001a81250f8 closure #1 in GeometryReader.Child.updateValue() + 108 (GeometryReader.swift:85)
33 SwiftUI 0x00000001a81268ac partial apply for closure #1 in GeometryReader.Child.updateValue() + 24 (<compiler-generated>:0)
34 SwiftUI 0x00000001a7a777d0 closure #1 in _withObservation<A>(do:) + 44 (ObservationUtils.swift:26)
35 SwiftUI 0x00000001a81268cc partial apply for closure #1 in _withObservation<A>(do:) + 24 (<compiler-generated>:0)
36 libswiftCore.dylib 0x00000001a1bcc068 withUnsafeMutablePointer<A, B>(to:_:) + 28 (LifetimeManager.swift:82)
37 SwiftUI 0x00000001a8124d88 GeometryReader.Child.updateValue() + 1108 (GeometryReader.swift:84)
38 SwiftUI 0x00000001a71e8bf8 partial apply for implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
39 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
40 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
41 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
42 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
43 SwiftUI 0x00000001a819fc40 DynamicViewContainer.view.getter + 48
44 SwiftUI 0x00000001a819fef0 DynamicViewList.updateValue() + 424 (DynamicView.swift:147)
45 SwiftUI 0x00000001a71e8bf8 partial apply for implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
46 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
47 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
48 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
49 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
50 SwiftUI 0x00000001a8035204 specialized implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 168 (<compiler-generated>:0)
51 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
52 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
53 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
54 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
55 SwiftUI 0x00000001a770a598 DynamicLayoutViewAdaptor.updatedItems() + 68 (DynamicLayoutView.swift:166)
56 SwiftUI 0x00000001a7441efc specialized DynamicContainerInfo.updateItems(disableTransitions:) + 68 (DynamicContainer.swift:468)
57 SwiftUI 0x00000001a7440e98 specialized DynamicContainerInfo.updateValue() + 376 (DynamicContainer.swift:326)
58 SwiftUI 0x00000001a760b04c specialized implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 24 (<compiler-generated>:0)
59 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
60 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
61 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
62 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
63 SwiftUI 0x00000001a7b14d34 DynamicPreferenceCombiner.info.getter + 96 (DynamicContainer.swift:0)
64 SwiftUI 0x00000001a7b14f4c DynamicPreferenceCombiner.value.getter + 228 (DynamicContainer.swift:877)
65 SwiftUI 0x00000001a72d30ac implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 324 (<compiler-generated>:0)
66 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
67 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
68 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
69 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
70 SwiftUI 0x00000001a77b6d70 HostPreferencesTransform.updateValue() + 104 (PreferenceTransformModifier.swift:123)
71 SwiftUI 0x00000001a71e8bf8 partial apply for implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
72 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
73 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
74 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
75 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
76 SwiftUI 0x00000001a77b6d70 HostPreferencesTransform.updateValue() + 104 (PreferenceTransformModifier.swift:123)
77 SwiftUI 0x00000001a71e8bf8 partial apply for implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
78 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
79 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
80 AttributeGraph 0x00000001cbd42810 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 720 (ag-graph.cc:1429)
81 AttributeGraph 0x00000001cbd423a4 AGGraphGetValue + 228 (AGGraph.mm:701)
82 SwiftUI 0x00000001a77b6d70 HostPreferencesTransform.updateValue() + 104 (PreferenceTransformModifier.swift:123)
83 SwiftUI 0x00000001a71e8bf8 partial apply for implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
84 AttributeGraph 0x00000001cbd4c240 AG::Graph::UpdateStack::update() + 512 (ag-graph-update.cc:578)
85 AttributeGraph 0x00000001cbd42f38 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 424 (ag-graph-update.cc:719)
86 AttributeGraph 0x00000001cbd4a538 AG::Graph::value_ref(AG::AttributeID, unsigned int, AGSwiftMetadata const*, unsigned char&) + 288 (ag-graph.cc:1201)
87 AttributeGraph 0x00000001cbd4d470 AGGraphGetWeakValue + 388 (AGGraph.mm:735)
88 SwiftUI 0x00000001a8708424 GraphHost.updatePreferences() + 48 (GraphHost.swift:773)
89 SwiftUI 0x00000001a7d2a940 ViewGraph.updateOutputs() + 300 (ViewGraph.swift:614)
90 SwiftUI 0x00000001a87db674 closure #2 in closure #1 in ViewRendererHost.render(interval:updateDisplayList:) + 1268 (ViewRendererHost.swift:243)
91 SwiftUI 0x00000001a87db028 closure #1 in ViewRendererHost.render(interval:updateDisplayList:) + 660 (ViewRendererHost.swift:235)
92 SwiftUI 0x00000001a87d9170 ViewRendererHost.render(interval:updateDisplayList:) + 408 (<compiler-generated>:0)
93 SwiftUI 0x00000001a77712d4 closure #1 in _UIHostingView.requestImmediateUpdate() + 72 (_UIHostingView.swift:701)
94 SwiftUI 0x00000001a8846690 thunk for @escaping @callee_guaranteed () -> () + 36
95 libdispatch.dylib 0x00000001ab16913c _dispatch_call_block_and_release + 32 (init.c:1530)
96 libdispatch.dylib 0x00000001ab16add4 _dispatch_client_callout + 20 (object.m:576)
97 libdispatch.dylib 0x00000001ab1795a4 _dispatch_main_queue_drain + 988 (queue.c:7898)
98 libdispatch.dylib 0x00000001ab1791b8 _dispatch_main_queue_callback_4CF + 44 (queue.c:8058)
99 CoreFoundation 0x00000001a32a551c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CFRunLoop.c:1780)
100 CoreFoundation 0x00000001a32a2218 __CFRunLoopRun + 1996 (CFRunLoop.c:3149)
101 CoreFoundation 0x00000001a32a1968 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
102 GraphicsServices 0x00000001e759f4e0 GSEventRunModal + 164 (GSEvent.c:2196)
103 UIKitCore 0x00000001a5714edc -[UIApplication _run] + 888 (UIApplication.m:3692)
104 UIKitCore 0x00000001a5714518 UIApplicationMain + 340 (UIApplication.m:5282)
105 SwiftUI 0x00000001a80db860 closure #1 in KitRendererCommon(_:) + 168 (UIKitApp.swift:51)
106 SwiftUI 0x00000001a80db6a8 runApp<A>(_:) + 152 (UIKitApp.swift:14)
107 SwiftUI 0x00000001a7cf79fc static App.main() + 132 (App.swift:114)
108 MyAppName 0x0000000100e6d120 static MyAppNameApp.$main() + 52 (MyAppNameApp.swift:0)
109 MyAppName 0x0000000100e6d120 main + 64
110 dyld 0x00000001c67c2d84 start + 2240 (dyldMain.cpp:1298)
报告还说
key path getter for EnvironmentValues.modelContext
,这看起来很奇怪。后台任务中没有使用环境变量,这是否与我设置后台任务的方式或传递容器的方式有关?知道我可以从哪里开始寻找问题吗?
看起来您错过了
static
上的
var sharedModelContainer