ios 帐户创建时崩溃,错误和按钮代码包括在内

cld4siwp  于 2023-05-23  发布在  iOS
关注(0)|答案(1)|浏览(164)

我在尝试创建帐户的20%左右的时间中会出现以下错误:有人能帮我诊断一下这个问题吗
崩溃:com.apple.main-thread EXC_BAD_ACCESS克恩_INVALID_ADDRESS 0x00000000000010

Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x2820 objc_msgSend + 32
1  UIKitCore                      0x2004a4 -[UIViewController dealloc] + 772
2  UIKitCore                      0x1fe0cc -[UINavigationController dealloc] + 308
3  UIKitCore                      0x486bbc -[_UISplitViewControllerColumnContents .cxx_destruct] + 44
4  libobjc.A.dylib                0x20a4 object_cxxDestructFromClass(objc_object*, objc_class*) + 116
5  libobjc.A.dylib                0x6e00 objc_destructInstance + 80
6  libobjc.A.dylib                0x104fc _objc_rootDealloc + 80
7  CoreFoundation                 0x2a26c cow_cleanup + 168
8  CoreFoundation                 0x62538 -[__NSDictionaryM dealloc] + 148
9  libobjc.A.dylib                0x20a4 object_cxxDestructFromClass(objc_object*, objc_class*) + 116
10 libobjc.A.dylib                0x6e00 objc_destructInstance + 80
11 libobjc.A.dylib                0x104fc _objc_rootDealloc + 80
12 libobjc.A.dylib                0x20a4 object_cxxDestructFromClass(objc_object*, objc_class*) + 116
13 libobjc.A.dylib                0x6e00 objc_destructInstance + 80
14 libobjc.A.dylib                0x104fc _objc_rootDealloc + 80
15 UIKitCore                      0x2008d8 -[UIResponder dealloc] + 124
16 UIKitCore                      0x2005dc -[UIViewController dealloc] + 1084
17 libobjc.A.dylib                0x21d4 AutoreleasePoolPage::releaseUntil(objc_object**) + 196
18 libobjc.A.dylib                0x5bdc objc_autoreleasePoolPop + 256
19 UIKitCore                      0x1a0444 -[_UIAfterCACommitBlock run] + 92
20 UIKitCore                      0x1a0364 -[_UIAfterCACommitQueue flush] + 168
21 UIKitCore                      0x1a0278 _runAfterCACommitDeferredBlocks + 496
22 UIKitCore                      0x3eee4 _cleanUpAfterCAFlushAndRunDeferredBlocks + 108
23 UIKitCore                      0x4fd608 _UIApplicationFlushCATransaction + 72
24 UIKitCore                      0x64de00 _UIUpdateSequenceRun + 84
25 UIKitCore                      0xcb1944 schedulerStepScheduledMainSection + 144
26 UIKitCore                      0xcb0ea0 runloopSourceCallback + 92
27 CoreFoundation                 0xd3208 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
28 CoreFoundation                 0xdf864 __CFRunLoopDoSource0 + 176
29 CoreFoundation                 0x646c8 __CFRunLoopDoSources0 + 244
30 CoreFoundation                 0x7a1c4 __CFRunLoopRun + 828
31 CoreFoundation                 0x7f4dc CFRunLoopRunSpecific + 612
32 GraphicsServices               0x135c GSEventRunModal + 164
33 UIKitCore                      0x39d37c -[UIApplication _run] + 888
34 UIKitCore                      0x39cfe0 UIApplicationMain + 340
35 SwiftUI                        0x1bc3d8 OUTLINED_FUNCTION_895 + 2472
36 SwiftUI                        0x1229b0 block_copy_helper.1 + 496
37 SwiftUI                        0x10ca54 OUTLINED_FUNCTION_901 + 2752
38 AppName                   0x7844 main + 23 (AppName.swift:23)
39 ???                            0x1ab1b0dec (Missing)

