I am currently moving from Android dev to Swift dev with Firebase.
In Android, it was very easy to parse a snapshot retrieved by Firebase to an object (e.g. dataSnapshot.getvalue(EventModel.class))
How can we achieve the same in Swift? It seems that there is no such functionality. I am using Firebase-ui to retrieve objects for a list, there is a parameter "modelClass", but there is no information on how to define that class.
How do we have to define the following objects:
EventModel:
MessageModel:
Is there a way to easily define this and parse the snapshot in Swift? Or do we have to use custom functions/classes like ObjectWrapper?
Kind regards,
Henry
Author of FirebaseUI here, good question!
The short answer is that the Objective-C client for Firebase has no similar functionality for the Android/Java snapshot.getValue(Object.class). This is due to several reasons but essentially Java has several things that Objective-C doesn't have (type annotations and generics being the two relevant ones in this case) that prevent us from doing this easily in the client. Typically, people end up building/using a JSON <--> Object mapper (which I assume ObjectWrapper is) given that Objective-C doesn't provide this functionality out of the box.
The longer answer is that doing this is sort of possible by using the Objective-C runtime and using reflection to transform the dictionary received into a model object. I hacked together a modelClass implementation that's semi-undocumented in FirebaseUI, as it only works in a limited set of circumstances. See below for my quick pass at documentation :)
The current feature simply creates an instance of the class then populates the fields directly from the dictionary provided from snapshot.value (see implementation here). This means that the properties must exactly match the keys in Firebase. This also means that it needs to receive a Dictionary back from Firebase--it won't automatically convert Strings, Numbers, Booleans, or Arrays. It also means that nested objects are unlikely to work, given that Objective-C doesn't have generics (at least not mainstream Obj-C, yet). Specifically, we currently only support single level Javascript objects with values that are primitive types.
If you have a JSON object in Firebase like:
{
message: {
name: String,
text: String
}
}
Your Objective-C (or Swift) class would look like:
// Obj-C
@interface MyMessageClass : NSObject
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *text;
@end
// Swift
class MyMessageClass {
let name: String!
let text: String!
}
You can then use the modelClass:(Class)class property passing in either [MyModelClass class] or MyModelClass.self in the constructors. Take a look at ViewController.m and Message.h for a simple implementation of how to use this feature.
I'm still testing how well I can make a more robust version that could be merged into the client library (it might look like -valueAsObject:(Class)class so you can do something like MyClass *customClass = [snapshot valueAsObject:[MyClass class]];) No word on if that's even really possible, or if it ever sees the light of day.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With