How to trigger file sync when editing from PHP API?

I’m editing a file as described here.

https://docs.nextcloud.com/server/latest/developer_manual/basics/storage/filesystem.html#writing-to-a-file

Except I’m using File::fopen and then fwrite instead of putContent because I need to append to the file and apparently putContent overwrites the whole file. Here’s the code.

    $s = 'Stuff I am appending.';
    $f = $this->rootFolder->get('somefile.txt')->fopen('a');
    if (fwrite($f, $s) != strlen($s)) {
        throw new \Exception("Failed to write file");
    }

It works fine except it doesn’t seem to trigger desktop syncing. I can see the edits on the server but they don’t show up on my desktop.

On the other hand, if I edit the same file using the built-in Files app it synchronizes immediately and I see the changes on my desktop right away.

Is there something else I need to do in the code to make it sync?

File::touch seems to do the trick. Is that necessary? Is there a better way that this?

    $s = 'Stuff I am appending.';
    $file = $this->rootFolder->get('somefile.txt');
    $f = $file->fopen('a');
    if (fwrite($f, $s) != strlen($s)) {
        throw new \Exception("Failed to write file");
    }
    fclose($f);
    $file->touch();