Hi,
I wanted to query from CB using the startKey and endKey property so that I can get some specific documents from CBdatabase and which can be annotated on the map. These are the steps I’m following,
Step 1:
Emit latitude and longitude from CBLView.
// Open Notifications View from the visible area
visibleAreaNotificationsView = database.viewNamed(CouchBaseConstants.visibleAreaNotifications)
visibleAreaNotificationsView.setMapBlock({ (doc, emit) in
guard let location = payload["Location"] as? [String: Any],
let latitudeString = location["Latitude"] as? String,
let longitudeString = location["Longitude"] as? String,
let latitudeDouble = Double(latitudeString.trimmingCharacters(in: .whitespaces)),
let longitudeDouble = Double(longitudeString.trimmingCharacters(in: .whitespaces))
else { return }
emit([latitudeDouble, longitudeDouble], nil)
}, version: “0.1.3”)
Step 2:
Figure out maps visible area xMin, yMin and xMax, yMax and specifying the query startKey and endKey so that I hope it will filter the DB result within the range of startKey and endKey.
guard let query = database.existingViewNamed(CouchBaseConstants.visibleAreaNotifications)?.createQuery() else { return }
query.prefetch = true
query.startKey = [xMin.latitude, yMin.longitude]
query.endKey = [xMax.longitude, yMax.longitude]
query.runAsync({ (enumeratorResult, error) in
}
But unfortunately filtering is not happening. Here, the expectation is startKey will be the xMin, yMin values and endKey is the xMax, yMax values. Based on this couchbase query will filter the documents within the range of startKey and endKey.
Current observations,
- enumeratorResult is empty
- When I put a comment on the query.startKey and query.endKey, I’m getting the result. But enumeratorResult.nextRow() returns key values as [0,0].
CBLQueryRow[key=[0,0]; value=(null); id=ec-114242454]
. I doubt is it because of the duplicate latitude and longitude value? If so, any solution to resolve this and making the query filter work?
Another approach I tried is using bounding box, instead of startKey and endKey I can give
query.boundingBox = CBLGeoRect(min: CBLGeoPoint(x: xMin, y: yMin), max: CBLGeoPoint(x: xMax, y: yMax))
But what should I emit in the View? I tried to emit using
emit(CBLGeoPoint(x: latitudeDouble, y: longitudeDouble), nil)
But this is throwing an error.
Exception caught in map block of view visibleEcNotifications, on doc {
“_id” = “ec-114315401”;
“_local_seq” = 109;
“_rev” = “1-15dba865c944413b0f72ed596fbcf5e7”;
metadata = {
appVersion = nodejs;
backendState = submitted;
lastEditedTime = “2018-11-05T20:51:43.561Z”;
};
payload = {
AdditionalData = {
IssuedDate = 00000000;
IssuedTo = “”;
“__metadata” = {
type = “ZAI_NOTIFICATION_SRV.AdditionalData”;
};
};
……
……
……
……
Invalid type in JSON write (_SwiftValue)
3 Foundation 0x0000000110b78e50 _writeJSONValue + 706
4 Foundation 0x0000000110b7c7bf ___writeJSONArray_block_invoke + 130
5 CoreFoundation 0x00000001119d6a6a -[__NSSingleObjectArrayI enumerateObjectsWithOptions:usingBlock:] + 58
6 Foundation 0x0000000110b7bf54 _writeJSONArray + 300
7 Foundation 0x0000000110b78b3a -[_NSJSONWriter dataWithRootObject:options:error:] + 124
8 Foundation 0x0000000110b7ae29 +[NSJSONSerialization dataWithJSONObject:options:error:] + 337
9 ConstructQA 0x0000000105d9f604 +[CBLJSON dataWithJSONObject:options:error:] + 335
10 ConstructQA 0x0000000105dcda4a toJSONData + 75
11 ConstructQA 0x0000000105dcd57b -[CBL_SQLiteViewStorage _emitKey:value:valueIsDoc:forSequence:] + 411
12 ConstructQA 0x0000000105dcd170 __39-[CBL_SQLiteViewStorage updateIndexes:]block_invoke.161 + 97
13 ConstructQA 0x0000000105bc1b33 $SyXlyXlSgIeyByy_ypypSgIegnn_TR + 371
14 ConstructQA 0x0000000105bd5d5d $SyXlyXlSgIeyByy_ypypSgIegnn_TRTA + 13
15 ConstructQA 0x0000000105bc385b $S11ConstructQA17PGEConstructStackC5lanID8division3lobACSS_S2StKcfcySDySSypG_yyp_ypSgtctcfU0 + 7419
16 ConstructQA 0x0000000105bc1987 $SSDySSypGypypSgIegnn_Ieggg_So12NSDictionaryCyXlyXlSgIeyByy_IeyByy_TR + 183
17 ConstructQA 0x0000000105dcc935 __39-[CBL_SQLiteViewStorage updateIndexes:]_block_invoke + 6927
Please enlighten me on these issues and help to resolve it.