# Request

> Executes a search request that will fetch search results asynchronously.

## Definition

* **Type:** Method
* **Namespace:** [UnityEditor.Search](/engine/6000.5/script-reference/unityeditor/search.md)
* **Assembly:** UnityEditor

## Request(SearchContext, SearchFlags)

Executes a search request that will fetch search results asynchronously.

```csharp
public static ISearchList Request(SearchContext context, SearchFlags options = SearchFlags.None)
```

### Parameters

**** (\[SearchContext]\(/engine/6000.5/script-reference/unityeditor/search/searchcontext)): Search context used to track asynchronous requests. You need to dispose of the context yourself.**** (\[SearchFlags]\(/engine/6000.5/script-reference/unityeditor/search/searchflags)): Options defining how the query is performed.

### Returns

| Type                                                                             | Description                        |
| -------------------------------------------------------------------------------- | ---------------------------------- |
| [ISearchList](/engine/6000.5/script-reference/unityeditor/search/isearchlist.md) | Asynchronous list of search items. |

### Remarks

The following example executes a query and print results over many frames using [EditorApplication.update](/engine/6000.5/script-reference/unityeditor/editorapplication/update.md).

```csharp
[MenuItem("Examples/SearchService/Request List")]
public static void RequestList()
{
    ISearchList results = SearchService.Request("*.cs");

    // It is important to note that when you request some search results,
    // that you need to enumerate them asynchronously using the returned search list.
    AsyncResultEnumerator.Fetch(results, item => Debug.Log(item));
}

class AsyncResultEnumerator
{
    private Action<SearchItem> m_OnEnumerate;
    private IEnumerator<SearchItem> m_Iterator;

    public static AsyncResultEnumerator Fetch(ISearchList results, Action<SearchItem> onEnumerate)
    {
        return new AsyncResultEnumerator(results, onEnumerate);
    }

    public AsyncResultEnumerator(ISearchList results, Action<SearchItem> onEnumerate)
    {
        m_OnEnumerate = onEnumerate;
        m_Iterator = results.GetEnumerator();
        EditorApplication.update += EnumerateResults;
    }

    private void EnumerateResults()
    {
        if (m_Iterator == null || !m_Iterator.MoveNext())
        {
            m_Iterator = null;
            EditorApplication.update -= EnumerateResults;
        }
        else if (m_Iterator.Current != null)
            m_OnEnumerate(m_Iterator.Current);
    }
}
```

If you are running a coroutine you can yield results like the following:

```csharp
public static IEnumerable<SearchItem> YieldResults()
{
    ISearchList results = SearchService.Request("*.cs");
    foreach (var result in results)
        yield return result;
}
```

## Request(string, SearchFlags)

Executes a search request that will fetch search results asynchronously.

```csharp
public static ISearchList Request(string searchText, SearchFlags options = SearchFlags.None)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): Search query to be executed.**** (\[SearchFlags]\(/engine/6000.5/script-reference/unityeditor/search/searchflags)): Options defining how the query is performed.

### Returns

| Type                                                                             | Description                        |
| -------------------------------------------------------------------------------- | ---------------------------------- |
| [ISearchList](/engine/6000.5/script-reference/unityeditor/search/isearchlist.md) | Asynchronous list of search items. |

### Remarks

The following example executes a query and print results over many frames using [EditorApplication.update](/engine/6000.5/script-reference/unityeditor/editorapplication/update.md).

```csharp
[MenuItem("Examples/SearchService/Request List")]
public static void RequestList()
{
    ISearchList results = SearchService.Request("*.cs");

    // It is important to note that when you request some search results,
    // that you need to enumerate them asynchronously using the returned search list.
    AsyncResultEnumerator.Fetch(results, item => Debug.Log(item));
}

class AsyncResultEnumerator
{
    private Action<SearchItem> m_OnEnumerate;
    private IEnumerator<SearchItem> m_Iterator;

    public static AsyncResultEnumerator Fetch(ISearchList results, Action<SearchItem> onEnumerate)
    {
        return new AsyncResultEnumerator(results, onEnumerate);
    }

    public AsyncResultEnumerator(ISearchList results, Action<SearchItem> onEnumerate)
    {
        m_OnEnumerate = onEnumerate;
        m_Iterator = results.GetEnumerator();
        EditorApplication.update += EnumerateResults;
    }

