0

我正在使用InstantSearchfrom Algolia,它基本上是您配置并绑定到搜索栏(小部件)的单例。问题是在他们的演示中,InstantSearch该类是 a Singleton,因此不能多次实例化。但是,它可以配置多次,但随后会更改整个应用程序的配置。

InstantSearch.shared.configure(
                    appID: algoliaAppID,
                    apiKey: key,
                    index: algoliaUserIndex
                )

他们提供的一种解决方案是多索引搜索,但在我的情况下,它是索引的聚合,而在我的情况下,它只想进行不同的隔离搜索。

对于多个索引参考:

let searcherIds = [SearcherId(index: algoliaUserIndex),
                                   SearcherId(index: algoliaMessageSessionsIndex)]

InstantSearch.shared.configure(appID: algoliaAppID, 
                               apiKey: key, 
                               searcherIds: searcherIds)

所以,我的问题是: 我怎样才能有两个不同的搜索栏,每个搜索栏搜索不同的索引?

4

1 回答 1

0

通过查看他们的代码,您可以通过提供必填字段,使用他们方便的 init 方法获得另一个 InstantSearch 实例。以下是它们提供的四种初始化方法:

/// Create a new InstantSearch reference with the given configurations.
///
/// - parameter appID: the Algolia AppID.
/// - parameter apiKey: the Algolia ApiKey.
/// - parameter index: the name of the index.
@objc public convenience init(appID: String, apiKey: String, index: String) {
    self.init()
    self.configure(appID: appID, apiKey: apiKey, index: index)
}

/// Create a new InstantSearch reference.
///
/// - parameter searcher: the `Searcher` used by InstantSearch
@objc public convenience init(searcher: Searcher) {
    self.init()
    configure(searcher: searcher)
}

// Multi-Index init

/// Create a new InstantSearch reference with the given configurations.
///
/// - parameter appID: the Algolia AppID.
/// - parameter apiKey: the Algolia ApiKey.
/// - parameter indexIds: the identifications for each index
@objc public convenience init(appID: String, apiKey: String, searcherIds: [SearcherId]) {
    self.init()
    self.configure(appID: appID, apiKey: apiKey, searcherIds: searcherIds)
}

/// Create a new InstantSearch reference with the given configurations.
///
/// - parameter searchables: an array of searchables
/// - parameter searchersIds: an array of searcherId that identifies a specific index
@objc public convenience init(searchables: [Searchable], searcherIds: [SearcherId]) {
    self.init()
    self.configure(searchables: searchables, searcherIds: searcherIds)
}

现在,是否要为一个视图控制器或整个应用程序创建实例取决于您

于 2019-06-25T09:59:42.193 回答