使用Swift中的变量进行Firebase查询

时间:2021-10-02 20:01:36

I'm trying to find specific user images from my Firebase data set I'm naming the group users with unique display names as children and those children with keys of name, lowerCaseName, UID, and Image.
使用Swift中的变量进行Firebase查询

我正在尝试从我的Firebase数据集中查找特定的用户图像我将具有唯一显示名称的组用户命名为子项以及具有名称,lowerCaseName,UID和Image的键的子项。

Is there anyway I can query through users as a variable in order to get to images?? I'm thinking http://firebase.com/users/%diplaynames/image but not sure.

反正我是否可以通过用户查询变量以获取图像?我在想http://firebase.com/users/%diplaynames/image但不确定。

3 个解决方案

#1


1  

What is your Firebase base url? You should be querying the path that looks something like:

你的Firebase基本网址是什么?你应该查询看起来像这样的路径:

https://<APP ID HERE>.firebaseio.com/

So you're initialization of Firebase should look like:

因此,您对Firebase的初始化应如下所示:

let fire = Firebase(url: "https://<APP ID HERE>.firebaseio.com/users/<DISPLAYNAME>/image")

Alternatively, you could do:

或者,您可以这样做:

let fire = Firebase(url: "https://<APP ID HERE>.firebaseio.com")
let imageRef = fire.childByAppendingPath("users").childByAppendingPath("<DISPLAYNAME>).childByAppendingPath("image")

#2


0  

In Firebase, you cannot retrieve a subset of the properties of a node. Either a node is retrieved, or it isn't.

在Firebase中,您无法检索节点属性的子集。检索节点,或者不检索节点。

If you have a use-case where you need to retrieve just the images, you should store a top-level node that has just those images.

如果您有一个只需要检索图像的用例,则应该存储只包含这些图像的*节点。

userImages
  Charles: base64data
  Zach: base64data

#3


0  

A couple of thoughts:

几点想法:

While there is nothing wrong with your structure, you may want to consider changing the node names from the users name to a firebase auto generated ID.

虽然您的结构没有任何问题,但您可能需要考虑将节点名称从用户名更改为firebase自动生成的ID。

users
   user_0
    name: Charles
    image: some image
   user_1
    name: Zaddy
    image: another image

Sometimes data will change, so for example say Charles wants to be called Charlie. To make that change you would have to delete the Charles node*, re-add it, and then update every node that refers back to Charles. If you name it with an auto-generated id (or the uid; best practice) user_0, then just update the name: "Charlie" and everything else falls into place.

有时数据会发生变化,例如,查尔斯希望将其称为查理。要进行更改,您必须删除Charles节点*,重新添加它,然后更新引用Charles的每个节点。如果使用自动生成的id(或uid;最佳实践)user_0命名,则只需更新名称:“Charlie”,其他所有内容都会到位。

*The child name: and the node name can certainly be the same or different, so this is a situational example.

*子名称:节点名称当然可以相同或不同,因此这是一个情境示例。

To get Charles' image (assuming it's base64 encoded) and you want to add it to an imageView

获取Charles的图像(假设它是base64编码的),并且您想将其添加到imageView

    let ref = Firebase(url:"https://your_app.firebaseio.com/users")
    ref.queryOrderedByChild("name").queryEqualToValue("Charles")
       .observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in

        //snapshot will contain the child key/value pairs
        let base64EncodedString = snapshot.value.objectForKey("image")
        let imageData = NSData(base64EncodedString: base64EncodedString as! String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
        let decodedImage = NSImage(data:imageData!)
        self.myImageView.image = decodedImage

    })

#1


1  

What is your Firebase base url? You should be querying the path that looks something like:

你的Firebase基本网址是什么?你应该查询看起来像这样的路径:

https://<APP ID HERE>.firebaseio.com/

So you're initialization of Firebase should look like:

因此,您对Firebase的初始化应如下所示:

let fire = Firebase(url: "https://<APP ID HERE>.firebaseio.com/users/<DISPLAYNAME>/image")

Alternatively, you could do:

或者,您可以这样做:

let fire = Firebase(url: "https://<APP ID HERE>.firebaseio.com")
let imageRef = fire.childByAppendingPath("users").childByAppendingPath("<DISPLAYNAME>).childByAppendingPath("image")

#2


0  

In Firebase, you cannot retrieve a subset of the properties of a node. Either a node is retrieved, or it isn't.

在Firebase中,您无法检索节点属性的子集。检索节点,或者不检索节点。

If you have a use-case where you need to retrieve just the images, you should store a top-level node that has just those images.

如果您有一个只需要检索图像的用例,则应该存储只包含这些图像的*节点。

userImages
  Charles: base64data
  Zach: base64data

#3


0  

A couple of thoughts:

几点想法:

While there is nothing wrong with your structure, you may want to consider changing the node names from the users name to a firebase auto generated ID.

虽然您的结构没有任何问题,但您可能需要考虑将节点名称从用户名更改为firebase自动生成的ID。

users
   user_0
    name: Charles
    image: some image
   user_1
    name: Zaddy
    image: another image

Sometimes data will change, so for example say Charles wants to be called Charlie. To make that change you would have to delete the Charles node*, re-add it, and then update every node that refers back to Charles. If you name it with an auto-generated id (or the uid; best practice) user_0, then just update the name: "Charlie" and everything else falls into place.

有时数据会发生变化,例如,查尔斯希望将其称为查理。要进行更改,您必须删除Charles节点*,重新添加它,然后更新引用Charles的每个节点。如果使用自动生成的id(或uid;最佳实践)user_0命名,则只需更新名称:“Charlie”,其他所有内容都会到位。

*The child name: and the node name can certainly be the same or different, so this is a situational example.

*子名称:节点名称当然可以相同或不同,因此这是一个情境示例。

To get Charles' image (assuming it's base64 encoded) and you want to add it to an imageView

获取Charles的图像(假设它是base64编码的),并且您想将其添加到imageView

    let ref = Firebase(url:"https://your_app.firebaseio.com/users")
    ref.queryOrderedByChild("name").queryEqualToValue("Charles")
       .observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in

        //snapshot will contain the child key/value pairs
        let base64EncodedString = snapshot.value.objectForKey("image")
        let imageData = NSData(base64EncodedString: base64EncodedString as! String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
        let decodedImage = NSImage(data:imageData!)
        self.myImageView.image = decodedImage

    })