I have a simple data model (Core Data), but here is a stripped down version of it:
Account
----------------------------------------------------
NSSet<Transaction> transactions
Transaction
----------------------------------------------------
Account account
NSNumber amount
NSNumber type ( type ∈ ( 0->'Credit', 1->'Debit' ) )
I want to get an account’s total balance, however, I don’t want to mark a debit as a negative number. I just want it to be an amount with a type of debit.
Currently, before I added the debits in, I could get the balance via this function in Account:
- (NSNumber *)currentBalance
{
return [self.transactions valueForKeyPath:@"@sum.amount"];
}
Obviously, that will only work if I make debits negative. Is there an elegant solution here, to perhaps filter the set into credits and debits, sum those, and perform a difference? Will that maintain accuracy? I’ve looked into NSPredicate, but I’m not sure how exactly to proceed.
Here is what my research has led me to:
Whilst it works, it seems like an excessive amount of work. Is it the best way?