核心数据问题。数据无法保存

时间:2013-09-28 19:42:17

标签: ios objective-c core-data save

所以我有一个实用程序应用程序,我正在尝试将一些文本保存到Flipside View Controller上的“To”和“Message:text”字段。但是,我的数据不会保存。我是目标C的新手我一直在使用多个不同的教程,以至于我完全混淆了自己。希望你可以帮助我。不知道此时还有什么可做......

FlipsideViewController.m

#import "CCCFlipsideViewController.h"
#import "CCCAppDelegate.h"
#import "CCCMainViewController.h"
#import "MessageDetails.h"

@interface CCCFlipsideViewController ()
{
   // NSManagedObjectContext *context;
}
@end

@implementation CCCFlipsideViewController
@synthesize allMessageDetails;
@synthesize managedObjectContext;

- (void)awakeFromNib
{
    [super awakeFromNib];

    CCCAppDelegate *appDelegateController = [[CCCAppDelegate alloc]init];
    self.managedObjectContext = appDelegateController.managedObjectContext;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"MessageDetails" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSError *error;

    self.allMessageDetails = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    /*
    NSManagedObject *managedObject; = [_fetchedResultsController valueForKey:@"to"];
    self.toTextField.text = managedObject to;

    messageDetails.to = [allMessageDetails firstObject];
    self.toTextField.text = messageDetails.to;

    messageDetails.message = [allMessageDetails valueForKey:@"message"];
    self.messageTextField.text = messageDetails.message;
    */
    NSLog(@"The 'to' is currently at %@ after viewdidload", self.toTextField.text);

    }

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   return [textField resignFirstResponder]; //function says that if (bool) the text field is open and the keyboard hits return, text field is to resign first responder.
}

#pragma mark - Actions
- (IBAction)done:(id)sender
{
    [self.delegate flipsideViewControllerDidFinish:self];
}

- (IBAction)resignFirstResponder:(id)sender {

    [self.toTextField resignFirstResponder];
    [self.messageTextField resignFirstResponder];
    NSLog(@"Resigned First Responder");
}


- (IBAction)save:(id)sender {

    // Create a new instance of the entity managed by the fetched results controller.
   NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    // If appropriate, configure the new managed object.
    [newManagedObject setValue:self.toTextField.text forKey:@"to"];
    [newManagedObject setValue:self.messageTextField.text forKey:@"message"];

    // Save the context.
    NSError *error = nil;
    if (![context save:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


}


#pragma mark -
#pragma mark Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    /*
     Set up the fetched results controller.
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MessageDetails" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"to" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;


    NSError *error = nil;
    if (![_fetchedResultsController 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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}


@end

3 个答案:

答案 0 :(得分:1)

我没有查看你的所有代码,因为顶部附近有一个问题否定了你之后所做的一切。不要在awakeFromNib或其他任何地方分配/初始化您的应用程序委托。您的应用委托的唯一实例已经存在(我不知道当有多个应用委托时会发生什么)。

CCCFlipsideViewController需要通过其他方式访问托管对象上下文。也许CCCMainViewController(或其他视图控制器)可以设置CCCFlipsideViewController的managedObjectContext属性。如果CCCMainViewController无权访问托管对象上下文,请让应用程序委托将该上下文传递给它。

实施例: App委托在根视图控制器上设置managedObjectContext属性;反过来,根视图控制器在子视图控制器上设置managedObjectContext属性(如你的翻转VC)等。

答案 1 :(得分:0)

您似乎没有将self.messageTextField.text或self.toTextField.text设置为任何内容 - 您已在viewDidLoad方法中注释掉了设置这些字段的代码。 bilobatum对你的AppDelegate问题也是完全正确的 - 你也可以使用像

这样的东西
[((NSObject*)[UIApplication sharedApplication].delegate) valueForKey: @"managedObjectContext"];

如果您想快速解决问题,请为您的应用程序获取应用程序委托,但长期的bilobatum解决方案是更好的设计。

答案 2 :(得分:0)

老实说,我认为你在这段代码上做了很多数字......;)

好的,首先,在你的save方法中,不要创建另一个NSManagedObjectContext,使用你已声明的实例变量“managedObjectContext。”

其次,我认为你已经让事情变得太复杂了......一旦你创建了NSManagedObject子类并在App Delegate中设置了所有东西,存储核心数据实际上非常简单......

在您的代码中,您似乎不需要“fetchedResultsController”中的任何信息,因为您正在保存,而不是提取。也许尝试将保存方法更改为:

- (IBAction)save:(id)sender {

    NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:@"MessageDetails" inManagedObjectContext:self.managedObjectContext];

    // If appropriate, configure the new managed object.
    [entity setValue:self.toTextField.text forKey:@"to"];
    [entity setValue:self.messageTextField.text forKey:@"message"];

    // Save the context.
    NSError *error = nil;
    [self.managedObjectContext save:&error]

    if (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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

}


编辑:并从app delegate获取托管对象上下文...

在@ synthesize之后,为App Delegate创建一个变量。

AppDelegate* appDelegateController;

在viewDidLoad中,初始化它:

appDelegateController = (AppDelegate*)[[UIApplication sharedApplication] delegate];

在viewDidLoad之后(或任何你想要的地方),你可以坚持使用一种方法来声明托管对象的上下文:

- (NSManagedObjectContext*)managedObjectContext {
    return appDelegateController.managedObjectContext;
}

然后返回viewDidLoad,用以下方法调用该方法:

self.managedObjectContext = [self managedObjectContext];