AutoCompleteTextField and potentially long filtering?

9 views
Skip to first unread message

Thomas

unread,
Apr 17, 2019, 2:14:50 PM4/17/19
to CodenameOne Discussions
The update of the suggestions popup of an AutoCompleteTextField is said to be completely asynchronous. 
However, looking at the code of the AutoCompleteTextField class, it looks like the filtering and creation of the model for the popup is called on the EDT thread directly. 
The only thing I see is a call to  Display.getInstance().callSerially(new Runnable() but as far as I  understand it, this do not create a thread in parallel to the EDT but only postpone the operations in this runnable to the next EDT cycle
So my question is: if I have a filtering process to create the popup that might be long (because the server takes some time to answer or the post-filtering of the server answer is complex), should I encapsulate that filtering into an invokeAndBlock call?
For example if I have:

final DefaultListModel<String> options = new DefaultListModel<>();
AutoCompleteTextField atf = new AutoCompleteTextField(options) {
            @Override
            protected boolean filter(String text) {
                if(text.length() == 0) {
                    return false;
                }
                String[] l = getSuggestions(text); 
                if(l == null || l.length == 0) {
                    return false;
                }

                options.removeAll();
                for(String s : l) {
                    options.addItem(s);
                }
                return true;
            }
        };

and getSuggestions(text) can potentially take a while, should I wrote:

final DefaultListModel<String> options = new DefaultListModel<>();
AutoCompleteTextField atf = new AutoCompleteTextField(options) {
    @Override
    protected boolean filter(String text) {
        if(text.length() == 0) {
            return false;
        }
    
	Display.getInstance().invokeAndBlock(new Runnable() {
		public void run() { 
			String[] l = getSuggestions(text); 
				
			options.removeAll();
			if(l != null && l.length > 0) {
				for(String s : l) {
					options.addItem(s);
				}
			}
	    } 
	});

	if(options.getSize()==0){
		return false;
	}
	return true;				
    }
};
 
to be sure not to block the EDT?

Shai Almog

unread,
Apr 17, 2019, 10:34:31 PM4/17/19
to CodenameOne Discussions
Everything is called on the EDT except when explicitly stated otherwise. This is especially true for the UI.
Asynchronous doesn't mean it isn't invoked on the EDT it means that it is called later but it would still be on the EDT. You can block using "legal" means such as addToQueueAndWait or invokeAndBlock.
Reply all
Reply to author
Forward
0 new messages