• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Performing a Fetch Request on a specific MO.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Prev by Date: Function to get current Backtrace and Address Slide
  • Next by Date: Re: Function to get current Backtrace and Address Slide
  • Previous by thread: Re: Function to get current Backtrace and Address Slide
  • Next by thread: Conditionally adding NSButtonCell to a column of NSTableview and allowing button cell to take action even if the corresponding row is not allowed to be selected
  • Index(es):
    • Date
    • Thread