Search code examples
javaserializationflatbuffers

should I call finish on member tables


So I have table as member of other tables, when I create data of the parent tables, do I need to call finish only on the parent table, or I need to finish both the member and the parent tables? The code is like this:

table Child {
    ...
}

table Parent {
    c: Child;
    ...
}

FlatBufferBuilder fbb = new FlatBufferBuilder(1024);
int child = Child.createChild(fbb, ...);
fbb.finish(child); // <-- Do I need to call this?

int parent = Parent.createParent(fbb, child, ...);
Parent.finishParentBuffer(fbb, parent); // <-- finishes the whole buffer for writing
// write(fbb.dataBuffer());

The question is do I need to call finish on the child table?


Solution

  • You have to finish each table before building a parent, or any other table. Finishing gives you the offset value that is then provided to other tables to store.

    Using the create<TABLE_NAME>() methods automatically finishes for you.

    Your example code is wrong, it would be like:

    int child_offset = Child.createChild(fbb, ...);
    int parent_offset = Parent.createParent(fbb, child_offset, ...);
    

    If you use the Child.startChild() method you then have to manually call Child.endChild(). Take a look at the generated code of Child.createChild()