Firestore API 连接失败导致我的应用程序崩溃

问题描述 投票:0回答:1

每次我从 Firebase 中的模拟数据切换到真实数据时,我的应用程序都会崩溃。 (https://i.sstatic.net/7UYBbfeK.png)

一旦我更改这 3 行代码,我的应用程序就会崩溃

主要代码

import SwiftUI

struct InboxView: View {
    
    @State private var showNewMessageView = false
//    @StateObject var viewModel = InboxViewModel() // uncomment this
    @State private var user = User.MOCK_USER // comment out this
    
//    private var user: User? { //uncomment this
//        return viewModel.currentUser
//    }
    
    var body: some View {
        NavigationStack {
            ScrollView {
                ActiveNowView()
                
                List {
                    ForEach(0 ... 10, id: \.self) { message in
                        InboxRowView()
                    }
                }
                .listStyle(PlainListStyle())
                .frame(height: UIScreen.main.bounds.height - 120)
            }
            .navigationDestination(for: User.self, destination: { user in
                ProfileView(user: user)
            })
            .fullScreenCover(isPresented: $showNewMessageView, content: {
                NewMessageView()
            })
            .toolbar {
                ToolbarItem(placement: .topBarLeading) {
                    HStack {
                        NavigationLink(value: user) {
                            CircularProfileImageView(user: user, size: .xsmall)
                        }
                        
                        Text("Chats")
                            .font(.title)
                            .fontWeight(.semibold)
                    }
                }
                
                ToolbarItem(placement: .topBarTrailing) {
                    Button {
                        showNewMessageView.toggle()
                    } label: {
                        Image(systemName: "square.and.pencil.circle.fill")
                            .resizable()
                            .frame(width: 32, height: 32)
                            .foregroundStyle(.black, Color(.systemGray5))
                    }
                }
            }
        }
    }
}

收件箱视图模型

import Foundation
import Combine
import Firebase

class InboxViewModel: ObservableObject {
    
    @Published var currentUser: User?
    
    private var cancellabels = Set<AnyCancellable>()
    
    init() {
        setupSuscribers()
    }
    
    private func setupSuscribers() {
        UserService.shared.$currentUser.sink { [weak self] user in
            self?.currentUser = user
        }.store(in: &cancellabels)
    }
}

用户

import FirebaseFirestore
import SwiftUI
import Firebase

struct User: Codable, Identifiable, Hashable {
    @DocumentID var uid: String?

    let fullname: String
    let email: String
    let profileImageUrl: String?
    
    var id: String {
        return uid ?? NSUUID().uuidString
    }
}

extension User {
    static let MOCK_USER = User(fullname: "Bruce Wayne", email: "[email protected]", profileImageUrl: "batman")
}

我一运行代码,终端就会抛出此错误

I0000 00:00:1732088994.725995 1362774 subchannel.cc:806] 子通道 0x101c70a80 {地址=ipv6:%5B2404:6800:4002:81a::200a%5D:443, args={grpc.client_channel_factory=0x, 60000002b3e0, grpc.default_authority= firestore.googleapis.com、grpc.http2_scheme=https、grpc.internal.channel_credentials=0x60000263cb40、grpc.internal.client_channel_call_destination=0x103d64828、grpc.internal.event_engine=0x60000002b4e0、grpc.internal.security_connector=0x600002c2 d900,grpc.internal.subchannel_pool= 0x600002949500、grpc.keepalive_time_ms=30000、grpc.primary_user_agent=grpc-c++/1.65.1、grpc.resource_quota=0x600000c99680、grpc.server_uri=dns:///firestore.googleapis.com}}:连接失败(未知:(域:NSPOSIXErrorDomain,代码:50,描述:操作无法完成。网络已关闭) {file:"/var/folders/hn/5bx1f4_d4ds5vhwhkxc7vdcr0000gn/T/ZipRelease/2024-07-25T04-01-28/project- ios/Pods/gRPC-Core/src/core/lib/transport/error_utils.cc", file_line:150,created_time:"2024-11-20T13:19:54.724606+05:30", grpc_status:2}), 支持关闭 874 毫秒

DEBUG:当前用户是可选的(Messenger.User(_uid:FirebaseFirestore.DocumentID)(值:可选(“Tg2TbvUlWBdjtL1XKkFJD6MeGKn1”)),全名:“Bruce Wayne”,电子邮件:“[电子邮件受保护]”,profileImageUrl:nil ))

ios swift firebase api google-cloud-firestore
1个回答
0
投票

从模拟数据切换到真实 Firebase 数据时,应用程序崩溃。终端日志包含 Network is down 错误,并显示 profileImageUrl 为 nil,导致渲染个人资料图像时崩溃。

更新用户代码:

var safeProfileImageUrl: String {
    return profileImageUrl ?? "defaultImageURL"
}

当从模拟数据切换到真实数据时,InboxViewModel 负责获取和观察真实的用户数据。如果 currentUser 为零,您的应用程序在尝试访问 profileImageUrl 等属性时可能会崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.