2017-02-09 33 views
-2

以下是我的代碼。我試圖檢索存儲在firebase的政治表中的數據,並希望將其存儲在Swift中uitextview類的textview對象中。 create()函數將數據存儲在Firebase表中。如何從Firebase中檢索數據將其存儲在以下給出的Swift代碼中的TextView對象中?

import UIKit 
import Firebase 
import FirebaseDatabase 

class PoliticsNewsViewController: UIViewController{ 
    @IBOutlet weak var Image: UIImageView! 
    @IBOutlet weak var textView: UITextView! 
    var ref:FIRDatabaseReference! 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     //Retrieval and insertion must done here currently 
     create() 
    } 

    func create() 
    { 
     //let date="9 feb 2017" 
     let text="Sidhu joins congress" 
     let aapNews="Arvind kejriwal now in race of wining wining punjab elections as he is gaining 70% seats in punjab" 
     let BJPNews="BJP sufeers major blow in punjab due to demonatization gaining only 40 seats" 
     ref=FIRDatabase.database().reference() 
     ref.child("Politics News Table").childByAutoId().setValue(["Text" : text,"AAP news" : aapNews,"BJP News" : BJPNews]) 
    } 
+1

你的問題是什麼?你的代碼有什麼問題? [編輯]你的問題與相關的細節。 – rmaddy

+0

我的問題是我應該如何從firebase中獲取數據並將其存儲在uitextview的textview對象中,因爲我想在我的應用程序的uitextview面板中顯示數據 –

+0

正如我所說的那樣,用相關詳細信息編輯您的問題。 – rmaddy

回答

1

爲了從firebase獲取數據,您應該使用firebase參考中的observe方法。您提供了一個閉包,以及何時收聽,並且每當數據庫中的數據發生變化時都會調用它。

看看下面這個例子

ref.observe(FIRDataEventType.value, with: { snapshot in 
     guard let info = snapshot.value as? [String : AnyObject], let textToShow = info["Text"] as? String else { 
      return 
     } 
     // The snapshot is an object from firebase that is castable to 
     // an array 
     // The info array contains the information that you database has 
     // under the child that your reference has 

     textView.text = textToShow 
     //Here the textView has updated the text 
    }) 

如需進一步信息,好像是avaialable的事件類型,請查看以下鏈接:

https://firebase.google.com/docs/database/ios/retrieve-data

+0

ok.one查詢。什麼作用被執行?[String:Anyobject]? –

+0

Firebase會返回一個快照,它是FIRSnapshot類的快照,它不是真正可用的。 問題是,類可以表示爲一個字典,這使得它更容易得到它的信息,只需通過從一個鍵獲取它的值。 我在那裏做的是將FIRSnapshot類轉換成類型爲[String:Anyobject]的字典,並將其作爲?運營商。我正在使用as?運算符而不是as!操作員爲了不強制downcast –

+1

@FedericoOjeda好的答案,但*回調*實際上應該是*封閉*。另外,FIRDataSnapshot在很多情況下都非常有用,因爲它不僅包含節點的關鍵字,而且還具有價值。你可以迭代它來檢索子數據。最後,由快照值表示的內容可能不總是字典;它可能是一個字符串,布爾或數組。這是一個非常強大的班級。 – Jay

0

這裏有一個更詳細的答案,附和正確的答案可能會有所幫助。

假設我們有一個帶有帖子的社交媒體應用程序。這裏的帖子節點

posts 
    -Yiias9j9asd 
    post: "a post about stuff" 
    date: "20170209" 
    -Yinjiisidd 
    post: "another post about stuff" 
    date: "20170210" 

,然後將代碼中的所有帖子閱讀並打印到日誌

let postsRef = ref.child("posts") 
postsRef.observeSingleEvent(of: .value, with: { snapshot in 
    for snap in snapshot.children { 
     let postKey = postSnap.key //the key of each post 
     let postSnap = snap as! FIRDataSnapshot //the child data snapshot 
     let postDict = postSnap.value as! [String:AnyObject] //child data dict 

     let post = postDict["post"] as! String 
     let date = postDict["date"] as! String 
     print("postKey: \(postKey) post: \(post) date: \(date)") 
     //now that you have the data, you can add it to a textView 
     // textView.text = post for example 
    } 
}) 

注意,節點鍵名,如同Yiias9j9asd與childByAutoId創建和一般建議讓Firebase創建謹慎的節點名稱。

相關問題