    private void EnumerateResults()
    {
        if (m_Iterator == null || !m_Iterator.MoveNext())
        {
            m_Iterator = null;
            EditorApplication.update -= EnumerateResults;
        }
        else if (m_Iterator.Current != null)
            m_OnEnumerate(m_Iterator.Current);
    }
}
```

If you are running a coroutine you can yield results like the following:

```csharp
public static IEnumerable<SearchItem> YieldResults()
{
    ISearchList results = SearchService.Request("*.cs");
    foreach (var result in results)
        yield return result;
}
```

## Request(string, Action\<SearchContext, IList\<SearchItem>>, SearchFlags)

Executes a search request and calls back the specified function when all results are available.

```csharp
public static void Request(string searchText, Action<SearchContext, IList<SearchItem>> onSearchCompleted, SearchFlags options = SearchFlags.None)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): **** (\[Action\<SearchContext, IList\<SearchItem>>]\(https\://learn.microsoft.com/dotnet/api/system.action-1)): Callback invoked when the search request is completed and all results are available.**** (\[SearchFlags]\(/engine/6000.5/script-reference/unityeditor/search/searchflags)):&#x20;

### Examples

```csharp
[MenuItem("Examples/SearchService/Request All")]
public static void RequestAll()
{
    SearchService.Request("t:texture", (SearchContext context, IList<SearchItem> items) =>
    {
        Debug.Log("All Textures");
        foreach (var item in items)
            Debug.Log(item);
    }, SearchFlags.Debug);
}
```

## Request(string, Action\<SearchContext, IEnumerable\<SearchItem>>, Action\<SearchContext>, SearchFlags)

Executes a search request and callbacks for every batch of incoming results. It is possible to get duplicate items, so filter the final list if needed.

```csharp
public static void Request(string searchText, Action<SearchContext, IEnumerable<SearchItem>> onIncomingItems, Action<SearchContext> onSearchCompleted, SearchFlags options = SearchFlags.None)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): **** (\[Action\<SearchContext, IEnumerable\<SearchItem>>]\(https\://learn.microsoft.com/dotnet/api/system.action-1)): Callback invoked everytime a batch of results are found and available.**** (\[Action\<SearchContext>]\(https\://learn.microsoft.com/dotnet/api/system.action)): Callback invoked when the search request is completed.**** (\[SearchFlags]\(/engine/6000.5/script-reference/unityeditor/search/searchflags)):&#x20;

### Examples

```csharp
[MenuItem("Examples/SearchService/Request Async")]
public static void RequestAsync()
{
    var batchCount = 0;
    var totalItemCount = 0;

    void OnIncomingResults(SearchContext context, IEnumerable<SearchItem> items)
    {
        var batchItemCount = items.Count();
        totalItemCount += batchItemCount;
        Debug.Log($"#{++batchCount} Incoming materials ({batchItemCount}): {string.Join("\n", items)}");
    }

    void OnSearchCompleted(SearchContext context)
    {
        Debug.Log($"Query <b>\"{context.searchText}\"</b> completed with a total of {totalItemCount}");
    }

    SearchService.Request("t:material", OnIncomingResults, OnSearchCompleted, SearchFlags.Debug);
}
```

## Request(SearchContext, Action\<SearchContext, IList\<SearchItem>>, SearchFlags)

Executes a search request and calls back the specified function when all results are available.

```csharp
public static void Request(SearchContext context, Action<SearchContext, IList<SearchItem>> onSearchCompleted, SearchFlags options = SearchFlags.None)
```

### Parameters

**** (\[SearchContext]\(/engine/6000.5/script-reference/unityeditor/search/searchcontext)): **** (\[Action\<SearchContext, IList\<SearchItem>>]\(https\://learn.microsoft.com/dotnet/api/system.action-1)): Callback invoked when the search request is completed and all results are available.**** (\[SearchFlags]\(/engine/6000.5/script-reference/unityeditor/search/searchflags)):&#x20;

### Examples

```csharp
[MenuItem("Examples/SearchService/Request All")]
public static void RequestAll()
{
    SearchService.Request("t:texture", (SearchContext context, IList<SearchItem> items) =>
    {
        Debug.Log("All Textures");
        foreach (var item in items)
            Debug.Log(item);
    }, SearchFlags.Debug);
}
```

## Request(SearchContext, Action\<SearchContext, IEnumerable\<SearchItem>>, Action\<SearchContext>, SearchFlags)

Executes a search request and callbacks for every batch of incoming results. It is possible to get duplicate items, so filter the final list if needed.

```csharp
public static void Request(SearchContext context, Action<SearchContext, IEnumerable<SearchItem>> onIncomingItems, Action<SearchContext> onSearchCompleted, SearchFlags options = SearchFlags.None)
```

### Parameters

**** (\[SearchContext]\(/engine/6000.5/script-reference/unityeditor/search/searchcontext)): **** (\[Action\<SearchContext, IEnumerable\<SearchItem>>]\(https\://learn.microsoft.com/dotnet/api/system.action-1)): Callback invoked everytime a batch of results are found and available.**** (\[Action\<SearchContext>]\(https\://learn.microsoft.com/dotnet/api/system.action)): Callback invoked when the search request is completed.**** (\[SearchFlags]\(/engine/6000.5/script-reference/unityeditor/search/searchflags)):&#x20;

### Examples

```csharp
[MenuItem("Examples/SearchService/Request Async")]
public static void RequestAsync()
{
    var batchCount = 0;
    var totalItemCount = 0;

    void OnIncomingResults(SearchContext context, IEnumerable<SearchItem> items)
    {
        var batchItemCount = items.Count();
        totalItemCount += batchItemCount;
        Debug.Log($"#{++batchCount} Incoming materials ({batchItemCount}): {string.Join("\n", items)}");
    }

    void OnSearchCompleted(SearchContext context)
    {
        Debug.Log($"Query <b>\"{context.searchText}\"</b> completed with a total of {totalItemCount}");
    }

    SearchService.Request("t:material", OnIncomingResults, OnSearchCompleted, SearchFlags.Debug);
}
```
