Performing a Fetch Request on a specific MO.
Performing a Fetch Request on a specific MO.
- Subject: Performing a Fetch Request on a specific MO.
- From: Gustavo Pizano <email@hidden>
- Date: Tue, 01 Mar 2011 00:19:59 +0100
Hello.
I come from EOF bg, and i remember it was quite simple to let's say, ask a specific object to give me the max number of a entity which was related to.
To make it clearer.
Enity A has a toMany relation with Entity B.
entity B has a property called versionNumber.
So I want to get the next version number I must set for a new Entity B instance for the specific object, which in fact is to search for the max versionNumber of specificObject.toVersions.
So Inside Entity A class I created a method called nextVersionNumber, then I did the following:
NSInteger vNumber = 0;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:DPA_VERSION_KEY inManagedObjectContext:[self managedObjectContext]];
[request setEntity:entity];
// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:VERSION_NUMBER_KEY];
NSExpression *maxNumberExpression = [NSExpression expressionForFunction:@"max:"
arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxNumber"];
[expressionDescription setExpression:maxNumberExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
// Execute the fetch.
NSError *error = nil;
NSArray *objects = [[self managedObjectContext] executeFetchRequest:request error:&error];
if (objects == nil) {
// Handle the error.
}
else {
if ([objects count] > 0) {
vNumber = [[[objects objectAtIndex:0] valueForKey:@"maxNumber"] integerValue] +1;
}
}
[expressionDescription release];
[request release];
return vNumber;
But of course this will fetch the Version Entity which has the max number, but this returned object may not be one related to the specificObject.
SO I was trying using the keyword self. but then of course the app will complain/crash because self is Invalid keypath element due that Im fetching over an NSEntityDescription not on self.
what am I missing? how can get this required value?
Thx
Gustavo
_______________________________________________
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