Re: NSManagedObject custom class & for loop
Re: NSManagedObject custom class & for loop
- Subject: Re: NSManagedObject custom class & for loop
- From: email@hidden
- Date: Mon, 29 Sep 2008 08:42:35 -0500 (CDT)
- Importance: Normal
> Hi folks,
> In my core data app I have an Account entity. I've subclassed this as
> I want to add a method that calculates the Account's balance by doing
> a fetch on "Transaction" entities and tallying up the amounts. I've
> created a custom class in xcode and added the following code in
> Account.m
>
> #import "Account.h"
>
> @implementation Account
>
> @dynamic name;
> @dynamic envelopeVisible;
> @dynamic accounts;
> @dynamic out;
> @dynamic type;
> @dynamic in;
> @dynamic envelope;
>
> //custom stuff
> - (NSNumber *)balance
> {
> // we fetch all the transactions our account is related to and tally
> up the balance
> NSNumber *balance;
> NSManagedObjectContext *moc = [self managedObjectContext];
> NSFetchRequest *request = [[NSFetchRequest alloc] init];
> NSEntityDescription *entity = [NSEntityDescription
> entityForName:@"Transaction" inManagedObjectContext:moc];
> [request setEntity:entity];
> NSPredicate *predicate = [NSPredicate predicateWithFormat:@"toAccount
> = %@", self];
> [request setPredicate:predicate];
> NSError *error = nil;
>
> NSArray *transactions = [moc executeFetchRequest:request error:&error];
> if (transactions == nil){
> NSLog(@"Account balance fetch returned nil");
> }
> NSLog(@"Account balance for %@ counted %d transactions",[self name],
> [transactions count]);
> //iterate through transactions and tally balance
> NSManagedObject *transaction;
> for (transaction in transactions){
> NSNumber *amount = [entity amount];
> balance = [NSNumber numberWithFloat:[balance floatValue] - [amount
> floatValue]];
> }
>
> return balance;
> }
> @end
>
> The problem is when the code hits the for (transaction in
> transactions) loop it dumps to GDB with an obj_msgsend error and I
> can't see why. Am I trying to do something that a custom class
> shouldn't be able to do? If I comment out the loop it runs fine.
In the first line of your loop you're sending the -amount message to the
entity, not the transaction object.
That line should probably read NSNumber *amount = [transaction amount]
A few other things though, with more than a few transactions this will be
really slow. You could request the sum of the transaction amounts directly
from CoreData and it could handle them much quicker. Something like
[account valueForKeyPath:@"email@hiddent"] assuming you've set
up a "transactions" relationship. If you don't have a relationship you
could still perform your fetch and issue a similar message to the
transactions array you get here, [transactions
valueForKeyPath:@"@sum.amount"]
If, for some reason, you still need to iterate over all of the objects
individually, I'd recommend that you don't convert your final balance to
an NSNumber until you're finished with your loop.
Ashley Clark
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden