核心数据模型设置 [英] Core Data Model set up

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

问题描述

我有使用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.

这篇关于核心数据模型设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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