Directory | Mobile SDK | 2GIS Documentation

Directory

To search for objects in the directory, first create a SearchManager object by calling one of the following methods:

let searchManager = SearchManager.createOnlineManager(context: sdk.context)

Then, to get object information by its ID, call the searchById() method. The method will return a deferred result with DirectoryObject.

searchManager.searchById(id: id).sink { object in
    print(object?.title)
}

If the object ID is not known, you can create a SearchQuery object using SearchQueryBuilder and pass it to the search() method. The method will return a deferred result with a SearchResult object, which will contain a paginated list of DirectoryObject.

let query = SearchQueryBuilder.fromQueryText(queryText: "pizza").setPageSize(pageSize: 10).build()

searchManager.search(query: query).sink{ searchResult in
    // Get the first object of the first page
    let directoryObjectTitle = searchResult.firstPage?.items?.first?.title ?? "NotFound"
    print(directoryObjectTitle)
}

To get the next page of search results, use the fetchNextPage() method of the page, which will return a deferred result with a Page object.

firstPage?.fetchNextPage().sink{ nextPage in
    let directoryObject = nextPage?.items?.first
}

You can also use object directory to get suggestions when text searching (see Suggest API for demonstration). To do this, create a SuggestQuery object using SuggestQueryBuilder and pass it to the suggest() method. The method will return a deferred result with a SuggestResult object, which will contain a list of Suggest objects.

let query = SuggestQueryBuilder.fromQueryText(queryText: "pizz").setLimit(limit: 10).build()

searchManager.suggest(query: query).sink{ suggestResult in
    // Get the first suggestion from the list
    let firstSuggestTitle = suggestResult.suggests.first ?? ""
    print(firstSuggestTitle)
}