I'm using the Twitter API to retrieve a list of tweets. The method should return the list of tweets, but the problem is that it returns before the request is over, which results in an empty list being returned. How do I fix this? Here's the source code of the method:
// Call to search through twitter with a query
func searchQuery(query: String) -> [Tweet] {
var tweets: [Tweet] = []
var query_URL = query.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
if let query_URL = query_URL {
TwitterClient.sharedInstance.GET("https://api.twitter.com/1.1/search/tweets.json?q=\(query_URL)", parameters: nil, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
tweets = parseJSON(response)
println(tweets.count)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
})
}
println("ret: \(tweets.count)")
return tweets
}
In the above code, the output would be
ret: 0
15
I've tried using dispatch groups, but I couldn't get them to work. Here's what I did with GCD:
// Call to search through twitter with a query
func searchQuery(query: String) -> [Tweet] {
var tweets: [Tweet] = []
var query_URL = query.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
var group = dispatch_group_create()
if let query_URL = query_URL {
dispatch_group_enter(group)
TwitterClient.sharedInstance.GET("https://api.twitter.com/1.1/search/tweets.json?q=\(query_URL)", parameters: nil, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
tweets = parseJSON(response)
dispatch_group_leave(group)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
dispatch_group_leave(group)
})
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
return tweets
}
But, that code seems to wait forever.
For reference, Tweet
is a struct for saving data related to a tweet. I use it to move around lots of data in a more compact way. parseJSON populates and returns an array of tweets based on the JSON response. Ideally, that returned array would be saved to tweets
, which should then return from the method, but that doesn't happen.
Any ideas or techniques to overcome this will be much appreciated!
Edit: @Hamza Ansari Here's the actual function:
// Call to search through twitter with a query
func searchQuery(query: String, completionHandler:(returntweets: [Tweet]) -> Void) {
var query_URL = query.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
if let query_URL = query_URL {
TwitterClient.sharedInstance.GET("https://api.twitter.com/1.1/search/tweets.json?q=\(query_URL)", parameters: nil, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
var tweets:[Tweet] = parseJSON(response)
println("size in method: \(tweets.count)")
completionHandler(returntweets: tweets)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
})
}
}
But, when called in a different method used to initialize the data source:
// Initializes the data source
func initialize(query: String) {
self.query = query
searchQuery(query, { (returnTweets) -> Void in
self.searches = returnTweets
})
println("size when called: \(searches.count)")
}
The output (in order):
size when called: 0
size in method: 15