First of all, I'm a beginner, and I'm going to make a few assumptions about what causes my problem here, which may seem really stupid, so please tell me.
I am trying to skip an array of String objects containing the dates of the month of October 2016, which means 31 String objects: October 1, 2016 ... October 31, 2016. For each object I want to get some data from the database and add a return value (also a String object ) into a new array. However, the tricky part is that the new array of String objects must be in the same order as the date array. So, for example, the 5th object in the new array should be the returned value of the 5th object in my date array (October 5, 2016), and the 14th object in the new array should be the returned value of the 14th object in my array dates (October 14, 2016), etc. Etc. Now itβs obvious that the database search process takes place in the background thread, and assuming that the system wants the entire process to be executed as quickly as possible, it simultaneously launches several search tasks (all in its stream). The problem is that it really messed up the order in which the new array is built. Honestly, this looks very confusing, and the strange part is that the order of the array is not random (which would probably confirm my assumption): the order of the new array is basically the first 8 returned values ββin the correct order and then the first 8 from the 9th value values ββare repeated, so it seems to be:
1 October -> 5 2 October -> 8 3 October -> 4 4 October -> 11 5 October -> 9 6 October -> 7 7 October -> 6 8 October -> 14 9 October -> 5 10 October -> 8 11 October -> 4 12 October -> 11 13 October -> 9 14 October -> 7 15 October -> 6 16 October -> 14 17 October -> 5 18 October -> 8 19 October -> 4 20 October -> 11 21 October -> 9 22 October -> 7 23 October -> 6 24 October -> 14 25 October -> 5 26 October -> 8 27 October -> 4 28 October -> 11 29 October -> 9 30 October -> 7 31 October -> 6
So, as you can see in this pattern, it just extracts 8 different values, and then repeats again until the array is full. If I started the entire cycle of the cycle in a very short time, the order usually remains the same, except that the first value in the array is no longer the same (so basically every value moves to 1 date). In any case, to move on to the pursuit: I assume that completing each search task one at a time will fix my problem. This is the loop that I am currently executing:
// Loop through each date for date in self.datesToDisplay { // Fire off retrieval method with date object as its only parameter self.getMessagesForDate(date) }
My model will add the return values ββto the new array and pass the newly created array back to the caller, for example:
// Delegate method which gets called whenever retrieval is finished func messagesRetrieved() { // Pass newly created array back to caller self.messagesForDatesToDisplay = self.retrieveModel.messages }
The above code is a slightly simplified version of the actual code that I run in my project, but you get the idea. First question: am I somewhere close to the right direction with my assumption of what might cause this problem? The second question is about the next steps: if I am right, how can I be sure that the second search process does not start until the first is COMPLETED (so after the delegate method to return the value was called and started)?