首页 > 解决方案 > 线程 1:FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 Posts 有 1 个?

问题描述

为什么我在尝试创建 firebase 计数器时不断收到此错误?我从字面上遵循 google firebase docs line for line。这是创建计数器功能

  func createCounter(ref: DocumentReference, numShards: Int) {
               ref.setData(["numShards": numShards]){ (err) in
                   for i in 0...numShards {
                       ref.collection("shards").document(String(i)).setData(["count": 0])
                   }
               }
           }

这就是我尝试使用它的方式

 Button("In there"){createCounter(ref: ref.document("Posts"), numShards: 0); incrementCounter(ref: ref.document("Posts"), numShards: 0); getCount(ref: ref.document("Posts"))
                        
                    }

当我有这个时,我也不断收到这个“从初始化程序返回而不初始化所有存储的属性”错误。

struct PostRow: View {
    
    var post: PostModel
    @ObservedObject var postData : PostViewModel
    let db = Firestore.firestore()
    let uid = Auth.auth().currentUser!.uid
    let numShards: Int
    let count: Int
    
    init(numShards: Int, count: Int) {
        self.numShards = numShards
        self.count = count
        
    }

标签: swiftxcodefirebasegoogle-cloud-firestoreswiftui

解决方案


init(){}在SwiftUI 视图中使用此代码。

init() {
      
        UINavigationBar.appearance().barTintColor = UIColor.clear        
        UINavigationBar.appearance().tintColor = .clear
        UINavigationBar.appearance().isOpaque = true

       }

推荐阅读