实体的核心数据保存属性 [英] Core Data Saving Attributes Of Entities

查看:27
本文介绍了实体的核心数据保存属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于核心数据的.

This question is about Core Data.

我创建了一个名为 TV 的实体,它具有三个属性:namepricesize.我还使用 TV.hTV.m 文件创建了 NSMutableObject 的子类.

I created a Entity called TV with three attributes called name, price and size. I also created a subclass of NSMutableObject with TV.h and TV.m files.

我将 TV.h 导入到我的 DetailViewController.h 中,它处理我想要获取值的滑块和 UIElements.

I imported the TV.h to my DetailViewController.h which handles my sliders und UIElements I want to take the values of.

所以我做了一个提取请求,一切正常,但是:

So I did a fetch request, and everything works fine, BUT:

每次我更新 UISlider (valueDidChange:) 时,Xcode 都会为我的实体创建一个 COPY 并将其添加到我的 TV-Object.

Everytime I update the UISlider (valueDidChange:), Xcode creates a COPY of my entity and adds it to my TV-Object.

我想要 Xcode 只是编辑并保存到当前实体,而不是编辑并保存到新实体中.

All I want Xcode is just to edit and save to the current entity, not to edit and save in a new entity.

非常感谢您的帮助!

提前谢谢你.

我的代码:

DetailViewController.m

- (IBAction)collectSliderValue:(UISlider *)sender {

if (__managedObjectContext == nil) {
    NSLog(@"Problem ...");
    __managedObjectContext = [(MasterViewController *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    NSLog(@"... solved!");
}

if (sender == sizeSlider) {

    NSError *error = nil;

    NSManagedObjectContext *context = [self managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TV" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

    TV * currentTV = [[TV alloc] initWithEntity:entity insertIntoManagedObjectContext:context];

    currentTV.size = [[NSNumber alloc] initWithInt:(sender.value + 0.5f)];
    currentTV.name = @"New TV!";

    NSError *error11;
    [__managedObjectContext save:&error11];

    for (NSManagedObject *info in fetchedObjects)
    {
        NSLog(@"Name = %@", [info valueForKey:@"name"]);
        NSLog(@"Size = %@", [info valueForKey:@"size"]);
        NSLog(@"Price = %@", [info valueForKey:@"price"]);
    }
    [fetchRequest release];
}

推荐答案

//Editing begins ...     
TV * currentTV = [[TV alloc] initWithEntity:entity insertIntoManagedObjectContext:context]; 

编辑并未开始,您正在那里创建一个新对象.您的视图控制器需要一个实例变量来保存您正在修改的当前 TV 实体.

Editing doesn't begin, you are creating a new object right there. Your view controller needs an instance variable to hold the current TV entity that you are modifying.

从您创建的模板项目中,变量 detailItem 包含您当前正在编辑的托管对象.您应该专门将其设置为 TV 对象,并在您的 detailViewController 代码中引用它而不是 currentTV.您必须删除所有获取请求和托管对象上下文代码 - 这与您的详细视图控制器无关,它应该由主视图控制器管理.

From the template project you have created, the variable detailItem contains the managed object that you are currently editing. You should specifically set this as a TV object, and refer to this instead of currentTV in your detailViewController code. You must remove all of the fetch request and managed object context code - this is not relevant in your detail view controller, it should be managed by the master view controller.

所以,在 DetailViewController.h 中:

So, in DetailViewController.h:

@property (strong, nonatomic) id detailItem;

变成

@property (strong, nonatomic) TV detailItem;

在您的 collectSliderValue 方法中,它应该看起来更简单,如下所示:

And in your collectSliderValue method, it should look something much more simple like this:

- (IBAction)collectSliderValue:(UISlider *)sender 
{

    if (sender == sizeSlider) 
        self.detailItem.size = [NSNumber numberWithFloat:sender.value];    
}

在返回到详细视图控制器之前不应保存托管对象上下文,这在您的应用程序委托中处理.

The saving of the managed object context shouldn't occur until back in your detail view controller, this is taken care of in your application delegate.

在您的主细节控制器 .m 文件中,您可能还需要导入 TV.h 文件,以便它知道 TV 是 NSManagedObject 子类.另外,在设置细节项时投射到电视上:

In your master detail controller .m file you may also need to import the TV.h file so that it knows that TV is a NSManagedObject subclass. Also, cast to TV when you are setting the detail item:

self.detailViewController.detailItem = (TV*)selectedObject;

这篇关于实体的核心数据保存属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