TheRealRonDez
TheRealRonDez

Reputation: 2807

Saving Objects to Firebase

First time working on a Firebase app. I understand that it doest not allow you to save actual Objective C object.I understand that everything is saved in a JSON tree which can be NSStrings, NSNumbers, NSArrays, NSDictionaries. You actually have to drill down to each property to get the actual value. Ex.

Say you have myObject => Firebase doesn't like it. You have to set the value by getting the property. Ex. myObject.name (which would be a string). This is very annoying if your objects have a a lot properties.

I understand that you can implement NSCoding to make it a bit more elegant. I have never done this before. I have the following Object for a user.

#import <Foundation/Foundation.h>

@interface User : NSObject <NSCoding>


@property (nonatomic, copy) NSString *username;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *currentCity;

@end

#import "User.h"

@implementation User
    @dynamic username;
    @dynamic name;
    @dynamic currentCity;

-(void)encodeWithCoder:(NSCoder *)encoder{

    [encoder encodeObject:self.username forKey:@"username"];
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeObject:self.currentCity forKey:@"currentCity"];


}

-(id)initWithCoder:(NSCoder *)decoder{

    if (self = [super init]) {
        self.username = [decoder decodeObjectForKey:@"username"];
        self.name = [decoder decodeObjectForKey:@"name"];
        self.currentCity = [decoder decodeObjectForKey:@"currentCity"];

    }

    return self;
}

so when saving to the User Object which is stored in Firebase, do we just encode the object when we are saving to firebase?

I'm a bit confused about how to save it or even retrieve it. Parse is a lot more straight forward, but for this app I need to user firebase.

Any help or examples of how you guys handled this would be greatly appreciated.

Thanks you!

Upvotes: 1

Views: 1025

Answers (1)

Jay
Jay

Reputation: 35648

This doesn't directly answer the question but provides a simple option.

Think of firebase nodes as having a name and then a set of key/value properties. Similar to an Object in ObjC.

To make it super easy, just give your ObjC objects some intelligence to handle the dictionary read in from Firebase, and likewise update firebase child nodes when the ObjC object changes.

For example; Read in a single firebase node (with children 'properties') as an FDataSnapshot. (assume we are reading in just one node instead of number of them). The snapshot has a .value property which is a dictionary of key/value pairs. So here's how it would look in Firebase:

user_id_0
   userName: "dude"
   name: "Joe Smith"
   currentCity: "Miami"

Your User object would be an object with three identical properties; userName, name and currentCity. (important to match the names in this example)

When you initialize the User object, simply pass it the dictionary from the FDataSnapshot (ss) and it will populate itself from those keys. Something like this

    NSDictionary *dict = ss.value;

    User *aUser = [User new];
    [aUser initWithDict:dict];

and in the initWithDict method in the User object

for ( id key in dict ) {
  id value = dict[key];
  [self setValue:value forKey:key];
}

The above walks through each of the key/value pairs in the dictionary from Firebase, and then populates the matching properties in the user object. This also allows the User object to be very flexible; as when you add a new child value (property) to the node in firebase (like favoriteFood), it can simply be added to the User object as a property with the same name.

If you don't want to tie the names together, just some if..then's on the Firebase names can be used to populate the User object.

Likewise, when updating a property of the User object, utilize a method within the User object that updates the child node in firebase. Hint: you should also store the Firebase node name in the User object so it will know which node to update.

There are 100 other ways of having this functionality but this option really simplifies moving data in and out of Firebase.

Upvotes: 3

Related Questions