ios - 核心数据问题。数据不会保存

标签 ios objective-c core-data save

所以我有一个实用程序应用程序,我试图将一些文本保存到 Flipside View Controller 上的“收件人”和“消息:”文本字段中。但是,我的数据不会保存。我是 Objective 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

最佳答案

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

CCCFlipsideViewController 需要通过另一种方式访问​​托管对象上下文。也许 CCCMainViewController (或另一个 View Controller )可以设置 CCCFlipsideViewController 的 ManagedObjectContext 属性。如果 CCCMainViewController 无权访问托管对象上下文,请让应用程序委托(delegate)将该上下文传递给它。

例子: 应用程序委托(delegate)在 Root View Controller 上设置一个 ManagedObjectContext 属性; Root View Controller 反过来在 subview Controller (如您的 Flipside VC)上设置 ManagedObjectContext 属性,等等。

关于ios - 核心数据问题。数据不会保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19070961/

相关文章:

ios - 核心数据和 NSManagedObject

MySQL 数据库规范化

ios - 下划线或无下划线

ios - 如何添加右侧滑动菜单 iOS

IOS:在uitableview中跳转一个位置

ios - 在函数调用后执行

iphone - 可以从主包中删除文件吗?

iphone - UIImage 屏幕截图失去质量

ios - 在 iOS 应用程序中集成 Google Plus

objective-c - 使用 KVO 观察 Objective-C 集合中对象属性的变化