Advertisement
RRK

DisplayOrder

RRK
May 15th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This function adds a new Entity
  2.  
  3. - (void)insertNewObject
  4. {
  5.     Test *test = [NSEntityDescription insertNewObjectForEntityForName:@"Test" inManagedObjectContext:self.managedObjectContext];
  6.  
  7.  NSManagedObject *lastObject = [self.controller.fetchedObjects lastObject];
  8.  
  9. float lastObjectDisplayOrder = [[lastObject valueForKey:@"displayOrder"] floatValue];
  10.  
  11. [test setValue:[NSNumber numberWithDouble:lastObjectDisplayOrder + 1.0] forKey:@"displayOrder"];
  12.  
  13. }
  14.  
  15. - (NSFetchedResultsController *)controller
  16. {
  17.     if (m_controller != nil)
  18.     {
  19.         return m_controller;
  20.     }
  21.    
  22.     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  23.    
  24.    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TEST" inManagedObjectContext:self.managedObjectContext];
  25.     [fetchRequest setEntity:entity];
  26.    
  27.      [fetchRequest setFetchBatchSize:20];
  28.  
  29.     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES];    
  30.  
  31.     NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];  
  32.      
  33.     [fetchRequest setSortDescriptors:sortDescriptors];
  34.    
  35.    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
  36.     aFetchedResultsController.delegate = self;
  37.     self.controller = aFetchedResultsController;
  38.    
  39.     NSError *error = nil;
  40.     if (![self.controller performFetch:&error]) {
  41.         // Replace this implementation with code to handle the error appropriately.
  42.         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  43.         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  44.         abort();
  45.     }
  46.    
  47.     return  m_controller;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement