Reputation: 238
I'm trying to implement Firestore on my Swift app and I have hit a slight bump in the road.
I am using this function to add an Event
object as a document to an Events
collection. 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.
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
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):
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:
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
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
}
Upvotes: 1
Views: 134
Reputation: 35667
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.
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
let event = Event(title: "Test", date: "1", month: "4")
and then store it in Firebase as an array
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
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.
Upvotes: 2