2016-11-23 127 views
0

我試圖使用Firebase 3和Swift 3創建一個允許用戶創建「測試」的應用程序,目的是要顯示完全隨機測試數據庫。從Firebase數據庫中獲取隨機孩子

我的表:

Users: 
- uid 
    - email 

tests 
    - id 
     - title 
     - user_uid 

如何隨機選擇「測試」任何一個孩子,所以我可以將它們顯示給用戶?我應該改變我的結構嗎?

我的意思是讓我們說我有這樣的:

tests: 
- 12391239123 
    - title: "My first test" 
    - user_uid: 12312345 
- 59696995883 
    - title: "Second test" 
    - user_uid 12352355 

我要選擇這兩個對象之一,所以我可以將它們顯示給用戶。它必須是完全隨機的。

也有可能查詢數據庫,所以我得到所有的孩子,其中user_uid等於我提供的用戶uid?如果是這樣,怎麼樣?

+1

**是**的問題「是否可以查詢數據庫,所以我得到的所有孩子,其中user_uid等於我提供的用戶uid?」 –

+0

,我該怎麼做? @ EICaptainv2.0 –

+0

你找到答案了嗎? – UmarZaii

回答

0
  • 首先,您必須查詢來自firebase的所有數據。然後你就可以正常使用隨機函數來獲得從該快照中隨機值。(第二部分)
  • 要查詢基礎上,你可以使用下面的代碼的用戶ID(第一部分)

    ref.child("users").queryEqualToValue("userId").observeEventType(.Value, withBlock: { snapshot in 
    print(snapshot) 
    } 
    
    
    
    
    for _ in 1...10 { 
    //generate a random number between 1 and the amount of questions you have 
    var randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1 
    
    //The reference to your questions in firebase (this is an example from firebase itself) 
    let ref = Firebase(url: "https://dinosaur-facts.firebaseio.com/dinosaurs") 
    //Order the questions on their value and get the one that has the random value 
         ref.queryOrderedByChild("value").queryEqualToValue(randomNumber).observeEventType(.ChildAdded, withBlock: { snapshot in 
    //Do something with the question 
    println(snapshot.key) 
    }) 
    } 
    
+0

因此可以說我的數據庫中有100,000個用戶,我應該檢索100,000個值?你確定這是一個最佳解決方案嗎? –

+0

這可能不是最佳解決方案,但沒有爲此目的而由Firebase提供的特定功能。 –

2

試着改變你的JSON樹這樣的: -

Users: 
    - uid 
    - email 

tests 
    - noOfTotalTest : 4 // Lets say 4 
    - id 
    - title 
    - user_uid 
    - index // Just index of the post 

現在使用此代碼塊: -

FIRDatabase.database().reference().child("tests/noOfTotalTest").observeSingleEvent(of: .value, with: {(Snap) in 


     let totalNoOfTest = Snap.value as! Int 
     print(totalNoOfTest) 
     let randNum = Int(arc4random_uniform(UInt32(totalNoOfTest))) + 1 
     print(randNum) 
     FIRDatabase.database().reference().child("tests").queryOrdered(byChild: "index").queryEqual(toValue: randNum).observeSingleEvent(of: .value, with: {(Snapshot) in 

      print(Snapshot.value!) 

     }) 
    }) 

現在只要您發佈一個測試你的數據庫,你得做這些事情: -

  • 查詢總數沒有在數據庫測試中,noOfTotalTest
  • 一旦檢索到的增量是由+ 1,更新noOfTotalTest,並把它與其他的測試細節,並將其設置爲你的數據庫
  • ,過程上....
  • 進行

PS: -爲了使後剛剛/ SAVING: -

FIRDatabase.database().reference().child("tests/noOfTotalTest").observeSingleEvent(of: .value, with: {(Snap) in 

if Snap.exists(){ 

      // This is not the first post 

      let totalNoOfTest = Snap.value as! Int 

      FIRDatabase.database().reference().child("tests").childByAutoId().setValue(["userID" : UID, "details" : Details, "index" : totalNoOfTest + 1]) 
    FIRDatabase.database().reference().child("tests/noOfTotalTest").setValue(totalNoOfTest + 1) 
     } else { 

     // This is your first post 

     FIRDatabase.database().reference().child("tests").childByAutoId().setValue(["userID" : UID, "details" : Details, "index" : 1]) 
    FIRDatabase.database().reference().child("tests/noOfTotalTest").setValue(1) 

     } 

}) 

擴展這個讓你能夠刪除,你可以保存在你的節點的活躍指數,其你需要隨機化。

對於添加到您的JSON樹: -

active_Indexes :{ 

    12 : true, 
    09 : true, 
    198 : true, 
    11: true, 
    103 : true, 
    } 

現在你需要的是這些類型的索引存儲在一個字典,然後隨機化數組元素: -

var localIndexDirectory = [Int : Bool] 

//Listen to any changes to the database, and update your local index directory accordingly 


override func viewDidLoad() { 
    super.viewDidLoad() 


    FIRDatabase.database().reference().child("active_Index").observe(.childRemoved, with: {(Snap) in 

     print(Snap.value) 
     let keyToBeChanged = Int(Snap.key)! 
     self.localIndexDirectory.removeValue(forKey: keyToBeChanged) 
     print(self.localIndexDirectory) 

    }) 

    FIRDatabase.database().reference().child("active_Index").observe(.childAdded, with: {(Snap) in 

     print(Snap) 
     let keyToBeChanged = Int(Snap.key)! 
     self.localIndexDirectory.updateValue(true, forKey: keyToBeChanged) 
     print(self.localIndexDirectory) 

    }) 
} 

這將讓您的數據庫中的索引更新(SINCE .observe是您的網絡線程中的連續線程),那麼您所需要的就是隨機化這些索引t那個特定時間,並且針對該特定索引詢問測試。但現在要激活應用程序中的刪除功能,您還需要修改保存功能,即每當將新節點保存到數據庫時,請確保您還在數據庫中追加了active_Indexes節點,指數。

PPS: -您還需要更新您的安全規則來處理不同的認證狀態。

+0

當我開始刪除帖子時,這不會造成問題嗎?那麼一些索引將是空的? –

+0

我已經更新了我的答案。如果它解決了你的問題,接受和upvote這個答案:)快樂編碼 – Dravidian

相關問題