从Firebase中检索一定数量的孩子

时间:2017-03-28 20:48:07

标签: ios swift firebase firebase-realtime-database

我正在尝试创建一个 tableView ,只加载一定数量的值,让我们的数据库大约30个左右。

我已经查看了其他教程,但它们是 Objective-C 或其他语言。 Swift 会很棒!

2 个答案:

答案 0 :(得分:3)

<强>夫特即可。获取PublishRequest publishRequest = new PublishRequest(); publishRequest.setTargetArn("arn:aws:sns:ap-south-1:818862955266:endpoint/GCM/TestApp/a1ec8114-58c9-371b-bb76-d8d16e674e52"); String message = "{\"GCM\": \"{ \"notification\": { \"text\": \"test message\" } }\"}"; ObjectMapper mapper = new ObjectMapper(); PushRequest pushRequest = new PushRequest(); pushRequest.setDef("Test"); GCM gcm = new GCM(); Notification notification = new Notification(); notification.setText("hello"); gcm.setNotification(notification); pushRequest.setGcm(gcm); String jsonInString = mapper.writeValueAsString(pushRequest); publishRequest.setMessage(jsonInString); publishRequest.setMessageStructure("json"); System.out.println("Publist request:"+publishRequest.toString()); PublishResult publishResult = amazonSNSTemplate.getAmazonSNSClient().publish(publishRequest); System.out.println(publishResult.toString()); System.out.println(publishResult.getSdkResponseMetadata().toString()); public class PushRequest { @JsonProperty("default") private String def; @JsonProperty("GCM") private GCM gcm; public String getDef() { return def; } public void setDef(String def) { this.def = def; } public GCM getGcm() { return gcm; } public void setGcm(GCM gcm) { this.gcm = gcm; } } public class GCM { private Notification notification; @JsonProperty("notification") public Notification getNotification() { return notification; } public void setNotification(Notification notification) { this.notification = notification; } } public class Notification { private String text; @JsonProperty("text") public String getText() { return text; } public void setText(String text) { this.text = text; } } 到父节点,然后使用此函数运行查询:

FIRDatabaseReference

然后使用以下方法检索生成的子项:

func queryLimited(toFirst limit: UInt) -> FIRDatabaseQuery

答案 1 :(得分:1)

有关FireBase文档的参考,请参阅: https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-queries

引用了Objective-C和Swift Code。

简而言之:您可以使用两个功能:

open func queryLimitedToLast(limit: UInt) -> FIRDatabaseQuery
open func queryLimitedToFirst(limit: UInt) -> FIRDatabaseQuery

UInt是一种数据类型&#34; unsigned int&#34;。意思是:将正数传递给函数: - )

不要忘记,通过调用&#34; observeEventType&#34;来观察查询结果。在它上面 - 这个函数包含一个块,你可以在其中使用你的项目(例如你可以将它们添加到一个项目数组中。该数组可以用于你的tableview的数据源。最后,重新加载tableview < / p>

调用它的示例可能看起来像这样(对于30个条目)(例如在viewDidLoad中):

DataSource数组声明为类变量

var objects = [MyObjects]()

执行您的查询:

override func viewDidLoad() {
    super.viewDidLoad()

    let ref = Firebase(url:"https://dinosaur-facts.firebaseio.com/dinosaurs")
    ref.queryLimitedToLast(30)
       .observeEventType(.ChildAdded, withBlock: { snapshot in
        println(snapshot.key)
        var newItems: [MyObject] = []
        for item in snapshot.children {
            let myObject = MyObject(snapshot: item as! FIRDataSnapshot)
            newItems.append(myObject)
        }
        self.objects = newItems
        self.tableView.reloadData()
    })
}

有趣的表格视图DataSource方法可能如下所示:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }