Hi!
(First post, as an enthusiastic new CBL developer on IOS…)
I am on the fairly recent Xcode (7.3.1), starting to use CBL couchbase-lite-ios-community_1.2.1-13.
Currently I am trying to understand how to use the ability to restrict a query by setting the .keys property.
My understanding of this feature is that CBL should consider only documents whose documentID is in the array that I am passing to the query.
So I am running unit tests to get my ‘CBL JSON’ caching object to do what I need.
I have a set of ‘Locations’ (JSON documents with ID style ‘Location-1’, …, 'Location-), that are indexed.
The method that runs the query does not put anything in the .keys query property, be default.
So the result I would expect in the code shown here under is to first get all 15 locations, as checked by the first XCTAssert.
On the second run, the only difference is that I do set 4 location keys in the keys property. The same query is run, followed by the same ‘enumerator walking’ method that just adds all found keys in an array
This array the test receives is empty; while I would expect the 4 locations to be the ones I do find (see the copied debug ‘trace’)…
What I am doing wrong?
Thanks!
Aldo
// ********************************************
func test_GetAllLocations() {
var resultsArray: NSArray?
let cacheManager = INAJSONCacheManager.sharedInstance
var resultSet = cacheManager.listLocationsByType(.All)
if let results = resultSet.result {
resultsArray = cacheManager.traverseEnumeratorForKeysArray(results)
XCTAssert(resultsArray?.count == 15 , "Could not find the expected number of locations!!")
print("Found locations (15):\n\(resultsArray!)")
}
resultSet = cacheManager.listLocationsByType(.All, asChildSetOfParentSetOfKeys: ["Location-1", "Location-2", "Location-3", "Location-10"])
if let results = resultSet.result {
resultsArray = cacheManager.traverseEnumeratorForKeysArray(results)
XCTAssert(resultsArray?.count == 4 , "Could not find the expected number of locations!!")
print("Found locations (4):\n\(resultsArray!)")
}
}
// ********************************************
func listLocationsByType(typeKey: LocationSelectorType, asChildSetOfParentSetOfKeys parentKeys: [AnyObject]? = nil) -> (result: CBLQueryEnumerator?, error: ErrorType?) {
var query: CBLQuery?
switch typeKey {
case .All:
query = self.database?.viewNamed(kLocationsIndex).createQuery()
break
case .Main:
query = self.database?.viewNamed(kMainLocationsIndex).createQuery()
break
case .NonMain:
query = self.database?.viewNamed(kNonMainLocationsIndex).createQuery()
break
case .SubLoc:
query = self.database?.viewNamed(kSubLocLocationsIndex).createQuery()
break
}
query?.mapOnly = true
if let parentKeys = parentKeys {
query?.keys = parentKeys
}
var result: CBLQueryEnumerator?
var error: ErrorType?
if let locQuery = query {
do {
result = try locQuery.run()
} catch let locQueryError {
error = locQueryError
}
}
let resultSet = (result: result, error: error)
return resultSet
}
// ********************************************
Test Case ‘-[Test_App.LocationsSearchTests test_GetAllLocations]’ started.
Found locations (15):
(
“Location-7”,
“Location-8”,
“Location-9”,
“Location-10”,
“Location-11”,
“Location-1”,
“Location-4”,
“Location-2”,
“Location-6”,
“Location-12”,
“Location-3”,
“Location-13”,
“Location-5”,
“Location-14”,
“Location-15”
)
Found locations (4):
(
)
Test Case ‘-[Test_App.LocationsSearchTests test_GetAllLocations]’ failed (0.018 seconds).