ios:将数据发布到 Firestore 时出现问题

Posted

技术标签:

【中文标题】ios:将数据发布到 Firestore 时出现问题【英文标题】:ios: Having problem with posting data to Firestore 【发布时间】:2019-04-03 20:01:39 【问题描述】:

我正在尝试将数据发布到 Firebase。但是当我点击我的按钮时,什么也没有发生。既没有数据发送也没有任何异常发生。我尝试了很多方法,也尝试了新项目,但问题仍然存在。我已经尝试了 Firebase 网站上给出的测试代码,没有任何反应。

这是我的常量

let THOUGHTS_REF = "thoughts"

let CATEGORY = "category"

let NUM_COMMENTS = "numComments"

let NUM_LIKES = "numLikes"

let THOUGHT_TXT = "thoughtTxt"

let TIMESTAMP = "timestamp"

let USERNAME = "username"

**这里是 ThoughtCategory 的枚举 **

enum ThoughtCategory: String 

    case serious = "serious"
    case funny = "funny"
    case crazy = "crazy"
    case popular = "popular"


这是 VIEW 背后的代码:

import UIKit
import Firebase
import FirebaseFirestore

class AddThoughtVC: UIViewController, UITextViewDelegate 


    //Outlets
    @IBOutlet weak var segmentControl: UISegmentedControl!
    @IBOutlet weak var usernameTxt: UITextField!
    @IBOutlet weak var thoughtTxt: UITextView!
    @IBOutlet weak var postBtn: UIButton!

    //Variables
    private var categorySelected = ThoughtCategory.funny.rawValue


    override func viewDidLoad() 
        super.viewDidLoad()
        thoughtTxt.delegate = self

        postBtn.layer.cornerRadius = 4
        thoughtTxt.layer.cornerRadius = 4
        thoughtTxt.text = "My random thought..."
        thoughtTxt.textColor = UIColor.lightGray
    

    func textViewDidBeginEditing(_ textView: UITextView) 
        textView.text = ""
        textView.textColor = UIColor.darkGray
    

    @IBAction func categoryChanged(_ sender: Any) 

        switch segmentControl.selectedSegmentIndex 
        case 0:
            categorySelected = ThoughtCategory.funny.rawValue
        case 1:
            categorySelected = ThoughtCategory.serious.rawValue
        default:
            categorySelected = ThoughtCategory.crazy.rawValue
        

    

    @IBAction func postBtnTapped(_ sender: Any) 

        guard let username = usernameTxt.text else  return 

            Firestore.firestore().collection(THOUGHTS_REF).addDocument(data: [

            CATEGORY : categorySelected,
            NUM_COMMENTS : 0,
            NUM_LIKES : 0,
            THOUGHT_TXT : thoughtTxt.text,
            TIMESTAMP : FieldValue.serverTimestamp(),
            USERNAME : username


        ])  (err) in
            if let err = err 
                debugPrint("Error adding document: \(err)")
             else 
                self.navigationController?.popViewController(animated: true)
            
        

    


这是日志文件中的错误

2018-10-30 22:58:50.091529+0500 RNDM[802:11061] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C3.1:2][0x7fdb1051de90] 获取输出帧失败,状态 8196

2018-10-30 22:58:50.091719+0500 RNDM[802:11061] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C3.1:2][0x7fdb1051de90] 获取输出帧失败,状态 8196

2018-10-30 22:58:50.092369+0500 RNDM[802:11061] TIC 读取状态 [3:0x0]: 1:57 2018-10-30 22:58:50.092511+0500 RNDM[802:11061] TIC 读取 状态 [3:0x0]: 1:57

【问题讨论】:

检查 Firestore 控制台上的安全规则。我认为您的应用程序无权访问 Firestore。检查控制台的规则选项卡。 谢谢,问题解决了 @Usama 你是如何解决这个问题的?我有同样的问题。安全规则设置为 public3 有关如何解决此问题的更多信息? 我刚刚将安全规则更改为公共@BradThomas Read: true Write: true 【参考方案1】:
Check Firestore rules
rules_version = '1';

service cloud.firestore 
  match /databases/database/documents 
    match /document=** 
      allow read, write;
    
  

【讨论】:

以上是关于ios:将数据发布到 Firestore 时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

构建iOS时出现颤振云Firestore错误

尝试搜索 Firestore 并重新加载 tableview 时出现 SearchBar 问题

在云功能上使用 Firestore SDK 时出现错误:无法编码类型

将数据从 Firestore 映射到 Swift 中的结构 - IOS

Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中?

添加“cloud_firestore”时出现颤振错误