当我在模拟器中运行时,我不会遇到这种崩溃,只有当我通过Firestore应用程序分发运行时。
导致崩溃的按钮代码和viewModels:

@StateObject var viewModel: AuthViewModel.CreateAccountViewModel
...
Button("Create Account", action: viewModel.submit)

AuthViewModel:

func makeCreateAccountViewModel() -> CreateAccountViewModel {
        return CreateAccountViewModel(action: authService.createAccount(name:email:password:))
    }

class CreateAccountViewModel: FormViewModel<(name: String, email: String, password: String)> {
        convenience init(action: @escaping Action) {
            self.init(initialValue: (name: "", email: "", password: ""), action: action)
        }
    }

FormViewModel:

@MainActor
@dynamicMemberLookup
class FormViewModel<Value>: ObservableObject {
    typealias Action = (Value) async throws -> Void
    
    @Published var value: Value
    @Published var error: Error?
    @Published var isWorking = false
    
    subscript<T>(dynamicMember keyPath: WritableKeyPath<Value, T>) -> T {
        get { value[keyPath: keyPath] }
        set { value[keyPath: keyPath] = newValue }
    }
    
    private let action: Action
    
    init(initialValue: Value, action: @escaping Action) {
        self.value = initialValue
        self.action = action
    }
    
    nonisolated func submit() {
        Task {
            await handleSubmit()
        }
    }
    
    private func handleSubmit() async {
        isWorking = true
        do {
            try await action(value)
        } catch {
            print("[FormViewModel] Cannot submit: \(error)")
            self.error = error
        }
        isWorking = false
    }
}

AuthService:

private let auth = Auth.auth()

func createAccount(email: String, password: String) async throws {
        let result = try await auth.createUser(withEmail: email, password: password)
        try await result.user.updateProfile(\.displayName, to: email)
    }

private extension FirebaseAuth.User {
    func updateProfile<T>(_ keyPath: WritableKeyPath<UserProfileChangeRequest, T>, to newValue: T) async throws {
        var profileChangeRequest = createProfileChangeRequest()
        profileChangeRequest[keyPath: keyPath] = newValue
        try await profileChangeRequest.commitChanges()
    }
}

请提出任何问题,如果有什么需要补充,使其更清楚。
谢谢
进一步编辑:在完成lorem ipsum的建议后,我打开了“strict/complete concurrency”,这给了我以下代码的“error”:

func createAccount(email: String, password: String) async throws {
        let result = try await auth.createUser(withEmail: email, password: password)
        try await result.user.updateProfile(\.displayName, to: email)
    }

让结果部分:从主参与者隔离上下文调用非隔离示例方法“createUser(withEmail:password:)”返回的不可发送类型“AuthDataResult”不能跨越参与者边界try await部分:非可发送类型'WritableKeyPath<UserProfileChangeRequest,String?>'在调用非隔离示例方法'updateProfile(_:to:)'时退出主参与者隔离上下文不能跨越参与者边界
有没有人对解决这个问题有什么建议?
谢谢

jxct1oxe

jxct1oxe1#

MainActor Package 器的目的是保持主参与者的UI更新。您可以通过不隔离和“泄漏”Task来移除安全性。
试试类似这样的

@State private var isProcessing: Bool = false

然后呢

Button {
                //This can start/stop a task
                isProcessing.toggle()
            } label: {
                Text("Create Account")
                    .task(id: isProcessing) {
                        guard isProcessing else {
                            return
                        }
                        await viewModel.handleSubmit()
                        isProcessing = false
                    }

            }

这将允许您以并行的精神“坚持”任务。
我还建议打开“严格/完全并发”,或者至少“有针对性”。
为什么Task<C,Error>可以工作,即使C是一个不是Sendable的类,或者是吗?
并加上

-Xfrontend -warn-concurrency 
-Xfrontend -enable-actor-data-race-checks

Build Settings > Swift Compiler > Other Swift Flags

相关问题