Sometimes when I try to set a key into an FLDict where the key alreaday exists, I get the following assertion:
FAILED PRECONDITION:
index<_items.size()
not true when calling class fleece::impl::ValueSlot &__cdecl fleece::impl::internal::HeapArray::setting(unsigned int) (at HeapArray.cc line 129)
I can’t get a stack trace, because I’m calling in from Dart and I have no idea how to do it yet, but here’s a C code that reproduces the same error.
The error seems to happens when:
- the key already exists in the dictionary
- AND I try to save the document - (ie. if I don’t call
CBLDatabase_SaveDocument
, I don’t get the error). - AND I haven’t done any “read” operation before hand. This last one is weird, basically if I do
FLDict_Get(props, slice("foo"));
, or do an FLDump on the properties, or even just read a completely different document beforehand (the first two commented lines in my code) then I don’t get the error at all and the document is updated correctly in the database.
If the key doesn’t already exists none of this happens.
void DocTest(CBLDatabase *db) {
// CBLDocument *doc1 = CBLDatabase_GetMutableDocument(db, "testdoc2");
// doc1->properties().get("foo");
CBLError *outError = &CBLError{};
CBLDocument *doc = CBLDatabase_GetMutableDocument(db, "testdoc1");
MutableDict props = CBLDocument_MutableProperties(doc);
// FLDict_Get(props, slice("foo"));
FLSlot sl = FLMutableDict_Set(props, slice("testkey"));
FLSlot_SetInt(sl, 15);
CBLDatabase_SaveDocument(db, doc, {kCBLConcurrencyControlLastWriteWins}, outError);
}
I’m working with a very simple test document here:
{
“_id”: “testdoc1”,
“foo”: “bar”,
“testkey”: 15
}
Apart from doing a dummy read, before every write I don’t know how to work around this.
Edit: the dummy read has to be before setting the new value, not before the save.