2013-08-19 78 views
0

當我嘗試更新核心數據對象(空錯誤)時,我遇到了一個奇怪的錯誤。第一次保存對象(餐廳)時,它可以完美保存。然而,當我嘗試更新的餐廳 - 這就是我遇到的錯誤:更新核心數據模型對象(iOS)時出現錯誤

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:在MapViewController中

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

相關的代碼。 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; 
     } 
    } 
} 

只有在我的nslog中彈出的東西我s(NULL)錯誤。如果你需要更多的代碼,請讓我知道。

回答

3

看來,在您的prepareForSegue方法中,如果它是更新,則不會設置controller.managedObjectContext屬性。您需要添加

controller.managedObjectContext = self.managedObjectContext; 

所以這就是它的調用方式,無論是新餐廳還是更新。