简体   繁体   English

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

[英]Core Data issue. Data won't save

So I have a utility app and I am trying to save some text into a "To" and "Message: text field on the Flipside View Controller. However, my data won't save. I am new to objective C and I have been using multiple different tutorials to the point where I have totally confused myself. Hopefully you can help me out. Not sure what else to do at this point... 因此,我有一个实用应用程序,我试图将一些文本保存到Flipside View Controller上的“ To”和“ Message:”文本字段中。但是,我的数据无法保存。我对目标C还是陌生的使用了多个不同的教程,使我完全感到困惑。希望您能为我提供帮助。不确定目前还需要做什么...

FlipsideViewController.m 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

I didn't look at all your code because there was a problem near the top that negates everything you do thereafter. 我没有查看您的所有代码,因为顶部附近存在一个问题,该问题否定了您以后所做的一切。 Don't alloc/init your app delegate in awakeFromNib or anywhere else for that matter. 不要在aakeakeFromNib或其他任何地方分配/初始化您的应用程序委托。 The one and only instance of your app delegate already exists (I have no idea what happens when there is more than one app delegate). 您的应用程序委托的唯一实例已经存在(我不知道当有多个应用程序委托时会发生什么)。

CCCFlipsideViewController needs to gain access to the managed object context through another means. CCCFlipsideViewController需要通过另一种方式来访问托管对象上下文。 Perhaps CCCMainViewController (or another view controller) could set the CCCFlipsideViewController's managedObjectContext property. 也许CCCMainViewController(或另一个视图控制器)可以设置CCCFlipsideViewController的managedObjectContext属性。 If CCCMainViewController does not have access to the managed object context, have the app delegate pass that context to it. 如果CCCMainViewController无权访问托管对象上下文,则让应用程序委托将该上下文传递给它。

Example: App delegate sets a managedObjectContext property on the root view controller; 示例:应用程序委托在根视图控制器上设置了ManagedObjectContext属性; the root view controller, in turn, sets the managedObjectContext property on a child view controller (like your flipside VC), etc. 根视图控制器依次在子视图控制器(如您的反面VC)上设置managedObjectContext属性,等等。

You don't seem to ever actually set self.messageTextField.text or self.toTextField.text to anything -- you have commented out code in your viewDidLoad method which sets these fields. 您似乎从未真正将self.messageTextField.text或self.toTextField.text设置为任何值-您已在设置这些字段的viewDidLoad方法中注释掉了代码。 bilobatum is entirely correct about your AppDelegate issue as well -- you could also use something like 关于您的AppDelegate问题,bilobatum也完全正确-您也可以使用类似

[((NSObject*)[UIApplication sharedApplication].delegate) valueForKey: @"managedObjectContext"];

to get the app delegate for your application if you want to fix that fast, though long term bilobatum's solution to this is better design. 如果您想快速解决问题,可以为您的应用程序获取应用程序委托,尽管长期的loblobatum解决方案是更好的设计。

Honestly, I think you've done quite a number on this code... ;) 老实说,我认为您已经在此代码上做了很多工作...;)

OK, first off, in your save method, don't create another NSManagedObjectContext, use the instance variable you already declared, "managedObjectContext." 好吧,首先,在您的save方法中,不要创建另一个NSManagedObjectContext,使用已经声明的实例变量“ managedObjectContext”。

Secondly, I think you've made things way too complicated for yourself... Storing Core Data is actually shockingly simple once you've created the NSManagedObject subclasses and set up everything in the App Delegate... 其次,我认为您使事情变得太复杂了……创建NSManagedObject子类并在App Delegate中设置所有内容后,存储核心数据实际上非常简单。

It seems as if you wouldn't need any info from the "fetchedResultsController" at that point in your code, since you're saving, not fetching. 似乎您的代码此时不需要“ fetchedResultsController”中的任何信息,因为这是保存而不是获取。 Maybe try changing your save method to something like: 也许尝试将您的保存方法更改为:

- (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();
    }

} }


Edit: And to get the managed object context from the app delegate... 编辑:并从应用程序委托获取托管对象的上下文...

Right after your @synthesize's, create a variable for the App Delegate. 在@synthesize之后,立即为App Delegate创建一个变量。

AppDelegate* appDelegateController;

And in viewDidLoad, initialize it: 然后在viewDidLoad中将其初始化:

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

Right after viewDidLoad (or anywhere you want), you can stick in a method to declare the managed object context: 在viewDidLoad之后(或您想要的任何地方)之后,可以使用一种方法声明托管对象的上下文:

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

Then back in viewDidLoad, call that method with: 然后回到viewDidLoad中,使用以下方法调用该方法:

self.managedObjectContext = [self managedObjectContext];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM