本文介绍了Firebase的独特价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firebase中拥有以下结构:

  -events 
-Firebase事件1的推送ID
** - 键:DF2342**
-name:事件1
-createdOn:12/12/12
-Firebase事件2的推送ID
* * -key:AB1232**
-name:Event 1
-createdOn:12/12/12

每个密钥都由运行应用程序的设备在本地自动生成(DF2342和AB1232),但是,我想先在设备上生成密钥,然后检查密钥是否已经存在在firebase数据库。如果存在,则重新生成密钥并重试。这里我使用的代码:

pre $ 让boardKey = self.keyGenHelper.generateUniqueKey()
var validData = false
while(!validData){
databaseRef?.child(events)。queryOrdered(byChild:key)。queryEqual(toValue:boardKey).observeSingleEvent(of:.childAdded,with :{(snapshot)in
if!snapshot.exists(){
let boardIdRef = self.databaseRef!.child(events)。childByAutoId()
boardIdRef.setValue(board。 generateDictionary(key:boardKey))
完成(boardKey,boardIdRef.key)
validData = true
}
},withCancel:{(error)in
print错误)
})
}

这里的问题是while循环一直在继续,永远不会从firebase得到回应。对于我来说,完成这个任务最好的办法是什么? 解决方案

由于你的应用程序要求你不能使用,一种确保你正在生成唯一的ID将是一个额外的表在firebase只是你已经生成的ID。例如:

 keys:{
DF2342:true,
AB1232: true,
...
}

然后当你生成你的钥匙可以这样做:

 让boardKey = self.keyGenHelper.generateUniqueKey()
var validData = false
while(!validData){
//在数据库中检查/键,看看我们是否已经生成了这个值的键
databaseRef?.child(keys)。queryEqual(toValue :boardKey).observeSingleEvent(of:.childAdded,with:{(snapshot)in
if!snapshot.exists(){
let boardIdRef = self.databaseRef!.child(events)。childByAutoId ()
boardIdRef.setValue(board.generateDictionary(key:boardKey))
完成(boardKey,boardIdRef.key)
validData = true
}
},withCancel :{(error)in
print(error)
})

还需要确保你正在写新生成的密钥 / keys


Having the following structure in Firebase:

-events
     -Firebase Push ID for Event 1
         **-key: "DF2342"**
         -name: "Event 1"
         -createdOn : 12/12/12
     -Firebase Push ID for Event 2
         **-key: "AB1232"**
         -name: "Event 1"
         -createdOn : 12/12/12

Every key is autogenerated locally (DF2342 and AB1232) by the device where the app is running on although, I want to generate the key first on the device, then check if the key already exists in firebase database. If it does exist regenerate the key and try again. Here the code I'm using for that:

 let boardKey = self.keyGenHelper.generateUniqueKey()
        var validData = false
        while(!validData){
        databaseRef?.child("events").queryOrdered(byChild: "key").queryEqual(toValue: boardKey).observeSingleEvent(of: .childAdded, with: { (snapshot) in
            if !snapshot.exists() {
                let boardIdRef = self.databaseRef!.child("events").childByAutoId()
                boardIdRef.setValue(board.generateDictionary(key: boardKey))
                completion(boardKey, boardIdRef.key)
                validData = true
            }
        }, withCancel: {(error) in
            print(error)
        })
        }

The problem here is that the while loop keeps going on and on and never got response from firebase. What would be the best approach for me to accomplish this task?

解决方案

Since your application requires that you can't use childByAutoId, a way to make sure you're generating unique IDs would be to store an additional table in firebase of just the IDs you've generated. For example:

"keys": {
  "DF2342": true,
  "AB1232": true,
  ...
}

Then when you generate your key you can do something like this:

let boardKey = self.keyGenHelper.generateUniqueKey()
var validData = false
while(!validData){
// check /keys in the database to see if we've already generated a key of this value
databaseRef?.child("keys").queryEqual(toValue: boardKey).observeSingleEvent(of: .childAdded, with: { (snapshot) in
    if !snapshot.exists() {
        let boardIdRef = self.databaseRef!.child("events").childByAutoId()
        boardIdRef.setValue(board.generateDictionary(key: boardKey))
        completion(boardKey, boardIdRef.key)
        validData = true
    }
}, withCancel: {(error) in
    print(error)
})

You also need to make sure you're writing the newly generated key to /keys.

这篇关于Firebase的独特价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:28