1
votes

Describing Core Data Model:

I have a Core data model for my Expense tracking application. I made 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 in my data model.

I have a relationship from the sub-entities "Expense" and "Income" to entity "Category" which in turn has a to-many relationship with the entity "SubCategory" as a category can have one or many sub-categories. The entity "Category" has a to-many relationship to the entities "Expense" and "Income" as well as there can be may expenses or incomes for a particular category. That makes sense to me as of now.

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

I have an alert view with buttons "ExpenseS" and "Income" - both are pushed to a same view controller which has the details like fill a vendor, amount, category, sub-category and SAVE button on the navigation bar.

That's my save method:

- (void)saveMoney:(id)sender
{    
    AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext * context = [applicationDelegate managedObjectContext];

    _money = (Money*) [NSEntityDescription insertNewObjectForEntityForName:@"Money" inManagedObjectContext:context];

    double expenseAmt = [_amountTxtField.text doubleValue];

    NSDate *expenseDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@",currentDateLbl.text]];

    _money.vendor = vendorTxtField.text;
    _money.cat = categoryLbl.text;
    _money.subcat = subCategoryLbl.text;
    _money.amount = [NSNumber numberWithDouble:expenseAmt];
    _money.date = expenseDate;
    _money.photo = _templateImgView.templateImage.image;
    _money.notes = notesLbl.text;
    _money.paidBy = paidResourceLbl.text;

    NSError * error = nil;
    [context save:&error];

    [self.navigationController dismissModalViewControllerAnimated:YES];

}

This saves the new expense/income to the table-view and I get a mix of the results(expenses/income) in my table view. But how will I know if my row is an expense or an income. I think a fetch request should make it work? But where this fetch request should be placed, in what method? I tried using the following code:

AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; NSManagedObjectContext * context = [applicationDelegate managedObjectContext];

// Retrieve the entity from the local store -- much like a table in a database
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Money" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setEntity:entity];
[request setIncludesSubentities:YES];

// Set the sorting -- mandatory, even if you're fetching a single record/object
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release]; sortDescriptors = nil;
[sortDescriptor1 release]; sortDescriptor1 = nil;

NSError * error;

NSArray * objects = [context executeFetchRequest:request error:&error];

for (Money* money in objects)
{
    NSLog(@"money class");

    if([money isKindOfClass:[Expense class]])
    {
        NSLog(@"expense class");
    }
    else
    {

    }
}

[context save:&error];

This never prints the "expense class", doesn't go there at all. I don't know how this shall work and in what method I shall place this fetch request.

Please help me out here. I will appreciate it.

Thank you

1

1 Answers

0
votes

When you create your entities you should use Expense or Income as the entity name, e.g.:

[NSEntityDescription insertNewObjectForEntityForName:@"Expense" inManagedObjectContext:context];

Then when you fetch using the entity name 'Money' it'll pull out the sub-entities as you're expecting.