Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This function adds a new Entity
- - (void)insertNewObject
- {
- Test *test = [NSEntityDescription insertNewObjectForEntityForName:@"Test" inManagedObjectContext:self.managedObjectContext];
- NSManagedObject *lastObject = [self.controller.fetchedObjects lastObject];
- float lastObjectDisplayOrder = [[lastObject valueForKey:@"displayOrder"] floatValue];
- [test setValue:[NSNumber numberWithDouble:lastObjectDisplayOrder + 1.0] forKey:@"displayOrder"];
- }
- - (NSFetchedResultsController *)controller
- {
- if (m_controller != nil)
- {
- return m_controller;
- }
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
- NSEntityDescription *entity = [NSEntityDescription entityForName:@"TEST" inManagedObjectContext:self.managedObjectContext];
- [fetchRequest setEntity:entity];
- [fetchRequest setFetchBatchSize:20];
- NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES];
- NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
- [fetchRequest setSortDescriptors:sortDescriptors];
- NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
- aFetchedResultsController.delegate = self;
- self.controller = aFetchedResultsController;
- NSError *error = nil;
- if (![self.controller performFetch:&error]) {
- // Replace this implementation with code to handle the error appropriately.
- // 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.
- NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
- abort();
- }
- return m_controller;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement