LilMoke
LilMoke

Reputation: 3454

iOS determine when NSMutableArray sortUsingComparator is complete

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?

Upvotes: 0

Views: 941

Answers (2)

Duncan C
Duncan C

Reputation: 131481

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.

Upvotes: 0

Nicola Miotto
Nicola Miotto

Reputation: 3706

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

Upvotes: 3

Related Questions