@@ -1097,7 +1097,81 @@ import Foundation
10971097 let ( content, error) = OfflineClient . parseResponse ( searchResults)
10981098 return ( MirroredIndex . tagAsLocal ( content: content) , error)
10991099 }
1100+
1101+ // ----------------------------------------------------------------------
1102+ // MARK: - Search for facet values
1103+ // ----------------------------------------------------------------------
11001104
1105+ /// Search for facet values, using the current request strategy to choose between online and offline.
1106+ /// Same parameters as `Index.searchForFacetValues(...)`.
1107+ ///
1108+ @objc ( searchForFacetValuesOf: matching: query: completionHandler: )
1109+ @discardableResult override public func searchForFacetValues( of facetName: String , matching text: String , query: Query ? = nil , completionHandler: @escaping CompletionHandler ) -> Operation {
1110+ // IMPORTANT: A non-mirrored index must behave exactly as an online index.
1111+ if ( !mirrored) {
1112+ return super. searchForFacetValues ( of: facetName, matching: text, query: query, completionHandler: completionHandler)
1113+ }
1114+ // A mirrored index launches a mixed offline/online request.
1115+ else {
1116+ let operation = MixedSearchFacetOperation ( index: self , facetName: facetName, facetQuery: text, query: query, completionHandler: completionHandler)
1117+ offlineClient. mixedRequestQueue. addOperation ( operation)
1118+ return operation
1119+ }
1120+ }
1121+
1122+ private class MixedSearchFacetOperation : OnlineOfflineOperation {
1123+ let facetName : String
1124+ let facetQuery : String
1125+ let query : Query ?
1126+
1127+ init ( index: MirroredIndex , facetName: String , facetQuery: String , query: Query ? , completionHandler: @escaping CompletionHandler ) {
1128+ self . facetName = facetName
1129+ self . facetQuery = facetQuery
1130+ self . query = query
1131+ super. init ( index: index, completionHandler: completionHandler)
1132+ }
1133+
1134+ override func startOnlineRequest( completionHandler: @escaping CompletionHandler ) -> Operation {
1135+ return index. searchForFacetValuesOnline ( of: facetName, matching: facetQuery, query: query, completionHandler: completionHandler)
1136+ }
1137+
1138+ override func startOfflineRequest( completionHandler: @escaping CompletionHandler ) -> Operation {
1139+ return index. searchForFacetValuesOffline ( of: facetName, matching: facetQuery, query: query, completionHandler: completionHandler)
1140+ }
1141+ }
1142+
1143+ /// Search for facet values on the online API, not the local mirror.
1144+ /// This method is an alias of `Index.searchForFacetValues(...)`.
1145+ ///
1146+ @objc ( searchForFacetValuesOnlineOf: matching: query: completionHandler: )
1147+ @discardableResult public func searchForFacetValuesOnline( of facetName: String , matching text: String , query: Query ? = nil , completionHandler: @escaping CompletionHandler ) -> Operation {
1148+ return super. searchForFacetValues ( of: facetName, matching: text, query: query, completionHandler: {
1149+ ( content, error) in
1150+ completionHandler ( MirroredIndex . tagAsRemote ( content: content) , error)
1151+ } )
1152+ }
1153+
1154+ /// Search for facet values on the local mirror, not the online API.
1155+ /// This method is the offline equivalent of `Index.searchForFacetValues(...)`.
1156+ ///
1157+ @objc ( searchForFacetValuesOfflineOf: matching: query: completionHandler: )
1158+ @discardableResult public func searchForFacetValuesOffline( of facetName: String , matching text: String , query: Query ? = nil , completionHandler: @escaping CompletionHandler ) -> Operation {
1159+ assert ( self . mirrored, " Mirroring not activated on this index " )
1160+ let operation = AsyncBlockOperation ( completionHandler: completionHandler) {
1161+ return self . _searchForFacetValuesOffline ( of: facetName, matching: text, query: query)
1162+ }
1163+ operation. completionQueue = client. completionQueue
1164+ self . offlineClient. searchQueue. addOperation ( operation)
1165+ return operation
1166+ }
1167+
1168+ /// Search for facet values on the local mirror synchronously.
1169+ private func _searchForFacetValuesOffline( of facetName: String , matching text: String , query: Query ? ) -> ( content: JSONObject ? , error: Error ? ) {
1170+ assert ( !Thread. isMainThread) // make sure it's run in the background
1171+ let searchResults = localIndex. searchForFacetValues ( of: facetName, matching: text, parameters: query? . build ( ) )
1172+ return OfflineClient . parseResponse ( searchResults)
1173+ }
1174+
11011175 // ----------------------------------------------------------------------
11021176 // MARK: - Notifications
11031177 // ----------------------------------------------------------------------
0 commit comments