Although looking at the code, I don't see much that would cause it to fail.  That's the first step to debugging it.  Post the value of .systemErrorCode() after a return of 0.

.systemErrorCode returns 0. I interpreted the doc so that .systemErrCode is only set when a syntax error occurs, which apparently is wrong.

Anyhow, I think I've cracked it. The code is in a unguarded method and there seemed to be an interference from another thread. I changed things around a bit and now it's working :)