iOS Swift Firebase数据库架构

时间:2018-03-16 07:39:21

标签: ios swift firebase firebase-realtime-database

我有一个需要存储这些对象的应用

User 
{
   auto-generated-id: String,
   name: String,
   schoolID: String,
   myClasses: String[]
}

School
{
   auto-generated-id: String,
   name: String,
   adress: String
}

Classes
{
  auto-generated-id: String,
  number: String,
  subject: String,
  schoolID: String
}

但是我不熟悉NoSQL结构,并且不确定如何存储这些对象以进行有效查询

我的应用程序将提供课程科目,班级编号,学校ID

我需要的查询

1)

select * 
from School s, Classes c
where c.subject = 'ABC' and c.number = '100', and c.schoolID = "1234" 

2)

select c.*
from User u, Classes c, School s
where u.auto-generated-id = "1234" and u.schoolID = s.schoolID and c.schoolID = s.schoolID 

我可以重新安排我的架构以适应nosql方法并仍然执行查询的最佳方法是什么

由于我的结构不佳,我也一直收到以下警告

使用未指定的索引。您的数据将在客户端上下载和过滤。考虑在somePath中添加“.indexOn”:“classNum”以获得更好的性能

1 个答案:

答案 0 :(得分:0)

试试这个数据库结构:

Users
 userid
   name:userx
   email: userx@gmail.com
 userid
   name:usery
   email:usery@gmail.com

Schools
   pushid
     name:school1
     address: at stackoverflow
   pushid
     name:school2
     address:ios section

Classes
   pushid
     number:1
     subject: Firebase
     schoolname: school1
   pushid
     number:2
     subject:ios development
     schoolname:school2

UserClass
  userid<---------if of userx
    randomid
      classname: Firebase
      schoolname: school1
  userid<--------------if of usery
    randomid
      classname:ios developement
      schoolname: school2

检查一下:

https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html

在Firebase中,没有joinswhere clause。 您只能指定一个路径,因此无法使用两个表,只能使用一个顶级节点并通过其他节点,例如:child("Users").child(userid)