Sorting lists with a JobHandle

Consider these jobs

            Dependency = populateForceDisable.Schedule(PreviouslyVisibleTiles, 16, Dependency);
            Dependency = toggleLightJob.Schedule(VisibleTiles, 16, Dependency);
            Dependency = ToggleData.Sort(new KeySorter(), Dependency);
            Dependency = assignEnabledDisabled.Schedule(Dependency);

The issue is that toggleLightJob writes to ToggleData which is then sorted before being consumed by assignEnabledDisabled
The dependency chain is correct, so why am I getting InvalidOperationException: The previously scheduled job LightProcessorSystem:ToggleLightJob writes to the Unity.Collections.NativeList`1[LightProcessorSystem+ToggleEntry] ToggleLightJob.ToggleData. You must call JobHandle.Complete() on the job LightProcessorSystem:ToggleLightJob, before you can write to the Unity.Collections.NativeList`1[LightProcessorSystem+ToggleEntry] safely.?

Did I interpret something wrong? Do Sort simply does not work like that despite taking a JobHandle?
I am on version Burst 1.5.4, Collections 0.15.0-preview.21 and Entities Version 0.17.0-preview.42

Because Sort requires a NativeArray, so you’re converting your NativeList into a NativeArray before it’s finished potentially adding elements.

You can just create your own IJob and pass in the List and call Sort inside that

2 Likes

That worked!