核心数据不保存和显示数据

时间:2014-11-07 09:05:42

标签: ios objective-c xcode core-data xcode6

我从Dennis Roberson那里学习了核心数据(https://www.youtube.com/watch?v=ZoTAKAjEIFM)。一切都很好,我已经正确设置了我的模型等但它似乎没有保存,即使NSLog in - (void)saveAndDismiss方法也没有显示在控制台中?有没有人遇到这个或者可能能帮到我?

My CoreViewController.m

#import "CoreViewController.h"
#import "AppDelegate.h"

@interface CoreViewController ()

@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext;

@end

@implementation CoreViewController

-(NSManagedObjectContext*)managedObjectContext{

    return [(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
}

-(void)cancelAndDismiss{

    [self.managedObjectContext rollback];
    [self.navigationController popViewControllerAnimated:YES];

}

-(void)saveAndDismiss{

    NSError *error = nil;
    if ([self.managedObjectContext hasChanges]) {
        if (![self.managedObjectContext save:&error]) {
            NSLog(@"Save Failed: %@",[error localizedDescription]);
        }   else {
            NSLog(@"Save Succeeded");
        }
    }

    [self.navigationController popViewControllerAnimated:YES];

}

@end

my personTableViewController.h

#import <UIKit/UIKit.h>
#import "Person.h"

@interface personTableViewController : UITableViewController <NSFetchedResultsControllerDelegate>

@property (nonatomic,strong)Person *person;

@end

我的PersonTableViewController.m

#import "personTableViewController.h"
#import "AppDelegate.h"
#import "personViewController.h"

@interface personTableViewController ()

@property (nonatomic,strong)NSManagedObjectContext *managedObjectContext;
@property (nonatomic,strong)NSFetchedResultsController *fetchedResultsController;


@end

@implementation personTableViewController

-(NSManagedObjectContext*)managedObjectContext{

    return [(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error = nil;

    if (![[self fetchedResultsController]performFetch:&error]) {
        NSLog(@"Error! %@", error);
        abort();
    }



}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [[self.fetchedResultsController sections]count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  //  static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...

    Person *person = [self.fetchedResultsController objectAtIndexPath:indexPath];

    cell.textLabel.text = person.personName;

    [self.tableView reloadData];


    return cell;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([[segue identifier]isEqualToString:@"addPerson"]) {

        UINavigationController *navigationController = segue.destinationViewController;

        personViewController *addPersonViewController = (personViewController*)
        navigationController.topViewController;

        Person *addPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[self managedObjectContext]];

        addPersonViewController.addPerson = addPerson;       
    }
}

#pragma mark - Fetched Results Controller Section

-(NSFetchedResultsController*)fetchedResultsController{

    if (_fetchedResultsController !=nil) {
        return _fetchedResultsController;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];

    NSManagedObjectContext *context = [self managedObjectContext];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"personName" ascending:YES];

    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];

    fetchRequest.sortDescriptors = sortDescriptors;

    _fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];

    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}

#pragma mark - Fetched Results Controller Delegates


-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller{

    [self.tableView beginUpdates];
}

-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{

    [self.tableView endUpdates];
}

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{

    UITableView *tableView = self.tableView;

    switch (type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate: {
            Person *changePerson = [self.fetchedResultsController objectAtIndexPath:indexPath];
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.textLabel.text = changePerson.personName;
        }
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath,nil] withRowAnimation:UITableViewRowAnimationFade];

            break;
    }

}

-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch (type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete   :
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeMove:
            NSLog(@"A table item was moved");
            break;
        case NSFetchedResultsChangeUpdate:
            NSLog(@"A table item was updated");
            break;
    }
}

@end

在personViewController.h中

#import <UIKit/UIKit.h>
#import "CoreViewController.h"
#import "Person.h"

@interface personViewController : CoreViewController

- (IBAction)savePressed:(id)sender;
- (IBAction)cancelPressed:(id)sender;

@property (strong, nonatomic) IBOutlet UITextField *personNameTextField;

@property (strong,nonatomic) Person *addPerson;

@end

我的personViewController.m(添加带有文本字段的人员视图控制器)

#import "personViewController.h"
#import "AppDelegate.h"
#import "CoreViewController.h"

@interface personViewController ()

@end

@implementation personViewController

@synthesize addPerson;

@synthesize personNameTextField;

- (IBAction)savePressed:(id)sender {

    addPerson.personName = personNameTextField.text;
    [super saveAndDismiss];   
}

- (IBAction)cancelPressed:(id)sender {

    [super cancelAndDismiss];
}
@end

对于这么多代码感到抱歉,我感到非常困难并且没有想法

0 个答案:

没有答案