限制从 Swift 中的 Deployd 查询返回的对象
Limit objects that are returned from a Deployd query in Swift
我正在使用 Deployd 作为我的 Swift 应用程序的后端。这就是我目前查询 JSON 数据的方式。我需要限制从查询返回的对象数量。我将自己实现分页部分。我只想知道如何在查询中嵌入下面提到的 $limit
方法。任何建议将不胜感激。
http://docs.deployd.com/docs/collections/reference/querying-collections.html#s-$limit-1416
来自 Deployd 的文档:
The $limit
command allows you to limit the amount of objects that are
returned from a query. This is commonly used for paging, along with
$skip
.
// Return the top 10 scores {
$sort: {score: -1},
$limit: 10 }
import Foundation
class ObjectHandler {
var greetings: [initTable] = []
init(filename: String) {
//filter data
let fileP = NSURL(string: "http://localhost:2403/users/me")
let jsonD = NSData(contentsOfURL:fileP!)
let jso = JSON(data: jsonD!, options: NSJSONReadingOptions.AllowFragments, error: nil)
var id = jso["id"]
let filePath = NSURL(string: "http://localhost:2403/postedjob")
let jsonData = NSData(contentsOfURL:filePath!)
let json = JSON(data: jsonData!, options: NSJSONReadingOptions.AllowFragments, error: nil)
for (key: String, subJson: JSON) in json {
var language:String?, link: String?, description:String?, greetingText: String?
for (key1, value:JSON) in subJson {
switch key1 {
case "briefDes": language = value.string
case "skill": link = value.string
case "userId": description = value.string
case "id": greetingText = value.string
default: break
}
}
let greeting = initTable(language: language, link: link, description: description, greetingText: greetingText)
self.greetings.append(greeting)
self.greetings = self.greetings.filter { [=12=].description == "\(id)"}
}
}
func getGreetingsAsAnyObjects() -> [String: [AnyObject]]{
return [SelectJobConstant.GreetingOBJHandlerSectionKey: greetings.map { [=12=] as AnyObject }]
} }
这就是我让它工作的方式:)
Alamofire.request(.GET, "http://localhost:2403/postedjob", parameters: ["$limit": 2, "$sort": ["userId":"-1"]])
.responseJSON { _, _, JSON, _ in
println(JSON)
}
我正在使用 Deployd 作为我的 Swift 应用程序的后端。这就是我目前查询 JSON 数据的方式。我需要限制从查询返回的对象数量。我将自己实现分页部分。我只想知道如何在查询中嵌入下面提到的 $limit
方法。任何建议将不胜感激。
http://docs.deployd.com/docs/collections/reference/querying-collections.html#s-$limit-1416
来自 Deployd 的文档:
The
$limit
command allows you to limit the amount of objects that are returned from a query. This is commonly used for paging, along with$skip
.// Return the top 10 scores { $sort: {score: -1}, $limit: 10 }
import Foundation
class ObjectHandler {
var greetings: [initTable] = []
init(filename: String) {
//filter data
let fileP = NSURL(string: "http://localhost:2403/users/me")
let jsonD = NSData(contentsOfURL:fileP!)
let jso = JSON(data: jsonD!, options: NSJSONReadingOptions.AllowFragments, error: nil)
var id = jso["id"]
let filePath = NSURL(string: "http://localhost:2403/postedjob")
let jsonData = NSData(contentsOfURL:filePath!)
let json = JSON(data: jsonData!, options: NSJSONReadingOptions.AllowFragments, error: nil)
for (key: String, subJson: JSON) in json {
var language:String?, link: String?, description:String?, greetingText: String?
for (key1, value:JSON) in subJson {
switch key1 {
case "briefDes": language = value.string
case "skill": link = value.string
case "userId": description = value.string
case "id": greetingText = value.string
default: break
}
}
let greeting = initTable(language: language, link: link, description: description, greetingText: greetingText)
self.greetings.append(greeting)
self.greetings = self.greetings.filter { [=12=].description == "\(id)"}
}
}
func getGreetingsAsAnyObjects() -> [String: [AnyObject]]{
return [SelectJobConstant.GreetingOBJHandlerSectionKey: greetings.map { [=12=] as AnyObject }]
} }
这就是我让它工作的方式:)
Alamofire.request(.GET, "http://localhost:2403/postedjob", parameters: ["$limit": 2, "$sort": ["userId":"-1"]])
.responseJSON { _, _, JSON, _ in
println(JSON)
}