How can one lock "edit" for a document being loaded?

I have some workaround:
I use a Job that listens for a OnLoadFinished event and then executes:

css::uno::Any TestJob::execute(const css::uno::Sequence<css::beans::NamedValue> &arguments) {
  //...
  auto args = model->getArgs();
  auto urlFound = std::find_if(std::cbegin(args), std::cend(args), [](auto &&item) { return item.Name == "URL"; });
  if (urlFound == std::cend(args))
    return {};

  rtl::OUString url;
  urlFound->Value >>= url;

  css::uno::Sequence<css::beans::PropertyValue> newArgs(args.getLength() + 3);
  std::copy(std::cbegin(args), std::cend(args), std::begin(newArgs));

  newArgs[args.getLength() + 0] = {"ReadOnly", 0, css::uno::Any(true), static_cast<const css::beans::PropertyState>(0)};
  newArgs[args.getLength() + 1] = {"LockEditDoc", 0, css::uno::Any(true),
                                   static_cast<const css::beans::PropertyState>(0)};
  newArgs[args.getLength() + 2] = {"MacroExecutionMode", 0, css::uno::Any(css::document::MacroExecMode::NEVER_EXECUTE),
                                   static_cast<const css::beans::PropertyState>(0)};
  model->attachResource(url, newArgs);
  //...
}

Actually it works, but not stable. Sometimes on Windows 10 I get an exception in ntdll.dll while model->attachResource() is executed (I can see it from call stack).

Maybe there is some proper solution?

This solution works fine! Iterator invalidation caused the problem on Windows 10.