【问题标题】:How to configure the Algolia InstantSearch class if you have 2 different search bars for 2 distinct indexes?如果您有 2 个不同的搜索栏用于 2 个不同的索引,如何配置 Algolia InstantSearch 类?
【发布时间】:2019-06-25 17:59:42
【问题描述】:

我正在使用来自AlgoliaInstantSearch,它基本上是您配置并绑定到搜索栏(小部件)的单例。 问题是在他们的演示中,InstantSearch 类是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)

所以,我的问题是: 如何让两个不同的搜索栏分别搜索不同的索引?

【问题讨论】:

    标签: ios swift algolia instantsearch


    【解决方案1】:

    通过查看他们的code,您可以通过提供必填字段来使用他们方便的 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)
    }
    

    现在由您决定是否要为一个视图控制器或整个应用创建实例

    【讨论】:

    • 谢谢我错过了方便的初始化。是的,我更喜欢尽可能避免使用 Singleton,并通过使用注入传递对象以一种干净的方式构建应用程序。我的用例也可以用 2 列的 UI 表示,每列 1 个搜索栏。如果这样做,使用 Singleton 看起来很糟糕,因为每次搜索栏成为第一响应者时都必须重新配置,如果每次配置时都会调用网络,那就更糟了。我只是为那些想知道的人添加此评论。 ^^/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2016-06-12
    • 2022-01-12
    • 2010-12-01
    相关资源
    最近更新 更多