核心数据不保存
Posted
技术标签:
【中文标题】核心数据不保存【英文标题】:Core Data Not Saving 【发布时间】:2014-08-17 13:42:29 【问题描述】:我有一个使用核心数据框架的应用程序,我对它还很陌生。当我输入有关“飞行员”的信息并将其保存时,该实体的 NSLog 返回 null,我无法弄清楚他们为什么不保存任何帮助将不胜感激。
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import <Foundation/Foundation.h>
#import "Pilot.h"
@interface PilotViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIPopoverControllerDelegate, ABPeoplePickerNavigationControllerDelegate>
@property (weak, nonatomic ) IBOutlet UITextField *nameField;
@property (weak, nonatomic ) IBOutlet UITextField *phoneField;
@property (weak, nonatomic ) IBOutlet UITextField *insuranceField;
@property (weak, nonatomic ) IBOutlet UITextField *emailField;
@property (strong ) NSManagedObject *pilot;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
#import "PilotViewController.h"
@interface PilotViewController ()
@end
@implementation PilotViewController
@synthesize pilot;
#pragma mark Core Data Managed Object
- (NSManagedObjectContext *)managedObjectContext
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)])
context = [delegate managedObjectContext];
return context;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
#pragma mark Save and Cancel Buttons
- (IBAction)cancel:(id)sender
[self dismissViewControllerAnimated:YES completion:nil];
- (IBAction)save:(id)sender
NSManagedObjectContext *context = [self managedObjectContext];
if (self.pilot)
//Update existing pilot information
[self.pilot setValue:self.nameField.text forKey:@"pilotName"];
[self.pilot setValue:self.phoneField.text forKey:@"pilotPhone"];
[self.pilot setValue:self.insuranceField.text forKey:@"pilotInsurance"];
[self.pilot setValue:self.emailField.text forKey:@"pilotEmail"];
else
// Create a new managed object
NSManagedObject *newPilot = [NSEntityDescription insertNewObjectForEntityForName:@"Pilot" inManagedObjectContext:context];
[newPilot setValue:self.nameField.text forKey:@"pilotName"];
[newPilot setValue:self.phoneField.text forKey:@"pilotPhone"];
[newPilot setValue:self.insuranceField.text forKey:@"pilotInsurance"];
[newPilot setValue:self.emailField.text forKey:@"pilotEmail"];
NSString *pilotName = [pilot valueForKey:@"pilotName"];
NSLog( @"%@",pilotName);
//NSLog(@"Value of pilotName = %@",pilot);
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error])
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
[self dismissViewControllerAnimated:YES completion:nil];
@end
【问题讨论】:
欢迎来到 SOF。不鼓励代码“倾销”。你应该只发布你不确定的代码。 我认为您的意思是respondsToSelector
不是 performSelector
。但是,我怀疑这会解决您的问题。
我们可以看到您在应用委托中设置托管对象上下文的位置吗?
更改为respondsToSelector
很重要,因为如果代理没有该选择器并且您尝试在代理上执行该选择器,您的应用程序将会崩溃。你会得到这些坏男孩之一,-[AppDelegate managedObjectContext]: unrecognized selector sent to instance <your_instance>
【参考方案1】:
似乎您的托管对象上下文为零。你为什么首先将它设置为零?检索它的正确方法是确保将应用委托转换为您的应用委托:
AppDelegate *delegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
return delegate.manageObjectContext;
Apple 推荐的模式不使用应用委托。您应该为您的班级提供一个 @property
作为上下文,并从呈现视图控制器传递上下文(例如在 prepareForSegue
中)。
【讨论】:
那么在我有 = nil 的地方,我应该有应用委托吗? 他们在哪里将上下文设置为零?我没看到。他们似乎只是在初始化 ManagedObjectContext 指针。此外,尚不清楚他们是否在托管对象上下文中设置了持久化。 @user3927993 不,你的问题没有意义。也许你对此有点过头了。以上是关于核心数据不保存的主要内容,如果未能解决你的问题,请参考以下文章