user3803526 user3803526 - 21 days ago 8
Objective-C Question

How can I search within a Podio App using PodioKit and PKTSearchAPI?

I'm trying to do a simple query within all items in a Podio app. But I don't understand how to access the results of the search request. Here's what I have:

let query = PKTSearchQuery.init(text: "search term")
let search = PKTSearchAPI.requestToSearchInAppWithID(1234567, query: query, offset: 0, limit: 30)


requestToSearchInAppWithID
creates a
PKTRequest
object so it seems something needs to be returned via a
PKTResponse
object. I understand that everything in PodioKit is asynchronous and the examples given here for Objective-C and here for Swift make it clear how responses are received using
onComplete
,
onSuccess
,
onError
completion blocks, but
PKTSearchAPI
seems to lack a completion block.

Any help would be much appreciated.

Answer

The completion blocks you noted from the documentation are accessible on any PKTAsyncTask

To retrieve a PKTAsyncTask given a PKTRequest object, you can use the performRequest method of PKTClient. For example, the following snippet will execute your search request, and then print the results to the console:

let query = PKTSearchQuery.init(text: "search term")
let search = PKTSearchAPI.requestToSearchInAppWithID(1234567, query: query, offset: 0, limit: 30)

let searchTask = PKTClient.currentClient().performRequest(search)

searchTask.onComplete { [weak self] response, error in
  let body = response.body as! [String: AnyObject]
  let items = body["results"] as? [[NSObject:AnyObject]]

  for item in items! {
    print("Search result \(item["rank"]!) title: \(item["title"]!)")
  }
}

Edit: For additional information about the data you can expect in the search response, see the Podio API Docs for Search