核心数据模型设置

编程入门 行业动态 更新时间:2024-10-28 20:30:54
本文介绍了核心数据模型设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有使用Core Data Model跟踪iOS应用程式的费用:

场景:

- >抽象父实体命名为Money,属性为vendor,date,amount等。两个子实体费用和收入从父实体金钱继承。然后还有其他实体,如类别和子类别及其属性。总数现在:我的数据模型中有5个实体(金钱,费用,收入,类别和子类别)。

问题:我想要实现的是跟踪费用,每周,每两周,每月和每年。我想让一个实体说月有12个属性(1月 - 12月 - >月名称),但不是太复杂吗?

思考? / p>

我有一个表视图,并使用NSFetchedResultsController来填充我的表视图混合od开支和收入。

解决方案

您必须使用 NSPredicate 创建一个 NSFetchRequest 适当的时间段。假设您想列出两个日期之间的所有费用,您可以实现一个返回在此期间过滤的 NSFetchRequest 的方法:

- (NSFetchRequest *)filteredFetchRequestForEntity:(NSString *)entityName startDate:(NSDate *)startDate endDate:(NSDate *)endDate { NSPredicate * predicate = [NSPredicate predicateWithFormat:@(date> =%@)AND(date< =%@,startDate,endDate]; NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription * entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; [fetchRequest setPredicate:predicate]; return fetchRequest; }

然后,您可以使用这个fetchRequest例如在 NSFetchedResultsController 类似于我的回答在你的另一个问题: CoreData和TableViews

然后你需要做的是创建NSDate对象的start-和endDate,时间段,并将其传递给此方法。

I have an expense tracking iOS Application using Core Data Model:

Scenario:

-> An abstract parent entity named "Money" with attributes "vendor", "date", "amount" and so on. The two sub-entities "Expense" and "Income" inherit from the parent entity "Money". Then there are other entities such as "Category" and "subCategory" with their attributes. Total as of now: 5 entities (Money, Expense, Income, Category and Subcategory) in my data model.

Question: What I want to achieve is to track expenses daily, per week, bi-weekly, monthly and yearly. I am thinking to make an entity say "Months" with 12 attributes ( Jan - Dec - > month names) but isn't that too much complicated?

Thoughts?

I have a Table-view and using NSFetchedResultsController to fill my table-view with the mix od expenses and Incomes.

解决方案

You have to create an NSFetchRequest with an NSPredicate that filters for the appropriate time period. Let's say you want to list all expenses between two dates your could implement a method that returns an NSFetchRequest that filters for this period:

- (NSFetchRequest *)filteredFetchRequestForEntity:(NSString *)entityName startDate:(NSDate *)startDate endDate:(NSDate *)endDate { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@", startDate, endDate]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; [fetchRequest setPredicate:predicate]; return fetchRequest; }

You could then use this fetchRequest for example in an NSFetchedResultsController similar to my answer in your other question: CoreData and TableViews

Then all you'd have to do is create the NSDate objects for start- and endDate for your desired time period and pass them to this method.

更多推荐

核心数据模型设置

本文发布于:2023-11-16 08:21:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1602586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   数据模型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!