0

I am now making a UITextField with AutoComplete that users will get a place name and zip code list after they have tapped one or some letters.

I got an Autocomplelt (https://github.com/cjcoax/Autocomplete) and there is a delegate function:

func autoCompleteItemsForSearchTerm(term: String) -> [AutocompletableOption]

I have to send a http request with term to server and wait for a json response as return.

For network connenction, I used Moya lib and its Method like: CredentialProvider.request(.Autocomplete(term, 10)) { (result) -> () in }

Now, my question is: How can I make a return value after getting the response from server?

Thanks

shilei365
  • 223
  • 2
  • 13
  • 1
    Use completion block – Janmenjaya Sep 28 '16 at 10:29
  • You can use a dispatch group and a dispatch group wait but this will block the main thread and give a poor user experience. You will need to refactor the library so that the completion list can be delivered asynchronously or find a different library or write your own code – Paulw11 Sep 28 '16 at 10:55
  • Using RxSwift can be a better approach [add an event on your request .onNext: get json & do your stuff] https://github.com/ReactiveX/RxSwift – StrawHara Sep 28 '16 at 11:54
  • Look this [example](http://stackoverflow.com/a/39567367/3108877) to set a function's return value in a closure. – Rob Sep 28 '16 at 13:59

1 Answers1

0

Declare a function with completion block:

class func authenticateUser(userName:String?,password:String?,completionHandler: (response:NSDictionary?) -> (Void)) ->Void
{
      completionHandler(response: nil(or)dict)
}

Call a function :

authenticateUser(emailId, password: password, completionHandler: { (response) in
      print(response)
})
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Venkat Reddy
  • 111
  • 1
  • 8