简体   繁体   English

更新核心数据模型对象(iOS)时出错

[英]Running into error when updating core data model object (iOS)

I am running into a weird error when I try and update my core data object (null error). 尝试更新核心数据对象时,我遇到了一个奇怪的错误(空错误)。 The first time I save the object (Restaurant) it saves perfectly. 第一次保存对象(餐厅)时,它可以完美保存。 However, when I try and update the restaurant - that is where I am running into errors: 但是,当我尝试更新餐厅时-这是我遇到错误的地方:

RestaurantDetailViewController.h : RestaurantDetailViewController.h:

@class Restaurant;

@interface RestaurantDetailViewController : UITableViewController <UINavigationControllerDelegate, UITextViewDelegate, CuisinePickerViewControllerDelegate>


@property (nonatomic, strong) IBOutlet UITextView *reviewTextView;
@property (nonatomic, strong) IBOutlet UILabel *cuisineLabel;
@property (nonatomic, strong) IBOutlet UILabel *dateLabel;
@property (strong, nonatomic) IBOutlet UILabel *addressLabel;

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, assign) double longitude;
@property (nonatomic, assign) double latitude;

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) Restaurant *restaurantToEdit;


- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
@end

RestaurantDetailViewController.m: RestaurantDetailViewController.m:

- (void)viewDidLoad
{
  [super viewDidLoad];

  if (self.restaurantToEdit != nil) {
    self.navigationItem.title = @"Edit Restaurant";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                              initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                              target:self
                                              action:@selector(done:)];
  }

  date = [NSDate date];

  self.navigationItem.title = name;
  self.reviewTextView.text = reviewText;
  self.cuisineLabel.text = @"No Cuisine selected";
  self.addressLabel.text = address;
  self.dateLabel.text = [self formatDate:date];


- (IBAction)done:(id)sender
{

  Restaurant *restaurant = nil;
  if (self.restaurantToEdit != nil) {
    restaurant = self.restaurantToEdit;
  } else {
    restaurant = [NSEntityDescription insertNewObjectForEntityForName:@"Restaurant" inManagedObjectContext:self.managedObjectContext];
  }

  restaurant.restaurantName = name;
  restaurant.restaurantReview = reviewText;
  restaurant.cuisine = cuisineType;
  restaurant.latitude = [NSNumber numberWithDouble:self.latitude];
  restaurant.longitude = [NSNumber numberWithDouble:self.longitude];
  restaurant.date = date;
  restaurant.address = address;

  NSError *error;
  if (![self.managedObjectContext save:&error]) {
    NSLog(@"Error %@", error);
    abort();
    return;
  }


  [self performSelector:@selector(closeScreen) withObject:nil afterDelay:0.6];
}

- (void)setRestaurantToEdit:(Restaurant *)newRestaurantToEdit
{
  if (restaurantToEdit != newRestaurantToEdit) {
    restaurantToEdit = newRestaurantToEdit;

    reviewText = restaurantToEdit.restaurantReview;
    cuisineType = restaurantToEdit.cuisine;
    date = restaurantToEdit.date;
    address = restaurantToEdit.address;
  }
}

relevant code in mapviewcontroller.m : mapviewcontroller.m中的相关代码:

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

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

      UINavigationController *navigationController = segue.destinationViewController;
      RestaurantDetailViewController *controller = (RestaurantDetailViewController *)navigationController.topViewController;

      if (newRestaurant == YES) {
        controller.name = restaurantName;
        controller.address = restaurantAddress;
        controller.longitude = restaurantLongitude;
        controller.latitude = restaurantLatitude;
        controller.managedObjectContext = self.managedObjectContext;
      } else {
        Restaurant *restaurant = [restaurants objectAtIndex:((UIButton *)sender).tag];
        controller.restaurantToEdit = restaurant;
      }
  }
} 

only thing that pops up in my nslog is (NULL) error. 在我的nslog中弹出的唯一错误是(NULL)错误。 If you need more code, just let me know. 如果您需要更多代码,请告诉我。

It appears that in your prepareForSegue method, you aren't setting the controller.managedObjectContext property if it's an update. 看来,在prepareForSegue方法中,如果它是更新,则没有设置controller.managedObjectContext属性。 You need to add 您需要添加

controller.managedObjectContext = self.managedObjectContext;

so that's it's called whether its a new restaurant or an update. 所以就是所谓的是新餐厅还是新餐厅。

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

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