Add Kotlin serialization-based APIs#475
Conversation
- Collection.getDocumentAs(), save() - Result.data() - ResultSet.data()
|
I just noticed that Swift has model-based |
|
|
||
| /** Saves a [DocumentModel] instance as a document in the collection, with a specified conflict handler. | ||
| * If the model's [DocumentModel.documentMeta] property is null, it will be saved as a new document with the | ||
| * given [docID], which must not be null. |
There was a problem hiding this comment.
In Swift, we also support id == nil as an auto generated id for a newly created doc. Can we allow that here as well?
| val doc: MutableDocument | ||
| if (meta == null) { | ||
| require(docID != null) { "docID argument must be given when saving a new document" } | ||
| doc = MutableDocument(docID) |
There was a problem hiding this comment.
Can we also allow null docID here (an auto id (uuid) will be generated)?
| private fun <T:DocumentModel> MutableDocument.setContentFromModel(model: T, serializer: SerializationStrategy<T>) { | ||
| val body = serializeToFleece(serializer, model) | ||
| val root = FLValue.fromData(body).asFLDict() | ||
| setContent(root, false) |
There was a problem hiding this comment.
Does the body need to be retained as well?
I read iOS code, and iOS is doing something similar. If I read the code correctly, Swift's DocumentEncoder calls FLEncoder_FinishDoc that returns an FLDoc, get the dict from the FLDoc, set the dict to the CBL doc, and abandon the FLDoc. So maybe the data is retained by the dict?
- (BOOL)finishIntoDocument:(CBLDocument*)document error:(NSError**)outError {
FLError error {};
FLDoc fldoc = FLEncoder_FinishDoc(_encoder, &error);
if (!fldoc) {
return convertError(error, outError);
}
Doc doc { fldoc };
Dict fleeceData = doc.asDict();
if (!fleeceData) {
return NO;
}
[document setFleece: (FLDict)fleeceData];
return YES;
}
There was a problem hiding this comment.
That Obj-C code above leaks the FLDoc, unfortunately. (The Doc constructor retains the FLDoc, then releases it when the function returns. But nothing releases the +1 ref returned by FLEncoder_FinishDoc.
That leak is the only reason it doesn't crash, I think, unless the setFleece: method is doing something clever like explicitly retaining the FLDict.
There was a problem hiding this comment.
I confess I can't figure out how the memory management of Fleece objects works in the Java code. Do you or Jim have any better knowledge of it?
Implements Kotlin extension methods that use serialization to allow developers to work with documents and query rows as native Kotlin classes. (Equivalent to the Swift API extensions implemented last year.)
Query Results & ResultSets
Documents
Document model classes must implement a new interface
DocumentModelthat defines adocumentMetaproperty that stores the document id and revID, which is needed in order to save the document.Documents can be loaded and saved as model instances:
Lower-Level
Both of these use some new internal functions that serialize Kotlin classes to and from Fleece.