使用 Core Data 在实体中插入和修改记录

发布于 2024-08-28 09:33:43 字数 272 浏览 11 评论 0原文

我试图在互联网上找到我的问题的答案,但我找不到。 我在核心数据中有一个简单的实体,它有一个 Value 属性(即整数)和一个 Date 属性。我想在 .m 文件中定义两个方法。

第一种方法是ADD方法。它需要两个参数:一个整数值(由用户在 UI 中输入)和一个日期(默认为当前日期)。然后根据参数将记录插入到实体中。

第二种方法类似于增量方法。它使用日期作为键来查找记录,然后递增该记录的整数值。

我不知道如何编写这些方法。 (假设我们有一个用于 xib 文件中的表的数组控制器)

I tried to find the answer of my question on the internet, but I could not.
I have a simple entity in Core data that has a Value attribute (that is integer) and a Date attribute. I want to define two methods in my .m file.

First method is the ADD method. It takes two arguments: an integer value (entered by user in UI) and a date (current date by default). and then insert a record into the entity based on the arguments.

Second method is like an increment method. It uses the Date as a key to find a record and then increment the integer value of that record.

I don't know how to write these methods. (assume that we have an Array Controller for the table in the xib file)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缺⑴份安定 2024-09-04 09:33:43

您正在寻找的第二种方法不适合添加到实体本身。它需要位于实体上方的某个位置,最有可能在您的控制器对象中。

第一种方法如下:

- (void)updateDate:(NSDate*)date andValue:(NSInteger)value
{
  [self setValue:date forKey:@"date"];
  [self setValue:[NSNumber numberWithInteger:value] forKey:@"value"];
}

这是相当直接的 KVC(键值编码),我强烈建议您阅读 Apple 关于该主题的文档。

对于您的其他方法,应该在控制器中,您需要执行提取来查找记录。

- (id)findRecordForDate:(NSDate*)date inManagedObjectContext:(NSManagedObjectContext*)moc
{
  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:moc]];
  [request setPredicate:[NSPredicate predicateWithFormat:@"date == %@", date]];

  NSError *error = nil;
  NSArray *objects = [moc executeFetchRequest:request error:&error];
  NSAssert1(error == nil, @"Error fetching object: %@", [error localizedDescription]);

  return [objects lastObject];
}

- (void)incrementEntityWithDate:(NSDate*)date
{
  id entity = [self findRecordForDate:date inManagedObjectContext:[self managedObjectContext]];
  NSInteger value = [[entity valueForKey:@"value"] integerValue];
  value += 1;
  [entity setValue:[NSNumber numberWithInteger:value] forKey:@"value"];
}

这也是非常简单的核心数据访问。我建议您阅读 Core Data 的工作原理。

顺便说一句,使用日期作为唯一值是一个非常糟糕的设计。

更新

马库斯,谢谢你的回答。这真的很有帮助。我是核心数据新手,所以我有几个问题可以让事情变得更清楚。

第一个方法的代码设置两个值,但它不会将新记录插入表中。如何将新创建的记录插入表中?

您需要了解 Core Data 的工作原理。 Apple 有关于 Core Data 如何工作的详细文档,如果失败,您可以购买我的书。互联网上有大量有关如何使用 Core Data 的信息。

我应该把第一个方法放在哪里?在我的 .m 文件中?

这是基本的 Objective-C。如果您问这个问题,您需要先退一步并学习该语言的基础知识。

您提到我需要在控制器中添加第二种方法。但控制器是在xib文件中定义的。如何添加第二种方法?

控制器从未在 xib 文件中定义。它在 xib 文件中被引用。再次强调,在深入研究之前,您需要先回到 Objective-C 的工作原理并学习基础知识。

The second method you are looking for is not appropriate to add to the entity itself. It needs to be somewhere above the entities, most likely in your controller object.

The first method is as follows:

- (void)updateDate:(NSDate*)date andValue:(NSInteger)value
{
  [self setValue:date forKey:@"date"];
  [self setValue:[NSNumber numberWithInteger:value] forKey:@"value"];
}

This is fairly straight KVC (Key-Value Coding) and I highly recommend that you read Apple's docs on the subject.

For your other method, that should be in a controller, you need to perform a fetch to find the record.

- (id)findRecordForDate:(NSDate*)date inManagedObjectContext:(NSManagedObjectContext*)moc
{
  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:moc]];
  [request setPredicate:[NSPredicate predicateWithFormat:@"date == %@", date]];

  NSError *error = nil;
  NSArray *objects = [moc executeFetchRequest:request error:&error];
  NSAssert1(error == nil, @"Error fetching object: %@", [error localizedDescription]);

  return [objects lastObject];
}

- (void)incrementEntityWithDate:(NSDate*)date
{
  id entity = [self findRecordForDate:date inManagedObjectContext:[self managedObjectContext]];
  NSInteger value = [[entity valueForKey:@"value"] integerValue];
  value += 1;
  [entity setValue:[NSNumber numberWithInteger:value] forKey:@"value"];
}

This is also very straightforward Core Data access. I recommend you read up on how Core Data works.

As an aside, using a date as a unique is a very bad design.

UPDATE

Marcus, Thanks for the answer. It is really helpful. I am new to core data so I have a few questions to make things clearer.

The code for the first method sets two values, but it doesn't insert the new record into the table. How can I insert the newly created record into the table?

You need to read up on how Core Data works. Apple has great documentation on how Core Data works and if that fails you can buy my book. There is a TON of information about how to use Core Data on the internet.

Where should I put the first method? in my .m file?

This is basic Objective-C. If you are asking this question you need to step way back and learn the fundamentals of the language first.

You mentioned that I need to add the second method in the controller. But the controller is defined in the xib file. How can I add the second method to that?

The controller is never defined in the xib file. It is referenced in the xib file. Again, you need to go back to the beginning of how Objective-C works and learn the basics before you dive this deep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文