核心数据不保存项目

时间:2015-09-29 16:39:53

标签: objective-c core-data

核心数据问题 - 当我保存项目时,他会放置旧项目的详细信息 而不是保存新项目的细节。

我怎么能解决它?

MenuViewcontroller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSLog(@"Pass");
    if ([[segue identifier] isEqualToString:@"UpdateItem"]) {
        SmartWishList *selectedDevice = [self.SmartWishList objectAtIndex:[[self.tableView indexPathForSelectedRow]row]];
        AddItemViewController *destViewController = segue.destinationViewController;
        destViewController.selectedDevice = selectedDevice;

    }
}

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

-(void)fetchObjects{
    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"SmartWishList"];
    self.SmartWishList = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}

Additemviewcontroller:

    - (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context =nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (IBAction)save:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

    if (self.selectedDevice) {
        // Update existing device

        [self.selectedDevice setValue:self.titleTextField.text forKey:@"title"];
        [self.selectedDevice setValue:self.webTextField.text forKey:@"website"];
        [self.selectedDevice setValue:self.priceTextField.text forKey:@"price"];
        [self.selectedDevice setValue:[self.priceTextField currencySignString] forKey:@"currency"];
    } else {


        // Create a new managed object
        NSManagedObject *newItem = [NSEntityDescription  insertNewObjectForEntityForName:@"SmartWishList" inManagedObjectContext:context];
        [newItem setValue:self.titleTextField.text forKey:@"title"];
        [newItem setValue:self.webTextField.text forKey:@"website"];
        [newItem setValue:self.priceTextField.text forKey:@"price"];
        [newItem setValue:[self.priceTextField currencySignString] forKey:@"currency"];
    }


    NSError *error = nil;
    if ([[self managedObjectContext] save:&error] == NO) {
        NSAssert(NO, @"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}
-(void)ChangeColorPlaceHolder:(UITextField *)textField withText:(NSString *)text andColor:(UIColor *)color
{

    textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:text attributes:@{NSForegroundColorAttributeName:color}];


}
- (IBAction)SegmentControl:(id)sender {
    NSInteger selectedIndex = ((UISegmentedControl *)sender).selectedSegmentIndex;
    [self.priceTextField setCurrency:(CurrencySign)selectedIndex];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    NSLog(@"Update item");

    if (self.selectedDevice) {
        // Update existing device
        self.title = self.selectedDevice.title;
        [self.titleTextField setText:self.selectedDevice.title];
        [self.webTextField setText:self.selectedDevice.website];

        [self.priceTextField setText:self.selectedDevice.price];
        [self.priceTextField setCurrecnySign:self.selectedDevice.currency];

        [self.segmentController setSelectedSegmentIndex:(NSInteger)self.priceTextField.currency];

    }



}

谢谢你的帮助!我怎么能解决它?

1 个答案:

答案 0 :(得分:-1)

我可以建议一个解决方案: 而不是:

NSManagedObjectContext *context = [self managedObjectContext];

使用它:

 NSManagedObjectContext *context = [[self getAppDelegate] managedObjectContext];

要支持此解决方案,请添加方法:

-(AppDelegate *)getAppDelegate{
return (AppDelegate *)[[UIApplication sharedApplication]delegate];

}

(导入AppDelegate.h)

现在,请添加此方法:

- (NSManagedObjectContext *)managedObjectContext{
if (_managedObjectContext != nil) {
    return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;

}

AppDelegate.m中的

现在,验证您的所有项目都有此方法调用以获取上下文。

并且不要忘记何时打开任何新视图(例如从表视图中选择任何行),以从Core数据中读取相关行。

这是我的第一个错误,我花了几天时间来理解错误的观点。

我希望这将有用