Skip to content

Commit 8a8a3f2

Browse files
committed
feat(deleteBy): add new deleteBy method that directly hits the new deleteByQuery endpoint
1 parent 7cacf19 commit 8a8a3f2

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

Source/Index.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,13 +620,36 @@ import Foundation
620620
}
621621
}
622622
}
623+
624+
/// Delete all objects matching a query.
625+
///
626+
/// - parameter query: The query that objects to delete must match.
627+
/// - parameter requestOptions: Request-specific options.
628+
/// - parameter completionHandler: Completion handler to be notified of the request's outcome.
629+
/// - returns: A cancellable operation.
630+
/// + Warning: Deprecated, use deleteBy instead.
631+
///
632+
@objc
633+
@discardableResult public func deleteBy(_ query: Query, requestOptions: RequestOptions? = nil, completionHandler: CompletionHandler? = nil) -> Operation {
634+
let path = "1/indexes/\(urlEncodedName)/deleteByQuery"
635+
let body = [
636+
"params": query.build()
637+
]
638+
return client.performHTTPQuery(path: path, method: .POST, body: body, hostnames: client.readHosts, requestOptions: requestOptions, completionHandler: completionHandler)
639+
}
640+
641+
@objc(deleteBy:completionHandler:)
642+
@discardableResult public func z_objc_deleteBy(_ query: Query, completionHandler: CompletionHandler?) -> Operation {
643+
return self.deleteBy(query, completionHandler: completionHandler)
644+
}
623645

624646
/// Delete all objects matching a query (helper).
625647
///
626648
/// - parameter query: The query that objects to delete must match.
627649
/// - parameter requestOptions: Request-specific options.
628650
/// - parameter completionHandler: Completion handler to be notified of the request's outcome.
629651
/// - returns: A cancellable operation.
652+
/// + Warning: Deprecated, use deleteBy instead.
630653
///
631654
@objc
632655
@discardableResult public func deleteByQuery(_ query: Query, requestOptions: RequestOptions? = nil, completionHandler: CompletionHandler? = nil) -> Operation {

Tests/IndexTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,53 @@ class IndexTests: OnlineTestCase {
844844
self.waitForExpectations(timeout: expectationTimeout, handler: nil)
845845
}
846846

847+
func testDeleteBy() {
848+
let expectation = self.expectation(description: #function)
849+
var objects: [JSONObject] = []
850+
for i in 0..<3000 {
851+
objects.append(["dummy": i])
852+
}
853+
854+
// Add a batch of objects.
855+
index.addObjects(objects, completionHandler: { (content, error) -> Void in
856+
if error != nil {
857+
XCTFail(error!.localizedDescription)
858+
expectation.fulfill()
859+
} else {
860+
// Wait for the objects to be indexed.
861+
self.index.waitTask(withID: content!["taskID"] as! Int, completionHandler: { (content, error) -> Void in
862+
if error != nil {
863+
XCTFail(error!.localizedDescription)
864+
expectation.fulfill()
865+
} else {
866+
// Delete by query.
867+
let query = Query()
868+
query.numericFilters = ["dummy < 1500"]
869+
self.index.deleteBy(query, completionHandler: { (content, error) -> Void in
870+
if error != nil {
871+
XCTFail(error!.localizedDescription)
872+
expectation.fulfill()
873+
} else {
874+
// Check that the deleted objects no longer exist.
875+
self.index.browse(query: query, completionHandler: { (content, error) in
876+
if error != nil {
877+
XCTFail(error!.localizedDescription)
878+
} else {
879+
XCTAssertEqual((content!["hits"] as? [Any])?.count, 0)
880+
XCTAssertNil(content!["cursor"])
881+
}
882+
expectation.fulfill()
883+
})
884+
}
885+
})
886+
}
887+
})
888+
}
889+
})
890+
891+
self.waitForExpectations(timeout: expectationTimeout, handler: nil)
892+
}
893+
847894
func testSearchDisjunctiveFaceting() {
848895
let expectation = self.expectation(description: "testAddObjects")
849896
let objects: [JSONObject] = [

Tests/ObjectiveCBridging.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ - (void)testIndex {
278278
[index deleteByQuery:[Query new] completionHandler:^(NSDictionary<NSString*,id>* content, NSError* error) {
279279
// Do nothing.
280280
}];
281+
[index deleteBy:[Query new] completionHandler:^(NSDictionary<NSString*,id>* content, NSError* error) {
282+
// Do nothing.
283+
}];
281284
[index searchDisjunctiveFaceting:[Query new] disjunctiveFacets:@[ @"disjunctive", @"facets" ] refinements:@{ @"facets": @[ @"refinements" ] } completionHandler:^(NSDictionary<NSString*,id>* content, NSError* error) {
282285
// Do nothing.
283286
}];

0 commit comments

Comments
 (0)