Вопрос

I have a custom search content provider that combines recent searches and specific results from making a network REST call.

The problem is that if the network hangs, or is slow, no results come back. I'm wondering if there's a way to progressively return results ... in my case, return previous searches immediately, and return network search results when they are available.

I'm not seeing how this would be possible, since this appears to be a pull model. the UI component is requesting the search results, and has no way to know when to re-request to obtain the network based results ... that's even if I could understand to hook into it to make it re-request the results.

Any ideas?

Это было полезно?

Решение

This is how I went about handling this problem. the content provider itself never accesses the network in the main request thread. in stead, it,

  1. looks for a cached result to the query
  2. if there is a cached result, return that immediately
  3. if there is no cached result, start a worker thread to find the result, and return empty results

when the worker thread from #3 is complete, it broadcasts an intent. this is received by the activity hosting the search, triggering it to re-submit the same search. this time when the search query gets to the content provider, the result is cached, and returned immediately.

the only complexity to this was how to force the activity to re-submit the search without user interaction. the solution was different depending on the version of android,

    public void onReceive(Context context, Intent intent) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        if (query == null) {
            return;
        }
        // if user has typed something new, ignore
        if (!query.equals(searchQuery)) {
            return;
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
            SearchView searchView = (SearchView) searchMenuItem.getActionView();
            searchView.setQuery(query, false);
        } else {
            SearchManager sm = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            ComponentName cm = FolderActivity.this.getComponentName();
            sm.startSearch(query, false, cm, null, false);
        }
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top