Search code examples
swiftdeployd

Limit objects that are returned from a Deployd query in Swift


I'm using Deployd as back-end for my Swift application. This is how I'm querying the JSON data currently. I need to limit the amount of objects that are returned from a query. I'll implement the pagination part myself. I just want to know that how can I embed the below mentioned $limit method in the query. Any advice would be greatly appreciated.

http://docs.deployd.com/docs/collections/reference/querying-collections.html#s-$limit-1416

From Deployd's Docs:

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 { $0.description == "\(id)"}
        }
    }

    func getGreetingsAsAnyObjects() -> [String: [AnyObject]]{

        return [SelectJobConstant.GreetingOBJHandlerSectionKey: greetings.map { $0 as AnyObject }]
    } }

Solution

  • So this is how I made it to work :)

    Alamofire.request(.GET, "http://localhost:2403/postedjob", parameters: ["$limit": 2, "$sort": ["userId":"-1"]])
            .responseJSON { _, _, JSON, _ in
                println(JSON)
        }