David Seek David Seek - 1 month ago 16
Swift Question

How to update Firebase with struct

I have a struct

EventDate
and am trying to update a reference in Firebase.

struct EventDate {

var club: String = ""
var date: String = ""
var eventText: String = ""
var free: String = ""
var monthYear: String = ""
}


My update function throws
lldb
. I guess because the
keys
are no Strings(?)

func updateEvent(_ backendlessUserObjectID: String, event: EventDate) {

let reference = firebase.child("eventDates").child(backendlessUserObjectID).child(event.date)
reference.setValue(event) { (error, ref) -> Void in
if error != nil {
print("\(error)")
}
} // lldb here
}


If I change the function to the following, everything is fine (because
Keys
and
Values
are now Strings?)

func updateEvent(_ backendlessUserObjectID: String, event: EventDate) {

let item: NSMutableDictionary = ["club" : event.club,
"date" : event.date,
"eventText" : event.eventText,
"free" : event.free,
"monthYear" : event.monthYear]

let reference = firebase.child("eventDates").child(backendlessUserObjectID).child(event.date)
reference.setValue(item) { (error, ref) -> Void in
if error != nil {
print("\(error)")
}
}
}


Am I right that I receive
lldb
because the
keys
from my models are not Strings? Or what am I missing and how will I be able to save the values into my Firebase using my model without creating the
NSMutableDictionary
? Help is very appreciated.

PS:
print(event.date)
=
201610120200000000
-> the desired value for
.child

Answer

No it has nothing to do with the type of keys that you are trying to save in your Firebase Database its just that struct is a dataModel or to be precise a physically grouped list of variables which you initialise with some custom Data, and you can only save four types of values types in your Firebase Database:-

  • NSDictionary
  • NSArray
  • NSNumber
  • NSString

Look up the docs :- Read And Write data, Firebase- iOS

So when you cast your values in a NSMutableDictionary, you come clean of struct. And struct and class is not recognisable by the Firebase Database.