在 iOS 14 中编辑 TextField 时 SwiftUI 崩溃
Posted
技术标签:
【中文标题】在 iOS 14 中编辑 TextField 时 SwiftUI 崩溃【英文标题】:SwiftUI crash on editing TextField in iOS 14 【发布时间】:2021-10-18 08:32:29 【问题描述】:我在 ios 14 中编辑自定义文本字段时遇到了崩溃,并且崩溃日志没有足够的信息来了解实际导致崩溃的原因。我根据文本字段的编辑模式在 UI 中显示 2 个不同的列表。我在下面附上了一个崩溃日志,还要注意这在 iOS 15 中运行良好。
0 libobjc.A.dylib 0x25c80 objc_retain + 16
1 SwiftUI 0x4a37e0 initializeWithCopy for ViewRootBody + 32
2 SwiftUI 0x292120 implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 236
3 AttributeGraph 0x577c AG::Graph::UpdateStack::update() + 492
4 AttributeGraph 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
5 AttributeGraph 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
6 AttributeGraph 0x1d09c AGGraphGetValue + 232
7 SwiftUI 0x736090 DynamicBody.updateValue() + 276
8 SwiftUI 0x3afb00 partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32
9 AttributeGraph 0x577c AG::Graph::UpdateStack::update() + 492
10 AttributeGraph 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
11 AttributeGraph 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
12 AttributeGraph 0x1d09c AGGraphGetValue + 232
13 SwiftUI 0xb04b74 _ConditionalContent<>.ChildView.content.getter + 64
14 SwiftUI 0xb04ddc _ConditionalContent<>.ChildView.value.getter + 524
15 SwiftUI 0xb04f00 protocol witness for Rule.value.getter in conformance _ConditionalContent<A, B><>.ChildView + 44
16 SwiftUI 0x292130 implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 252
17 AttributeGraph 0x577c AG::Graph::UpdateStack::update() + 492
18 AttributeGraph 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
19 AttributeGraph 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
20 AttributeGraph 0x1d09c AGGraphGetValue + 232
21 SwiftUI 0x73bbf4 AnyViewChildList.view.getter + 28
22 SwiftUI 0x73bce8 AnyViewChildList.updateValue() + 164
23 SwiftUI 0x3afb00 partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32
24 AttributeGraph 0x577c AG::Graph::UpdateStack::update() + 492
25 AttributeGraph 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
26 AttributeGraph 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
27 AttributeGraph 0x1d09c AGGraphGetValue + 232
28 SwiftUI 0x735694 StaticBody.container.getter + 80
29 SwiftUI 0x735e00 StaticBody.updateValue() + 196
30 SwiftUI 0x3afb00 partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32
31 AttributeGraph 0x577c AG::Graph::UpdateStack::update() + 492
32 AttributeGraph 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
33 AttributeGraph 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
34 AttributeGraph 0x1d09c AGGraphGetValue + 232
35 SwiftUI 0x488c00 SecondaryLayerGeometryQuery.alignment.getter + 60
36 SwiftUI 0x488dd0 SecondaryLayerGeometryQuery.value.getter + 156
37 SwiftUI 0x1d1f3c specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 36
38 AttributeGraph 0x577c AG::Graph::UpdateStack::update() + 492
39 AttributeGraph 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
40 AttributeGraph 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
41 AttributeGraph 0x1d09c AGGraphGetValue + 232
42 SwiftUI 0x9e9238 AnimatableFrameAttribute.updateValue() + 52
43 SwiftUI 0x250d90 partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 24
44 AttributeGraph 0x577c AG::Graph::UpdateStack::update() + 492
45 AttributeGraph 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
46 AttributeGraph 0xedc4 AG::Subgraph::update(unsigned int) + 884
47 SwiftUI 0xa641c0 GraphHost.runTransaction() + 180
48 SwiftUI 0xa67010 GraphHost.runTransaction(_:) + 92
49 SwiftUI 0xa65880 GraphHost.flushTransactions() + 168
50 SwiftUI 0xa66f60 closure #1 in closure #1 in GraphHost.asyncTransaction<A>(_:mutation:style:) + 24
51 SwiftUI 0x4ed6b4 partial apply for closure #1 in ViewGraphDelegate.updateGraph<A>(body:) + 28
52 SwiftUI 0x9afc8c closure #1 in ViewRendererHost.updateViewGraph<A>(body:) + 108
53 SwiftUI 0x9a6840 ViewRendererHost.updateViewGraph<A>(body:) + 92
54 SwiftUI 0x4e9a18 ViewGraphDelegate.updateGraph<A>(body:) + 80
55 SwiftUI 0xa66f30 closure #1 in GraphHost.init(data:) + 160
56 SwiftUI 0xa682bc partial apply for closure #1 in GraphHost.asyncTransaction<A>(_:mutation:style:) + 40
57 SwiftUI 0x516008 thunk for @escaping @callee_guaranteed () -> () + 28
58 SwiftUI 0x327374 static NSRunLoop.flushObservers() + 148
59 SwiftUI 0x3272d8 closure #1 in closure #1 in static NSRunLoop.addObserver(_:) + 16
60 SwiftUI 0x322250 specialized thunk for @callee_guaranteed () -> (@error @owned Error) + 24
61 libswiftObjectiveC.dylib 0x1f24 autoreleasepool<A>(invoking:) + 64
62 SwiftUI 0x3272b8 closure #1 in static NSRunLoop.addObserver(_:) + 64
63 SwiftUI 0x327410 @objc closure #1 in static NSRunLoop.addObserver(_:) + 56
64 CoreFoundation 0xa2588 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
65 CoreFoundation 0x9c6ac __CFRunLoopDoObservers + 572
66 CoreFoundation 0x9cc58 __CFRunLoopRun + 1052
67 CoreFoundation 0x9c308 CFRunLoopRunSpecific + 600
68 GraphicsServices 0x3734 GSEventRunModal + 164
69 UIKitCore 0xbca75c -[UIApplication _run] + 1072
70 UIKitCore 0xbcffcc UIApplicationMain + 168
71 0x52e28 main + 23 (__hidden#4115_:23)
72 libdyld.dylib 0x1cf8 start + 4
请在我的自定义文本字段下方找到
struct ClearableTextField: View
@State private var fieldState: FieldState = .normal
let label: String
let symbol: String?
@Binding var text: String
let validationRegex: String?
let inputRegex: String?
let errorText: String?
var onEditingChanged: ((Bool) -> Void)?
var body: some View
VStack(alignment: .leading)
TextField(label, text: $text, onEditingChanged: edit in
onEditingChanged?(edit)
fieldState = edit ? .focused : .normal
if !edit
guard let validationRegex = validationRegex,
!text.matches(validationRegex) else return
fieldState = .error
)
.onReceive(Just(text)) newValue in
guard let inputRegex = inputRegex, let value = newValue.last else return
if String(value).matches(inputRegex)
self.text = newValue
else
self.text.removeLast()
// .textFieldStyle(ClearableTextFieldStyle(text: $text,
// fieldState: $fieldState,
// symbol: symbol))
if fieldState == .error, let errorText = errorText
Text(errorText)
.foregroundColor(fieldState.color)
.font(.caption)
.padding(.bottom, 12)
init(label: String,
symbol: String? = nil,
text: Binding<String>,
validationRegex: String? = nil,
inputRegex: String? = nil,
errorText: String? = nil,
onEditingChanged: ((Bool) -> Void)? = nil)
self.label = label
self.symbol = symbol
_text = text
self.validationRegex = validationRegex
self.inputRegex = inputRegex
self.errorText = errorText
self.onEditingChanged = onEditingChanged
【问题讨论】:
我认为您在理解崩溃日志方面缺少一步:您需要对其进行符号化。此外,如果它出现在 XCode 中,您可能会从那里检索它,它可能会直接出现在问题行上。 @Larme 我编辑了问题并添加了符号化日志,顺便说一句,此日志来自 firebase。 您的自定义文本字段是什么?系统框架中的调用堆栈崩溃在这里没有帮助。 @EugeneDudnyk 我已经添加了我的 TextField 【参考方案1】:我解决了我的问题,将索引添加到列表中,这样它就不会转到每个项目来比较更改...
每当更改状态属性时,例如文本字段,整个视图都会重新渲染,因此所有列表都会再次比较它们的值
因此,这解决了我的问题
https://www.hackingwithswift.com/articles/210/how-to-fix-slow-list-updates-in-swiftui
【讨论】:
以上是关于在 iOS 14 中编辑 TextField 时 SwiftUI 崩溃的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI iOS 14 beta TextField 100% CPU
损坏的内联 datePicker 作为 TextField Swift iOS 14 的 InputView