在Firebase-ios-swift中创建和追加数组 [英] Creating and appending arrays in Firebase-ios-swift

查看:185
本文介绍了在Firebase-ios-swift中创建和追加数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在firebase 3.0中创建一个数组并执行数组中的append和delete函数

这是我正在寻找的数据结构: / p>

  1-UserIds 
--0。)12345
--1。)678910
- 2.)1112131415
2-UserProfile
--0。)12345
-0。)userName:Rayega
-1。)friends:[Julian ,Mary,Powers]
- 1。)678910
-0。)userName:Morse
-1。)friends:[Polme,Mary朋友:[Ester,Hulio,Julian,Finn]
- 2。)1112131415
-0。)userName:Tanya
-1。 ]

我想创建一个像UserIds这样的数组,并且像我的用户配置文件能够从这些数组中追加和删除数据。我知道如何创建一个字典,但我将如何绕过在该字典中存储数组,并能够执行追加和删除函数。

我存储UserIds数组,因为我需要显示每个用户在一个tableView的信息。

解决方案

由伊万提供的链接解释,在多个用户同时添加/更改数据的数据库中,数组很少是一个好主意。似乎也没有必要将朋友列表存储为数组。将其存储为一组用户ID 实际上更有效率。这样一来,您就可以自动保证每个用户ID只能在一次设置中。

 用户:{
12345:Rayega,
67890:Morse,
24680:Tanya,
13579:Julian,
86420:Mary,
97531:权力
},
UserFriends:{
12345:{
13579:true,
86420:true,
97531:true
},
67890:{
86420:true,
24680:true,
97531:true
}
}

最后一个结构称为索引,在Firebase文档中有创建可缩放的数据。


How to create an array in the firebase 3.0 and perform the functions of append and delete in the array

This is the data structure i am looking for:-

1-UserIds
  --0.)   12345
  --1.)   678910
  --2.)   1112131415
2-UserProfile
  --0.)  12345
         -0.)  userName : "Rayega"
         -1.)  friends : ["Julian","Mary","Powers"]
  --1.)  678910
         -0.)  userName : "Morse"
         -1.)  friends : ["Polme","Mary","Finn"]
  --2.)  1112131415
         -0.)  userName : "Tanya"
         -1.)  friends : ["Ester","Hulio","Julian"]

i want to create an array like UserIds and like friends in every child of my user profile and be able to append and delete data from those arrays.I know how to create a dictionary but how would i go around storing an array in that dictionary and be able to perform append and delete functions in it.

I am storing UserIds array because i need to display info of every user in a tableView.

解决方案

As the links provided by Ivan explain, arrays are seldom a good idea in a database where multiple users are concurrently adding/changing the data. There also seems to be no need to store the friend list as an array. It's actually more efficient to store it as a set of user ids. That way, you're automatically guaranteed that each user id can be in the set only once.

Users: {
   12345: "Rayega",
   67890: "Morse",
   24680: "Tanya",
   13579: "Julian",
   86420: "Mary",
   97531: "Powers"
},
UserFriends: {
   12345: {
      13579: true,
      86420: true,
      97531: true
   },
   67890: {
      86420: true,
      24680: true,
      97531: true
   }
}

That last structure is called an index and is covered in the Firebase documentation under creating data that scales.

这篇关于在Firebase-ios-swift中创建和追加数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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