Reputation: 618
I have the Textfield USERNAME. Here when the user start to type, for every letter I have to hit server for check "username availability". Now i call it in foreground so it stuck little bit. I could not cancel or refresh the previous connection. Any idea ?
note: I am using AFNetwork sessionManager.
Upvotes: 0
Views: 1101
Reputation: 4329
Your approach is good. but here i suggest something new approach. When you are going to typing At that time for each character why to call web-service and increase the load. Please check below suggestions.
if you are going to check the username, you should call the api after you write at-least 3 letters. because after that only, any word will be able to use as username otherwise those are the characters. This is totally on the functionality requirement which you are implementing.
just wait till user stop typing and then only call web-service. In between is user is typing continuously (till hold for some ammountof time), cancel previous request to call web-service using below code.
[NSObject cancelPerformSelectorsWithTarget:self]; [self performSelector:@selector(sendSearchRequest) withObject:searchText afterDelay:0.1f];
Upvotes: 0
Reputation: 1031
If you are using the AFNetworking as below:
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
NSURLSessionDataTask *task = [sessionManager GET:@"http://example.com/resources.json" parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
you can use
[task cancel];
and if you are using AFNetworking as below:
AFHTTPRequestOperationManager *operationManager=[AFHTTPRequestOperationManager manager];
requestOperation=[operationManager GET:@"" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
}];
you can use
[requestOperation cancel];
Upvotes: 2