简体   繁体   English

Swift 的 Firebase Firestore 似乎没有到达 Google Cloud

[英]Firebase Firestore for Swift doesn't seem to be reaching Google Cloud

I'm trying to implement Firestore on my Swift app and I have hit a slight bump in the road.我正在尝试在我的 Swift 应用程序上实现 Firestore,但我在路上遇到了一点小问题。

I am using this function to add an Event object as a document to an Events collection.我正在使用此 function 将Event object 作为文档添加到Events集合中。 the function appears to be working as I am getting a Document ID returned to me, However looking through the Firebase console I can only see the document I added from the console. function 似乎正在工作,因为我收到了返回给我的文档 ID,但是通过 Firebase 控制台查看我只能看到我从控制台添加的文档。

func addEvent(event: Event){
    print("")
    print("Running addEvent()")
    print(event.toArray())
    var ref: DocumentReference? = nil
    ref = db.collection("Events").addDocument(data: event.toArray())
        { err in
                if let err = err {
                    print("Error adding document: \(err)")
                } else {
                    print("Document added with ID: \(ref!.documentID)")
                }
            }
    print(ref?.documentID)
}

To be sure it wasn't the Firestore rules, I set it to accept all incoming connections为了确保这不是 Firestore 规则,我将其设置为接受所有传入连接

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

If you need to know如果你需要知道

I have the following code to call the function (inside a viewDidLoad() that get's called right as the app is loading up):我有以下代码来调用 function (在应用程序加载时正确调用的viewDidLoad()内部):

let ch = CalendarHandler()
let event = Event("nil", title: "Test", date: "1", month: "4", year: "2021", start: "12:00", end: "13:00", count: "0", creator: "me", privacy: "1", allDay: "1")
ch.addEvent(event: event)

and the console outputs this when the function is called:当调用 function 时,控制台会输出:

Running addEvent()
["day": "1", "month": "4", "start": "12:00", "year": "2021", "isPrivate": true, "count": "0", "title": "Test", "isAllDay": true, "end": "13:00", "creator": "me"]
Optional("Ig8yw85mvm1oFsGhaYDc")

Can anyone see where I've gone wrong?谁能看到我哪里出错了? If it's not the code, does anyone know what might be causing this?如果不是代码,有谁知道可能是什么原因造成的?

thanks:)谢谢:)

EDIT编辑

as requested here is the Event class这里要求的是Event class

class Event{
let id: String
var title: String?
var date: String?
var month: String?
var year: String?
var start: String?
var end: String?
var creator: String?
var notes: String? = nil
var count: String = "0"
var invitees:[String] = []
var location: String? = nil
var canInvite: Bool = false
var isPrivate: Bool = false//false = visible, true = "busy"
var isUserInvited: Bool = false
var isAllDay: Bool = true

init(){
    id = "0"
}

init(_ id: String, title: String, date: String, month: String, year: String, start: String, end: String, count: String = "0", creator: String, privacy: String, allDay: String) {
    self.id = id
    self.title = title
    self.date = date
    self.start = start
    self.end = end
    self.creator = creator
    
    self.month = month
    self.year = year
    
    self.count = count
   //MARK: Re-implement these functions 
   // setPrivacy(Int(privacy)!)
   // setAllDay(Int(allDay)!)
    
   // isInvitee()
    
}

public func toArray() -> Dictionary<String,Any>{
    var ev = Dictionary<String,Any>()
    ev["title"] = title
    ev["day"] = date
    ev["month"] = month
    ev["year"] = year
    ev["start"] = start
    ev["end"] = end
    ev["creator"] = creator
    ev["notes"] = notes
    ev["count"] = count
    //ev["invitees"]
    ev["isPrivate"] = isPrivate
    ev["isAllDay"] = isAllDay
    return ev
}

The code in the question essentially works but is mis-labeled creating confusion.问题中的代码基本上可以工作,但被错误标记造成混乱。 Here's a re-do of the Event class that can return either an array or a dictionary.这是对可以返回数组或字典的事件 class 的重做。

struct Event {
    let title: String!
    let date: String!
     let month: String!

    func toArray() -> [String: String] {
        let array = [
            "title": title,
            "date": date,
            "month": month,
        ]

        return array as! [String : String]
    }

    func toDict() -> Dictionary<String, Any> {
        var ev = Dictionary<String, Any>()
        ev["title"] = self.title
        ev["data"] = self.date
        ev["month"] = self.month

        return ev
    }
}

then populate an Event object然后填充事件 object

let event = Event(title: "Test", date: "1", month: "4")

and then store it in Firebase as an array然后将其作为数组存储在 Firebase

ref = db.collection("Events").addDocument(data: event.toArray())

then as a dict然后作为一个字典

ref = db.collection("Events").addDocument(data: event.toDict())

then in firebase, check the results - you'll see they produce an identical result然后在 firebase 中,检查结果 - 你会看到它们产生相同的结果

What's in the array is a series of key: value pairs and what's in the dictionary is well, a dictionary containing a series of key: value pairs.数组中的内容是一系列 key: value 对,而字典中的内容很好,一个包含一系列 key: value 对的字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM