How to approach parallelising job when filtering with WithSharedComponentFilter?

I have an entities.foreach that does some processing and then modifies the rotation of some entities, these need to be grouped with GetAllUniqueSharedComponentData so I use WithSharedComponentFilter on the ForEach job. What I’ve come to realise is, when I schedule these jobs in a for loop, I have it so that each job is dependent on the previous one even though each job processes different entities which leads to poor performance.

Is there a way I can parallelise this without running into the “InvalidOperationException: The previously scheduled job writes to the ComponentTypeHandle…” Error? I’ve tried having an overall nativearray that they can all write to, and then applying these changes at the end however that caused similar problems with parallel nativearray writing and added another step.

Any help is appreciated, thanks.

Edit: Just discovered the nativestream which might be useful for this, still want to try and keep the logic contained to the processing job though if possible.

You may want to switch to an IJobEntityBatch for this. You can look up the shared component index in each batch and compare that to the shared components in question.