David Seek David Seek - 1 year ago 110
Swift Question

How to update Firebase with struct

I have a struct

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
. I guess because the
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 {
} // lldb here

If I change the function to the following, everything is fine (because
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 {

Am I right that I receive
because the
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
? Help is very appreciated.

-> the desired value for

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download