0

I am sorting a mutable array as follows:

[m_maFoundObjects sortUsingComparator:
 ^NSComparisonResult(id obj1, id obj2){
     FADObject * o1 = (FADObject *)obj1;
     FADObject * o2 = (FADObject *)obj2;
     return [o1.name compare:o2.name];
 }
 ];

Can someone tell me the best way to determine when the sort is done so I can call reloadData on my list?

Wain
  • 118,658
  • 15
  • 128
  • 151
LilMoke
  • 3,176
  • 7
  • 48
  • 88
  • You can use 2 methods for to check for to execute your reload data after to complete the first method There are a lot of examples in stackoverflow like [this](http://stackoverflow.com/a/20779096/921789) or [this](http://stackoverflow.com/a/16472235/921789) – Fran Martin Mar 07 '14 at 13:16

2 Answers2

3

The sorting is done synchronously, so you should be able to reload in the next statement.

[m_maFoundObjects sortUsingComparator:
 ^NSComparisonResult(id obj1, id obj2){
     FADObject * o1 = (FADObject *)obj1;
     FADObject * o2 = (FADObject *)obj2;
     return [o1.name compare:o2.name];
 }
 ];

// Here you can reload your data
Nicola Miotto
  • 3,647
  • 2
  • 29
  • 43
  • Hmm, actually that is what I thought as I have sorted this way before. However, in this case it was not working. I must have other issues in the code. Thanks for the clarification/sanity check!!!! – LilMoke Mar 07 '14 at 13:09
  • Yes, it was my fault… too early, not enough coffee… turn out I was sorting the wrong list. Thanks for the help, sometimes you just need someone to explain it to. – LilMoke Mar 07 '14 at 13:14
  • 1
    No problem, I understand the coffee issue ;) If you have more issues within the scope of this question, let me know and I will help. Otherwise remember to accept the answer as correct – Nicola Miotto Mar 07 '14 at 13:16
0

Interestingly, all the array sorting methods operate synchronously, even sortWithOptions:usingComparator: with an options value of NSSortConcurrent.

The docs are silent on this point, but I just tested and confirmed. I wrote a test comparator block that logged the time and thread id, and logged the time before and after the sort. With a large enough array I as able to see the comparator block firing concurrently from different threads, but the sort method did not return until the sort process was complete.

I just posted the following documentation comment:

The documentation for the method sortWithOptions:usingComparator: is incomplete.

This method always operates synchronously, even when you specify an opts value of NSSortConcurrent.

Put in plain english: 'This method does not return until the array is sorted, even if you specify sort options of NSSortConcurrent". In that case the comparator blocks may be invoked on background threads, but the method waits until the sort is complete before returning control to the caller.

Let's all send in feedback on this issue, since Apple is more likely to fix something if they receive multiple reports about it.

Duncan C
  • 128,072
  • 22
  • 173
  • 272