0

I have a problem updating my tableView. So, I know how to implement autocomplete textfield with tableView. The problem is that my keyboard typing is not as smooth as I want it to be. Flow goes like this: when I type 3 characters, my async server method is called. It's response object returns my searched addresses. But the problem is that when I type, for like half a second, I cannot do anything, I need to wait until it loads result. I was wondering to solve it with dispatch_group_t but I need to enter and leave that async and then in notify method actually update my UI. This is the code:

if (string.length == 0 && range.length > 0) {
        if (newText.length >= 3) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [Address getAddreses:newText city:[City getDefaultCity] location:self.lockedPosition block:^(NSError *error, NSArray *addresses) {
                    self.suggestedAddresses = addresses;
                    if (addresses.count < AUTOCOMPLETE_RESULTS_MAX) {
                        self.sugesstedAddressesMaxResult = self.suggestedAddresses;
                    }
                    else {
                        self.sugesstedAddressesMaxResult = [addresses subarrayWithRange:NSMakeRange(0, AUTOCOMPLETE_RESULTS_MAX)];
                    }
                    [self calculateTableViewFrame:self.manualLocationInput tableViewTag:TAG];
                    [self.myTableView reloadData];
                }];
            });

        }
}

What I want to do: type as fast as I can, not to wait on every character half a second to update my UI. Typing should be like typing in messages, but it needs to update my table view according to latest typing.

Flipper
  • 1,107
  • 1
  • 11
  • 32

4 Answers4

0

If you clearly observe your code to fetch results of web service are happening in displath_main-queue. This is why you are facing issues while typing keyboard. Move dispatch_get_main_queue block to next level as shown below. This should your problem.

     //dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
     dispatch_queue_t globalConcurrentQueue =
     dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    dispatch_async(globalConcurrentQueue, ^{

    if (string.length == 0 && range.length > 0) {
    if (newText.length >= 3) {
            [Address getAddreses:newText city:[City getDefaultCity] location:self.lockedPosition block:^(NSError *error, NSArray *addresses) {
                self.suggestedAddresses = addresses;
                if (addresses.count < AUTOCOMPLETE_RESULTS_MAX) {
                    self.sugesstedAddressesMaxResult = self.suggestedAddresses;
                }
                else {
                    self.sugesstedAddressesMaxResult = [addresses subarrayWithRange:NSMakeRange(0, AUTOCOMPLETE_RESULTS_MAX)];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                [self calculateTableViewFrame:self.manualLocationInput tableViewTag:TAG];
                [self.myTableView reloadData];
            }];
        });

    }
  }
}
AskIOS
  • 918
  • 7
  • 19
  • Still facing the same problem. If I type next character in that half second, that new character is not recognised. – Flipper Jan 21 '16 at 10:07
  • Check my updated answer. Even apple uses the same for getting autocomplete search for locations. Lets try. – AskIOS Jan 21 '16 at 10:14
  • I forgot to add that I am doing this in textfield's shouldChangeCharactersInRange: – Flipper Jan 21 '16 at 10:24
  • Where ever you call service is not a matter unless you are running that in GCD queue. As others said check with AFNetworking once. – AskIOS Jan 21 '16 at 10:30
0

whenever u type 3 or more char , getAddreses method call , which is perform on main thread . so it block main thread until it's not complete. i.e. until ur data reload ur ui not respond.

try: fetch data on back ground thread and just reload table on main thread.

Let me know if this works...

0

I have found answer here: cancellation blocks

How to throttle search (based on typing speed) in iOS UISearchBar?

Thank you all very much

Community
  • 1
  • 1
Flipper
  • 1,107
  • 1
  • 11
  • 32
0

In my opinion the typical solution in that case is to let the user types fluently until he stops typing. Then you can make a request in background and as soon as you receive the response, just update your textfield in the main thread.

In that direction you can just focus in your business logic, and to use a library like SearchTextField for the suggestion list UI.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
pishke
  • 11
  • 1