diff --git a/iOS/MonsterCards/Views/EditMonsterViewController.m b/iOS/MonsterCards/Views/EditMonsterViewController.m index 9ae5a5d..fa6f4ad 100644 --- a/iOS/MonsterCards/Views/EditMonsterViewController.m +++ b/iOS/MonsterCards/Views/EditMonsterViewController.m @@ -8,6 +8,7 @@ #import "EditMonsterViewController.h" #import "EditableShortStringTableViewCell.h" +#import "AppDelegate.h" @interface EditMonsterViewController () @@ -15,10 +16,15 @@ @end -@implementation EditMonsterViewController +@implementation EditMonsterViewController { + NSManagedObjectContext *_context; +} - (void)viewDidLoad { [super viewDidLoad]; + AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; + _context = appDelegate.persistentContainer.viewContext; + self.monsterTableView.dataSource = self; self.monsterTableView.delegate = self; } @@ -32,9 +38,12 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([@"DiscardChanges" isEqualToString:segue.identifier]) { + [_context rollback]; } else if ([@"SaveChanges" isEqualToString:segue.identifier]) { // TODO: this should use a method on originalMonster to copy values from editingMonster or pass the new monster back some way. Core Data would save and probably trigger a refresh in the receiving view. self.originalMonster.name = self.editingMonster.name; + [_context refreshObject:self.editingMonster mergeChanges:NO]; + [_context save:nil]; } else { NSLog(@"Unknown Segue %@", segue.identifier); } diff --git a/iOS/MonsterCards/Views/LibraryViewController.m b/iOS/MonsterCards/Views/LibraryViewController.m index 9406d5f..e3b28fa 100644 --- a/iOS/MonsterCards/Views/LibraryViewController.m +++ b/iOS/MonsterCards/Views/LibraryViewController.m @@ -17,36 +17,32 @@ @end -@implementation LibraryViewController +@implementation LibraryViewController { + NSManagedObjectContext *_context; +} - (void)viewDidLoad { [super viewDidLoad]; AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; - NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext; - - // Temporary setup of allMonsters until we bind to CoreData. - Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}" andContext:context]; - Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}" andContext:context]; - self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil]; + _context = appDelegate.persistentContainer.viewContext; } - (void)viewWillAppear:(BOOL)animated { - // TODO: fetch monsters from CoreData + self.allMonsters = [_context executeFetchRequest:[Monster fetchRequest] error:nil]; [self.monstersTable reloadData]; } - (IBAction)addNewMonster:(id)sender { - AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; - NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext; - Monster *monster = [[Monster alloc] initWithContext:context]; + Monster *monster = [[Monster alloc] initWithContext:_context]; monster.name = @"Unnamed Monster"; self.allMonsters = [self.allMonsters arrayByAddingObject:monster]; + //DispatchQueue.main.async{"code here"} + [_context save:nil]; [self.monstersTable reloadData]; } #pragma mark - Navigation -// In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([@"ShowMonsterDetail" isEqualToString:segue.identifier]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; diff --git a/iOS/MonsterCards/Views/SearchViewController.m b/iOS/MonsterCards/Views/SearchViewController.m index 9df021c..5e61f9e 100644 --- a/iOS/MonsterCards/Views/SearchViewController.m +++ b/iOS/MonsterCards/Views/SearchViewController.m @@ -18,17 +18,18 @@ @end -@implementation SearchViewController +@implementation SearchViewController { + NSManagedObjectContext *_context; +} - (void)viewDidLoad { [super viewDidLoad]; AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; - NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext; - Monster *pixie = [[Monster alloc] initWithContext:context]; - pixie.name = @"Pixie"; - Monster *acolyte = [[Monster alloc] initWithEntity:[NSEntityDescription entityForName:@"Monster" inManagedObjectContext:context] insertIntoManagedObjectContext:nil]; - acolyte.name = @"Acolyte"; - self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil]; + _context = appDelegate.persistentContainer.viewContext; +} + +- (void)viewWillAppear:(BOOL)animated { + self.allMonsters = [_context executeFetchRequest:[Monster fetchRequest] error:nil]; self.foundMonsters= self.allMonsters; }