Pregunta

I'm building my first iOS app and I'm using Firebase as my backend.

I'm trying to store an array into my Firebase backend like so:

Firebase* f = [[Firebase alloc] initWithUrl:@"https://test.firebaseio.com/items"];
[f setValue:@{@"UNIQUE OBJECT ID HERE": @{@"meh": @"lol", @"hum": @"kek"}}];

It works as expected, but I need to have an unique id for each "object". Right now, every time that method runs Firebase just replaces the old data with the new one.

Coming from a web dev background, I know that in JavaScript I would do this:

var ref = new Firebase("https://test.firebaseio.com/items");
ref.push({meh: "lol", hum: "kek")};

Which yields the expected result, since it gives the object above a name of "-IuxeSuSiNy6xiahCXa0".

How do I accomplish this in ObjC?

¿Fue útil?

Solución

Firebase has build in system in Objective-C as well, but the syntax is a touch different.

Firebase * ref;

Firebase * autoId = [ref childByAutoId];

This:

Firebase* f = [[Firebase alloc] initWithUrl:@"https://test.firebaseio.com/items"];
[f setValue:@{@"UNIQUE OBJECT ID HERE": @{@"meh": @"lol", @"hum": @"kek"}}];

Should be:

Firebase* f = [[Firebase alloc] initWithUrl:@"https://test.firebaseio.com/items"];
[[f childByAutoId] setValue:@{@"meh": @"lol", @"hum": @"kek"}}];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top