1

I'm looking for an effective way to map Firebase lists to custom Objective-C objects with Mantle. Firebase doesn't really have a concept of arrays, so every item in a list has an explicit id. A list in Firebase of type event appears something like this:

"events": {
    "id0": {
        "name": "Event 1",
        "date": "2017-03-28T08:00:00+00:00",
        "venue": "Venue 1"
    },
    "id1": {
        "name": "Event 2",
        "date": "2017-03-29T08:00:00+00:00",
        "venue": "Venue 2"
    },
    ...
    "id5": {
        "name": "Event 6",
        "date": "2017-04-05T08:00:00+00:00",
        "venue": "Venue 6"
    }
}

In Objective-C terms, this translates perfectly well into an NSDictionary, but it's easier to work with an NSArray in my UITableViewController. At the moment I'm using NSDictionary for each event, and simply moving the event's key inside the Dictionary and creating an array of dictionaries as follows (feel free to comment on whether this is a good or bad idea):

{
    "eventId": "id0",
    "name": "Event 1",
    "date": "2017-03-28T08:00:00+00:00",
    "venue": "Venue 1"
},
{
    "eventId": "id1",
    "name": "Event 2",
    "date": "2017-03-29T08:00:00+00:00",
    "venue": "Venue 2"
}
...

My idea is to then create an Event object:

@interface Event: NSObject <MLTModel>

@property (nonatomic, strong) NSString *eventId;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) NSString *venue;

@end

So my question is this: is there a way to map this Firebase list to a custom Event object with Mantle so that it can be easily used for UITableViews in iOS? Specifically, how does one map the key (id0, id1 etc) of the Firebase event to be a property of the custom Event object, and back again? Or would it be better to move the key "manually" and then hand over to Mantle?

AL.
  • 36,815
  • 10
  • 142
  • 281
Unieklee
  • 13
  • 6

0 Answers0