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

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

问题描述

这个问题是关于Core Data的。



我创建了一个名为 TV 的实体, ,价格尺寸。我还用 TV.h TV.m 文件创建了NSMutableObject的子类。



strong> TV.h 到我的 DetailViewController.h ,它处理我的滑块和UIElements,我想取值。



所以我做了一个抓取请求,一切工作正常,



每当我更新UISlider(valueDidChange :),Xcode创建一个 COPY ,并将其添加到我的电视对象



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



帮助非常感谢!



提前感谢。



我的代码:



DetailViewController.m

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

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

if(sender == sizeSlider){

NSError * error = nil;

NSManagedObjectContext * context = [self managedObjectContext];

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:@TVinManagedObjectContext: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 = @新电视!;

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 实体。



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



因此,在DetailViewController.h中:

  @property ,nonatomic)id detailItem; 

成为

  @property(strong,nonatomic)TV detailItem; 

在你的collectSliderValue方法中,它应该看起来更简单一些:

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

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

托管对象上下文的保存,视图控制器,这是在您的应用程序委托。



在您的主详细信息控制器.m文件,您可能还需要导入TV.h文件,知道电视是一个 NSManagedObject 子类。此外,当您设置细节项目时,转换为电视:

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


This question is about Core Data.

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.

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:

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

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

Help is very appreciated!

Thank you in advance.

My Code:

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]; 

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.

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.

So, in DetailViewController.h:

@property (strong, nonatomic) id detailItem;

becomes

@property (strong, nonatomic) TV detailItem;

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.

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天全站免登陆