使用Firestore提取数据时出现问题

时间:2020-10-22 00:32:03

标签: swift firebase google-cloud-firestore swiftui

我在Firebase上的YouTube上看到了一个教程,该教程显示了如何在firestore和swiftui(https://www.youtube.com/watch?v=f6u3AnOKZd0)之间获取数据。这是我完全相同地复制的代码:

import SwiftUI
import Firebase
import FirebaseFirestore
import Foundation

class BooksViewModel: ObservableObject{
    @Published var books = [Book]()`
    
    private var db = Firestore.firestore()
    
    func fetchData(){
        db.collection("books").addSnapshotListener { (querySnapshot, error) in
            guard let documents = querySnapshot?.documents else{
                print("No results")
                return
            }
            self.books = documents.map { (queryDocumentSnapshot) -> Book in
                let data = queryDocumentSnapshot.data()
                
                let title = data["title"] as? String ?? ""
                let author = data["author"] as? String ?? ""
                let pages = data["pages"] as? Int ?? 0
                
                return Book(title: title, author: author, numberOfPages: pages)
          
            }
        }
    }

}

struct Book: Identifiable{
    var id:String = UUID().uuidString
    var title: String
    var author: String
    var numberOfPages: Int

}

struct Search: View {
   @ObservedObject private var viewModel = BooksViewModel()
  
    var body: some View {
        
        List(viewModel.books){ book in
            VStack{
                Text(book.title).font(.headline)
                Text(book.author).font(.subheadline)
                Text("\(book.numberOfPages) pages").font(.subheadline)                  
            }
        }
    }

}

问题是什么都没有显示。 在Firestore中,我具有“ books”集合,并且在每个自动ID的内部都有一个标题,一个作者和一个页面。 当我显示不带Firestore的列表时(如他在视频开头所显示的),它可以正常工作。但是,当我将其连接到Firestore时,它什么也没显示。但问题在于,它可以像Firebase中“ Cloud Firestore”中的“使用”中那样进行连接,其中包括86个读数,15个记录和8个排除项的峰值。有人知道如何解决吗?

11.0 Beta大苏尔 12.1 Xcode

控制台上显示的内容: 2020-10-22 08:21:35.875930-0300 aplicativo [38475:1811559] 6.34.0-[Firebase / Core] [I-COR000008]项目的Bundle ID与“ GoogleService-Info.plist”中的Bundle ID不一致',如果您使用自定义选项,则选择选项中的Bundle ID。为了确保可以正确配置所有内容,您可能需要使Bundle ID保持一致。要继续此plist文件,您可以将应用程序的捆绑包标识符更改为“ com.sigma2”。或者,您可以从https://console.firebase.google.com/下载与您的捆绑包标识符匹配的新配置文件,并替换当前的配置文件。 2020-10-22 08:21:36.176284-0300 aplicativo [38475:1811496] [] nw_protocol_get_quic_image_block_invoke dlopen libquic失败 2020-10-22 08:21:37.083782-0300 aplicativo [38475:1811559] 6.34.0-[Firebase / Analytics] [I-ACS023007] Analytics v.60900000已开始 2020-10-22 08:21:37.172409-0300 aplicativo [38475:1811559] 6.34.0-[Firebase / Analytics] [I-ACS023008]要启用调试日志记录,请设置以下应用程序参数:-FIRAnalyticsDebugEnabled(请参见““) 2020-10-22 08:21:39.418386-0300 aplicativo [38475:1811558] 6.34.0-[Firebase / Analytics] [I-ACS800023]没有待激活的快照。 SDK名称:app_measurement 2020-10-22 08:21:42.157064-0300 aplicativo [38475:1811560] 6.34.0-[Firebase / Analytics] [I-ACS023012]启用了分析收集 2020-10-22 08:21:42.157878-0300 aplicativo [38475:1811560] 6.34.0-[Firebase / Analytics] [I-ACS023220]启用了Analytics(分析)屏幕报告。调用+ [FIRAnalytics logEventWithName:FIREventScreenView参数:]记录屏幕视图事件。要禁用自动屏幕报告,请将Info.plist中的标志FirebaseAutomaticScreenReportingEnabled设置为NO(布尔值)

更新的控制台信息: 2020-10-22 11:18:52.062418-0300 aplicativo [49379:2031899] [] nw_protocol_get_quic_image_block_invoke dlopen libquic失败 2020-10-22 11:18:53.463035-0300 aplicativo [49379:2031919] 6.34.0-[Firebase / Analytics] [I-ACS023007] Analytics v.60900000已开始 2020-10-22 11:18:53.504736-0300 aplicativo [49379:2031919] 6.34.0-[Firebase / Analytics] [I-ACS023008]要启用调试日志记录,请设置以下应用程序参数:-FIRAnalyticsDebugEnabled(请参见“ goo.gl / RfcP7r“) 2020-10-22 11:18:55.710625-0300 aplicativo [49379:2031900] 6.34.0-[Firebase / Analytics] [I-ACS800023]没有待激活的快照。 SDK名称:app_measurement 2020-10-22 11:19:03.453691-0300 aplicativo [49379:2031891] 6.34.0-[Firebase / Analytics] [I-ACS023012]启用了分析收集 2020-10-22 11:19:03.556959-0300 aplicativo [49379:2031891] 6.34.0-启用[Firebase / Analytics] [I-ACS023220] Analytics(分析)屏幕报告。调用+ [FIRAnalytics logEventWithName:FIREventScreenView参数:]记录屏幕视图事件。要禁用自动屏幕报告,请将Info.plist中的标志FirebaseAutomaticScreenReportingEnabled设置为NO(布尔值)

3 个答案:

答案 0 :(得分:1)

尝试在主队列上进行{ "error": { "code": 403, "message": "Request had insufficient authentication scopes.", "errors": [ { "message": "Insufficient Permission", "domain": "global", "reason": "insufficientPermissions" } ], "status": "PERMISSION_DENIED" } } 分配,例如

books

答案 1 :(得分:1)

感谢添加日志,这很有帮助。

第一条消息日志消息表明捆绑软件ID有问题:

2020-10-22 08:21:35.875930-0300 aplicativo[38475:1811559] 6.34.0 - [Firebase/Core][I-COR000008] The project's Bundle ID is inconsistent with either the Bundle ID in 'GoogleService-Info.plist', or the Bundle ID in the options if you are using a customized options. To ensure that everything can be configured correctly, you may need to make the Bundle IDs consistent. To continue with this plist file, you may change your app's bundle identifier to 'com.sigma2'. Or you can download a new configuration file that matches your bundle identifier from https://console.firebase.google.com/ and replace the current one.

本质上,该应用程序的捆绑软件ID与GoogleService-Info.plist文件中的捆绑软件ID不匹配。确保捆绑包ID匹配。文档中的Firebase Getting Started with iOS页显示了如何在Firebase项目中注册iOS应用。

答案 2 :(得分:0)

代码很好,问题出在Firebase安全规则中。

这就是我用来使其正常工作的地方:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
        request.time < timestamp.date(2020, 11, 25);
    }
  }
}