Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8547607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:18:07+00:00 2026-06-11T13:18:07+00:00

I have a simple data model (Core Data), but here is a stripped down

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:18:08+00:00Added an answer on June 11, 2026 at 1:18 pm

    Here is what my research has led me to:

    - (NSNumber *)currentBalance
    {
        // credits
        NSPredicate *creditPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            return [((Transaction *)evaluatedObject).type intValue] == kTransactionTypeCredit;
        }];
    
        NSSet *credits = [self.transactions filteredSetUsingPredicate:creditPredicate];
    
        NSDecimalNumber *totalCredits = [credits valueForKeyPath:@"@sum.amount"];
    
        // debits
        NSPredicate *debitPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            return [((Transaction *)evaluatedObject).type intValue] == kTransactionTypeDebit;
        }];
    
        NSSet *debits = [self.transactions filteredSetUsingPredicate:debitPredicate];
    
        NSDecimalNumber *totalDebits = [debits valueForKeyPath:@"@sum.amount"];
    
        // do the math
        return [totalCredits decimalNumberBySubtracting:totalDebits];
    }
    

    Whilst it works, it seems like an excessive amount of work. Is it the best way?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple Core data Model comprising of a single Gallery which
I have an ipad application that has a simple core data model with two
I have a simple data model of two tables, email and recipients, email can
I'm new to WCF data services. I have a quite simple data model. Some
I have fit a simple bi-variate VAR model to this data set and I
I have a Core Data model with an NSDate property. I want to filter
I have a simple has many relationship between 2 entities in Core Data: Team
I have a simple Document based Core Data app (built around the standard Apple
I have a relationship in a Core Data model that feels like it wants
I'm familiar with Core Data basics and have done some dabbling, but have not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.