Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coredata and NSOrderedSet - how is the order of the set defined?

I had a look through the existing documentation and couldn't find anything that confirms the behaviour I am seeing.

Starting on iOS 5, CoreData relationships can be ordered and in this case the returned object is a NSOrderedSet.

Can anyone confirm that the order of the objects in the set matches the order in which they have been created?

Cheers, Rog

like image 776
Rog Avatar asked Feb 29 '12 10:02

Rog


2 Answers

If you don't sort the order yourself using sortedArrayUsingComparator: then I believe the objects are sorted in the order in which they are added.

You can also manual set the order with with – insertObject:atIndex: .

https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSMutableOrderedSet_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableOrderedSet

like image 77
elprl Avatar answered Sep 22 '22 12:09

elprl


You should create a NSMutabledOrderedSet with data from your old NSOrderedSet, make your changes and move your data back again:

NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithSet:[self.myOrderedSet set]];
[orderedSet insertObject:newObject atIndex:index];
self.myOrderedSet = orderedSet;
like image 25
monavari-lebrecht Avatar answered Sep 22 '22 12:09

monavari-lebrecht