NSOrderedSet response from server and master data

I want to show the data coming from the backend. So let's get an example json file:

{ "fonts": [ { "name": "Helvetica", "styleIdentifier": "H0", "size": 17 }, { "name": "Helvetica", "styleIdentifier": "H1", "size": 14 }, { "name": "Helvetica-Bold", "styleIdentifier": "H0Bold", "size": 17 }, { "name": "HelveticaNeue-Light", "styleIdentifier": "H0Light", "size": 40 } ] } 

So, I create a relationship parameter (many - many) with ordered . And as I type, I see that it always writes the same to Core Data, but when I try to extract it

configuratation.fonts , where fonts is an NSOrderedSet , I get the elements in completely random order. Am I missing the spec? Or should I sort it out somehow?

__ __ EDIT

Firstly, when I get the data above json, I have a configuration set with an empty font ratio. Then I get this and paste it into the main data with:

 NSMutableArray *returnArray = [NSMutableArray new]; for(NSDictionary *fontDictionary in jsonArray) { Font *fontObj = [Font font:fontDictionary inContext:context]; [returnArray addObject:fontObj]; } 

And in this array, the data is in the correct order. Then in the configuration object, I add it to the NSOrderedSet by:

 -(void)appendTracks:(NSArray<Font*>*)fontArray { self.fonts = [NSOrderedSet orderedSetWithArray: fontArray]; } 

And then I try to extract it just using the link:

 configuration.fonts 

And at this point, the data is completely out of order.

+6
source share
2 answers

Do not set the NSOrderedSet directly.

Or modify the existing set:

 [self.fonts addObjectsFromArray:fontArray]; 

Or:

Xcode generates methods for adding and removing records from ordered sets in the generated NSManagedObject class.

Assuming you have an Entity called ManagedConfiguration that contains many-ordered, called fonts :

 ManagedConfiguration *managedObjectConfigurationInstance = //create or fetch configuration in ManagedObjectContext NSOrderedSet<ManagedFont> *fonts = //created or fetched fonts in wanted order managedObjectConfigurationInstance.addToFonts(fonts) 

replaceFonts , removeFromFontsAtIndex aso. methods are also generated.


Depending on your requirements, you may want to keep the fonts in random order and apply NSSortDescriptor to your NSFetchRequest to retrieve data in a specific order.

+2
source

Instead of trying to set the data directly to your property ( fonts ), you need to first extract a modified copy of your NSOrderedSet from a subclass of NSmanagedObject (I assume it will be Font ).

 NSMutableOrderedSet *orderedSet = [self mutableOrderedSetValueForKey:@"fonts"]; 

Then add the objects from the array to this ordered set.

 [orderedSet addObjectsFromArray:array]; 

You should now correctly set the values ​​for the fonts key. So now your appendTracks function will look like this.

 -(void)appendTracks:(NSArray<Font*>*)fontArray { NSMutableOrderedSet *orderedSet = [self mutableOrderedSetValueForKey:@"fonts"]; [orderedSet addObjectsFromArray:fontArray]; } 

Now execute the query for the selection. You should get the data in the given order in the array.

PS: I used your JSON answer to check this.

+1
source

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


All Articles