IOS: Basic data: how to save an ordered set of objects in a managed object

I have an NSManagedObject in my iOS app. This object is called Round. In the round, I have a lot to do with a bunch of Person objects.

xCode generates my managed object class using NSSet as the data type of the my-to-many relationship with character-driven objects.

So my Round managed object looks like this:

@interface Round : NSManagedObject { } @property (nonatomic, retain) NSSet* people; @end 

However, NSSet is not an ordered collection, and I want to keep the order in NSArray I use to keep these Person objects, as I assign it to my circular managed object.

I tried just converting my NSArray to an NSSet, however the original dialing order is not preserved.

I tried changing the type from NSSet to NSArray in my managed circular object, getting the following error at runtime.

  2011-03-11 14: 00: 06.950 SkeetTracker [42782: 207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ** 'Unacceptable type of value for to-many relationship: property = "people";  desired type = NSSet;  given type = __NSArrayM; ** value = (
     "(entity: Person; id: 0x5bed0c0; data: {\ n firstName = Todd; \ n lastName = McFarlane; \ n round = \" 0x5bf2cb0 \ "; \ n scores = \" \ "; \ n})",

Has anyone ever encountered such a situation and was aware of a solution?

Regards, George

+4
source share
3 answers

I believe that iOS 5 really built it, but regardless of whether you want to have ordered ordered ordering, you need to have a stored attribute from which you can create an NSSortDescription . Then you add another method (I prefer the readonly property so that I can also get it) that returns an array of [NSSet sortedArrayUsingDescriptors:self.unsortedSetMethod] .

+3
source

This is kind of a complicated thing, and I want Apple to build it. But since it is not, you need to use a workaround. Usually I use a transition attribute of type undefined and add an index attribute to the elements in the array. When the data is loaded, you create an array that is an attribute of the transient, using objects sorted by their index. When the context is saved, you go through the array and make sure that each object has the correct index. Alternatively, if there are few changes, you can change the index whenever you change the array.

+2
source

thanks for the great idea. I think that in my case, I can just add the "index" property to the elements of my array (they are also controlled by objects) and make the index a transition property.

I did not understand a bit why you need to create an additional transient attribute of the array.

0
source

Source: https://habr.com/ru/post/1343378/


All Articles