使用唯一ID推入Firebase [英] Push into Firebase with unique ID

查看:106
本文介绍了使用唯一ID推入Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个iOS应用程序,并将Firebase作为后端。



我试图将数组存储到我的Firebase后端, :

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

它按预期工作,但是我需要为每个对象设置一个唯一的ID。现在,每次运行该方法时,Firebase都会将新数据替换为旧数据。



从Web开发背景来看,我知道在JavaScript中我会这样做:

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

得到预期的结果,因为它提供了一个名称为-IuxeSuSiNy6xiahCXa0的对象。 / p>

如何在ObjC中完成此项工作?

解决方案

在Objective-C中的系统中,但是语法却是一个不同的触摸。

  Firebase * ref; 

Firebase * autoId = [ref childByAutoId];



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

应该是:

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


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?

解决方案

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"}}];

这篇关于使用唯一ID推入Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