从Firebase检索子值

时间:2016-04-03 19:08:16

标签: ios swift firebase

我有一个查询查找特定的UUID并返回该对象。

print(snapshot)  
Snap (users) {
"-KENROf9kGUPw1j6ghFo" =     {
    UUID = "A1166D66-31B4-4B1C-B28C-58E9E5615D16";
    nickname = Craig;
};
}

我正在尝试提取nickname,但遇到了麻烦。

这是我的查询let queryRef = Firebase(url:"https://<url>.firebaseio.com/users")

这是查询参考。

var handle = queryRef.queryOrderedByChild("UUID").queryEqualToValue("A1166D66-31B4-4B1C-B28C-58E9E5615D16").observeSingleEventOfType(.Value, withBlock: { snapshot in
        if snapshot.value is NSNull {
            print("snapshot empty")
        } else {
            print(snapshot) //this prints the snapshot above
            self.nickname = snapshot.value.objectForKey("nickname") as! String  //this returns an error
            print(self.nickname)
        }
    })
}

self.nickname....行返回以下错误。

fatal error: unexpectedly found nil while unwrapping an Optional value我尝试了几种不同的结构方法,但还没有找到一种不会失败的方法。

2 个答案:

答案 0 :(得分:1)

您将用户存储在推送ID下,然后通过他们的uid查询它们。这是一个坏主意。相反,将用户存储在他们的uid(或您的情况下为UUID)中,如Firebase documentation on storing user data所示的示例所示:

ref.authUser("jenny@example.com", password:"correcthorsebatterystaple") {
    error, authData in
    if error != nil {
        // Something went wrong. :(
    } else {
        // Authentication just completed successfully :)
        // The logged in user's unique identifier
        println(authData.uid)
        // Create a new user dictionary accessing the user's info
        // provided by the authData parameter
        let newUser = [
            "provider": authData.provider,
            "displayName": authData.providerData["displayName"] as? NSString as? String
        ]
        // Create a child path with a key set to the uid underneath the "users" node
        // This creates a URL path like the following:
        //  - https://<YOUR-FIREBASE-APP>.firebaseio.com/users/<uid>
        ref.childByAppendingPath("users")
           .childByAppendingPath(authData.uid).setValue(newUser)
    }
}

如果您坚持坚持当前的数据结构,则需要处理.Value查询返回子项列表这一事实。即使只有一个项目与查询条件匹配,它仍然是一个项目的列表。

var query = queryRef.queryOrderedByChild("UUID").queryEqualToValue("A1166D66-31B4-4B1C-B28C-58E9E5615D16")
query.observeSingleEventOfType(.Value, withBlock: { snapshot in
    if snapshot.value is NSNull {
       print("snapshot empty")
    } else {
        for child in snapshot.children {
            self.nickname = child.value.objectForKey("nickname") as! String
            print(self.nickname)
        }
    }
})

答案 1 :(得分:0)

替换此行:

<system.web>
   .....
</system.web>

<runtime>
...
</runtime>

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
         <binding name="EconomicWebServiceSoap">
             <security mode="Transport" />
         </binding>
         <binding name="EconomicWebServiceSoap1" />
      </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://api.e-conomic.com/secure/api1/economicwebservice.asmx"
            binding="basicHttpBinding"  bindingConfiguration="EconomicWebServiceSoap"
            contract="PTS.S2s.Economic.WebService.EconomicWebServiceSoap"
            name="EconomicWebServiceSoap" />
    </client>
</system.serviceModel>
</configuration>

以下内容:

self.nickname = snapshot.value.objectForKey("nickname") as! String

确保您的快照是具有属性&#34;昵称&#34;

的对象