Skip to content

Commit 8a3d2fb

Browse files
author
Clément Le Provost
authored
Add an Index.getObjects() method with attributes to retrieve (#121)
Fixes #120.
1 parent 8aed27f commit 8a3d2fb

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Source/Index.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,29 @@ import Foundation
173173

174174
return client.performHTTPQuery(path, method: .POST, body: request, hostnames: client.readHosts, completionHandler: completionHandler)
175175
}
176+
177+
/// Get several objects from this index, optionally restricting the retrieved content.
178+
///
179+
/// - parameter objectIDs: Identifiers of objects to retrieve.
180+
/// - parameter attributesToRetrieve: List of attributes to retrieve. If `nil`, all attributes are retrieved.
181+
/// If one of the elements is `"*"`, all attributes are retrieved.
182+
/// - parameter completionHandler: Completion handler to be notified of the request's outcome.
183+
/// - returns: A cancellable operation.
184+
///
185+
@objc public func getObjects(objectIDs: [String], attributesToRetrieve: [String], completionHandler: CompletionHandler) -> NSOperation {
186+
let path = "1/indexes/*/objects"
187+
var requests = [AnyObject]()
188+
requests.reserveCapacity(objectIDs.count)
189+
for id in objectIDs {
190+
let request = [
191+
"indexName": indexName,
192+
"objectID": id,
193+
"attributesToRetrieve": attributesToRetrieve.joinWithSeparator(",")
194+
]
195+
requests.append(request)
196+
}
197+
return client.performHTTPQuery(path, method: .POST, body: ["requests": requests], hostnames: client.readHosts, completionHandler: completionHandler)
198+
}
176199

177200
/// Partially update an object.
178201
///

Tests/IndexTests.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ class IndexTests: XCTestCase {
259259
})
260260
}
261261
})
262-
263262
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
264263
}
265264

@@ -331,6 +330,44 @@ class IndexTests: XCTestCase {
331330
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
332331
}
333332

333+
func testGetObjectsFiltered() {
334+
let expectation = expectationWithDescription(#function)
335+
let objects: [[String: AnyObject]] = [
336+
["objectID": "1", "name": "Snoopy", "kind": "dog"],
337+
["objectID": "2", "name": "Woodstock", "kind": "bird"]
338+
]
339+
index.addObjects(objects, completionHandler: { (content, error) -> Void in
340+
guard let content = content else {
341+
XCTFail("Error during addObjetcs: \(error)")
342+
expectation.fulfill()
343+
return
344+
}
345+
self.index.waitTask(content["taskID"] as! Int, completionHandler: { (content, error) -> Void in
346+
guard error == nil else {
347+
XCTFail("Error during waitTask: \(error)")
348+
expectation.fulfill()
349+
return
350+
}
351+
self.index.getObjects(["1", "2"], attributesToRetrieve: ["name", "nonexistent"], completionHandler: { (content, error) -> Void in
352+
guard let content = content else {
353+
XCTFail("Error during getObjects: \(error)")
354+
expectation.fulfill()
355+
return
356+
}
357+
let items = content["results"] as! [[String: String]]
358+
XCTAssertEqual(2, items.count)
359+
XCTAssertEqual(items[0]["name"]! as String, "Snoopy")
360+
XCTAssertEqual(items[1]["name"]! as String, "Woodstock")
361+
XCTAssertNil(items[0]["kind"])
362+
XCTAssertNil(items[1]["kind"])
363+
expectation.fulfill()
364+
})
365+
})
366+
})
367+
368+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
369+
}
370+
334371
func testPartialUpdateObject() {
335372
let expectation = expectationWithDescription("testPartialUpdateObject")
336373
let object = ["city": "New York", "initial": "NY", "objectID": "a/go/?à"]

0 commit comments

Comments
 (0)