Created
August 23, 2016 06:08
-
-
Save skagedal/5fafe1daa35177536dabbe4415d6274c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example code for https://openradar.appspot.com/radar?id=4975264899530752 since I can't attach | |
// a zip there and am to lazy to create a git repo for the whole thing. | |
// Create a standard iOS "Single View" application with Core Data in Xcode 7.3.1 | |
// Create one Core Data entity called Thing with an integer "order" and a string "name" | |
// Hook up a simple view controller to these actions | |
#import "AppDelegate.h" | |
#import "ViewController.h" | |
#import "Thing.h" | |
@interface ViewController () <NSFetchedResultsControllerDelegate> | |
@property (strong, nonatomic) NSFetchedResultsController *controller; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.controller = [self createFetchedResultsController]; | |
self.controller.delegate = self; | |
[self.controller performFetch:NULL]; | |
} | |
- (IBAction)createThings:(id)sender { | |
NSManagedObjectContext *context = [self context]; | |
Thing *th1 = [NSEntityDescription insertNewObjectForEntityForName:@"Thing" inManagedObjectContext:context]; | |
th1.name = @"Hello"; | |
th1.order = @(1); | |
Thing *th2 = [NSEntityDescription insertNewObjectForEntityForName:@"Thing" inManagedObjectContext:context]; | |
th2.name = @"World"; | |
th2.order = @(2); | |
[[self appDelegate] saveContext]; | |
} | |
- (IBAction)deleteThings:(id)sender { | |
NSManagedObjectContext *context = [self context]; | |
NSArray <Thing *> *things = [self allThings]; | |
for (Thing *thing in things) { | |
NSLog(@"Deleting thing %@", thing.name); | |
[context deleteObject:thing]; | |
} | |
[[self appDelegate] saveContext]; | |
} | |
- (IBAction)toggleOrder:(id)sender { | |
NSArray <Thing *> *things = [self allThings]; | |
for (Thing *thing in things) { | |
if (thing.order.integerValue == 1) { | |
thing.order = @(2); | |
} else { | |
thing.order = @(1); | |
} | |
} | |
[[self appDelegate] saveContext]; | |
} | |
- (NSArray <Thing *> *)allThings { | |
NSManagedObjectContext *context = [self context]; | |
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Thing"]; | |
NSError *error; | |
NSArray <Thing *> *things = [context executeFetchRequest:request error:&error]; | |
if (!things) { | |
NSLog(@"ERROR: %@", error); | |
} | |
return things; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (NSManagedObjectContext *)context { | |
return [self appDelegate].managedObjectContext; | |
} | |
- (AppDelegate *)appDelegate { | |
return (AppDelegate *)[UIApplication sharedApplication].delegate; | |
} | |
- (NSFetchedResultsController *)createFetchedResultsController { | |
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Thing"]; | |
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES]]; | |
NSFetchedResultsController * fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[self context] sectionNameKeyPath:nil cacheName:nil]; | |
return fetchController; | |
} | |
#pragma mark - NSFetchedResultsControllerDelegate | |
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { | |
NSLog(@"%@", NSStringFromSelector(_cmd)); | |
} | |
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { | |
switch (type) { | |
case NSFetchedResultsChangeInsert: | |
NSLog(@"INSERT at %ld", newIndexPath.row); | |
break; | |
case NSFetchedResultsChangeDelete: | |
NSLog(@"DELETE at %ld", indexPath.row); | |
break; | |
case NSFetchedResultsChangeUpdate: | |
NSLog(@"UPDATE at %ld", indexPath.row); | |
break; | |
case NSFetchedResultsChangeMove: | |
NSLog(@"MOVE from %ld to %ld", indexPath.row, newIndexPath.row); | |
break; | |
} | |
} | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { | |
NSLog(@"%@", NSStringFromSelector(_cmd)); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment