How to use loops in Job System

Hello, I want to calculate the distance between the list of game objects So element will check its distance with every other element and so on, I am doing this with following code it works but not efficient enough. Can anyone tell me the correct way to use loops in job system.

struct Field: IJobParallelForTransform
    {
        public NativeArray<float> Dist;
        public Vector3 PlayerPos;

        public void Execute(int i, TransformAccess transform)
        {

              Dist[i] = Vector3.Distance(PlayerPos, transform.position);

           
        }
    }
    public List<Transform> Childrens;//All gameobjects list
 
    public TransformAccessArray ALlObjects;
 

 
    NativeArray<float> distances;
 

 
    void Start ()
 
    {
 

 
        ALlObjects = new TransformAccessArray(0, -1);
 
       
 
        distances = new NativeArray<float>(Childrens.Count, Allocator.Persistent);
 

 
    }
 

 
       void Update()
 
    {
 
        for (int i = 0; i < Childrens.Count; i++)
 
        {
 
            Field job1 = new Field()
 
            {
 
                Dist = distances,
 
                PlayerPos= Childrens[i].position,
 

 
            };
 
            JobHandle jH = job1.Schedule(ALlObjects);
 
            jH.Complete();
 
            for (int j = 0; j < distances.Length; j++)
 
            {
 

 
                if (distances[j] <= 0.8f && distances[j] > 0)
 
                {
 
                 
 
                    break;
 
                }
 
                if (distances[j] > 0.8f )
 
                {
 
           
 
                }
 

 
            }
 

 
        }
 
    }

You are creating a parallelFor job for each gameobject, that’s for sure not how you want to do this. I’m confused as I already posted a nice and clean solution in an earlier post from you:

Also, consider what you are doing when you are calling Job.Complete(). Essentially, you are forcing the main thread to wait for your job to finish, which kind of destroys the entire purpose of what you are doing here. Make sure that you call Schedule() at the beginning of your game logic updates, and then call Complete() at the end of your update loops. Otherwise, call Complete() after some time has passed to be clever.

Hello, In last thread I was checking distance of player against all other objects but here I am checking every objects distance against eachother. I thought rather than calling job from every single object I can use loop and check from main parent object

Thanks, I changed the code and it helps performance