如何让Firebase在数据库的三个独立区域中显示相同的ID?

时间:2018-12-12 07:23:15

标签: android ios swift firebase

我有一个应用程序,该应用程序使用了Android和iOS都使用的firebase功能。

使用下面的 Android recordToFirebase()方法:

  • 它添加了一个“历史”节点
  • 它为用户/驾驶员和用户/乘车者添加了一个“历史”节点
  • 它在用户/驾驶员和用户/乘车者中都将id设置为true
    DatabaseReference riderRef = FirebaseDatabase.getInstance().getReference("Users/Riders").child(riderId).child("history");

    DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference("Users/Drivers").child(driverId).child("history");

    final DatabaseReference historyRef = FirebaseDatabase.getInstance().getReference().child("history");

    requestId = historyRef.push().getKey(); // same key for all 3 refs
    driverRef.child(requestId).setValue(true);
    riderRef.child(requestId).setValue(true);

在Android中运行上述代码时,应在数据库中执行此操作 确实,它还会为所有3个(历史/标识,用户/驾驶员/历史/标识,用户/车手/历史/标识)分配符号ID:

- Users
  - Drivers
    - history
      - Ljdfuhkd8_kdnjd : true

  - Riders
    - history
      - Ljdfuhkd8_kdnjd : true

- history
  - Ljdfuhkd8_kdnjd
    - email: me@me.com

iOS 中,相同的方法(与Android相同,只是在Swift中):

let historyRef = ref.child("history")
let riderRef = ref.child("Users/Riders").child(riderID).child("history")
let driverRef  = ref.child("Users/Drivers").child(driverID).child("history")

driverRef.childByAutoId().setValue(true)        
riderRef.childByAutoId().setValue(true)

运行上述Swift方法时,它不会执行Android代码所执行的操作, 会为所有这三个分配不同的ID。

我如何才能重写此代码以使其像Android代码一样工作?

1 个答案:

答案 0 :(得分:2)

对于IOS:

const list1 = [
    {
        "id": [
            "5cca1dbc-dd5c-498f-8f83-735062c05240",
            "2a10c30a-7c3a-4081-8246-9d37e19c2d6f",
            "3128f36c-1c79-4301-b08f-e0182c256c03"
        ],
        "tag": "tag1",
        "count": {
            "low": 53,
            "high": 0
        }
    },
    {
        "id": [
            "510019af-1728-4628-9019-343cd3c1b3e1",
            "fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
            "adf0cd4c-3072-4ecf-9aa7-ecd5580c31ae"
        ],
        "tag": "tag2",
        "count": {
            "low": 43,
            "high": 0
        }
    }
]

const list2 = [
    {
        "id": "5cca1dbc-dd5c-498f-8f83-735062c05240",
        "createdDate": "2017-10-08T22:40:33.020Z",
        "modifiedDate": "2017-10-08T22:40:33.020Z",
        "title": "Good morning! #tag1",
        "text": " ",
        "media": [
            {
                "id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
                        "metadata": {
                            "mimetype": "image/jpeg",
                            "imageHeight": 400,
                            "imageWidth": 300
                        }
            }
        ],
        "topics": [
            {
                "topicId": "22a96a83-def3-4981-bc91-9277464b7105"
            },
            {
                "name": "Fashion",
                "topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
            }
        ],
        "language": null,
        "sourceId": "d25205ca-2ef308261113",
    },
    {
        "id": "fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
        "createdDate": "2017-10-08T22:40:33.020Z",
        "modifiedDate": "2017-10-08T22:40:33.020Z",
        "title": "Good morning! #tag1",
        "text": " ",
        "media": [
            {
                "id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
                        "metadata": {
                            "mimetype": "image/jpeg",
                            "imageHeight": 400,
                            "imageWidth": 300
                        }
            }
        ],
        "topics": [
            {
                "topicId": "22a96a83-def3-4981-bc91-9277464b7105"
            },
            {
                "name": "Fashion",
                "topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
            }
        ],
        "language": null,
        "sourceId": "d25205ca-2ef308261113",
    },
    {
        "id": "efde2bc9-018b-49c1-9c01-a4eda9817a33",
        "createdDate": "2017-10-08T22:40:33.020Z",
        "modifiedDate": "2017-10-08T22:40:33.020Z",
        "title": "Good morning! #tag1",
        "text": " ",
        "media": [
            {
                "id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
                        "metadata": {
                            "mimetype": "image/jpeg",
                            "imageHeight": 400,
                            "imageWidth": 300
                        }
            }
        ],
        "topics": [
            {
                "topicId": "22a96a83-def3-4981-bc91-9277464b7105"
            },
            {
                "name": "Fashion",
                "topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
            }
        ],
        "language": null,
        "sourceId": "d25205ca-2ef308261113",
    }
]

var arr = []
list1.forEach(l1I => {
 l1I.id.forEach(eID => list2.forEach(l2I => {
   if(l2I.id === eID){
    var obj = {}
      obj["details"] = l2I;
      obj["tag"] = l1I.tag;
      obj["count"] = l1I.count;
      arr.push(obj);
   } 
}))
})


console.log("output", arr)