Skip to content

Instantly share code, notes, and snippets.

@dangthaison91
Last active June 1, 2016 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dangthaison91/7e1f97521cd90f5a76a65b5eb0fe59b6 to your computer and use it in GitHub Desktop.
Save dangthaison91/7e1f97521cd90f5a76a65b5eb0fe59b6 to your computer and use it in GitHub Desktop.
private var lastScheduledWord: dispatch_cancelable_block_t?
private var lastRequest: NSURLSessionTask?
var numberOfRetries = 3
let NUMBER_OF_RETRIES = 3
func searchRepository(keyword: String) {
cancel_block(lastScheduledWord)
lastRequest?.cancel()
lastRequest = nil
lastScheduledWord = dispatch_after_delay(0.5) {
lastRequest = findRepository(keyword, success: { repository in
findIssues(repository)
}, failure: { error in
if numberOfRetries++ < NUMBER_OF_RETRIES {
searchRepository(keyword)
} else {
numberOfRetries = 0
displayError(error)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment