Advertisement
Guest User

Indexing Core Data Managed Objects

a guest
Mar 20th, 2010
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (NSMutableArray *) orderedComponents{
  2.     NSSortDescriptor *orderSort=[[NSSortDescriptor alloc] initWithKey:[ComponentToLabelMO order_Key]
  3.                                                             ascending:YES];
  4.     //------------------------------------------------------^
  5.     NSArray *sorted=[[[self components] allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:orderSort]];
  6.     self.orderedComponents=[[NSMutableArray alloc] initWithArray:sorted]; //use self.property= to handle retention
  7.     return orderedComponents;
  8. }
  9.  
  10. - (ComponentToLabelMO *) componentAtIndex:(NSUInteger) index{
  11.     return [self.orderedComponents objectAtIndex:index];
  12. }
  13.  
  14. // I use the NSMutableArray methods to manage the order for me.
  15. -(void) updateComponentOrder{
  16.     NSUInteger i, count = [self.orderedComponents count];
  17.     for (i = 0; i < count; i++) {
  18.         ComponentToLabelMO* component = [self.orderedComponents objectAtIndex:i];
  19.         component.order=[NSNumber numberWithUnsignedInt:i];
  20.     }
  21. }
  22.  
  23. // creates new linking object and assigns it's order to the end of the label
  24. - (void) addComponentToLabelWithComponent:(LabelComponentMO *) aLabelComponent{
  25.     ComponentToLabelMO *ctlMO=[NSEntityDescription insertNewObjectForEntityForName:[ComponentToLabelMO entityName]
  26.                                                           inManagedObjectContext:self.managedObjectContext];
  27.         //------------------------------------------------^
  28.     ctlMO.label=self; //this will add the ctl automatically to self.components
  29.     ctlMO.component=aLabelComponent;
  30.     ctlMO.order=[NSNumber numberWithInt:[self.orderedComponents count]]; //the order is zero indexed so first labels order is zero, second is one etc
  31.    
  32. }
  33.  
  34. //Used to add a component to the end of the index i.e. when order is not known or needed
  35. - (void) addComponent:(LabelComponentMO *) aLabelComponent;{
  36.     if (aLabelComponent!=nil) {
  37.         [self addComponentToLabelWithComponent:aLabelComponent]; //automatically add at end of order       
  38.         [self updateComponentOrder];
  39.     }
  40. }
  41.  
  42. // Used to add a component to a specific index in the lable
  43. - (void) addComponent:(LabelComponentMO *) aLabelComponent AtIndex:(NSUInteger) index{
  44.     if (aLabelComponent!=nil) {
  45.         [self addComponentToLabelWithComponent:aLabelComponent];
  46.         [self.orderedComponents exchangeObjectAtIndex:[self.orderedComponents count] withObjectAtIndex:index];
  47.         [self updateComponentOrder];
  48.     }
  49. }
  50.  
  51.  
  52. - (void) swapComponentAtIndex:(NSUInteger) firstIndex withComponentAtIndex:(NSUInteger) otherIndex{
  53.     NSUInteger comCount=[self.orderedComponents count];
  54.     if ( (firstIndex<=comCount) && (otherIndex<=comCount) ){ // prevent range exception
  55.         [self.orderedComponents exchangeObjectAtIndex:firstIndex withObjectAtIndex:otherIndex];
  56.         [self updateComponentOrder];
  57.     }
  58.  
  59. }
  60.  
  61. - (void) removeComponentAtIndex:(NSUInteger) index{
  62.     if (index<=[self.orderedComponents count]) {
  63.         ComponentToLabelMO *removedLabel=[self.orderedComponents objectAtIndex:index];
  64.         [self removeComponentsObject:removedLabel];
  65.         [self updateComponentOrder];
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement