BossBols
BossBols

Reputation: 175

Custom method in Managed Object for Core data predicate

I want to perform a predicate based on a stored attribute's (NSDate) month. I understand that comparing a start and end date (manually set) allows this.

But I feel like a custom method in the Managed Object's class should allow this fairly easily as well? Well, I tried, but I can't seem to access a simple method by having the predicate fire it. Is this in any way possible?

Managed Object files:

.h file:

@property (nonatomic,retain) NSNumber *monthOfDate;
-(NSNumber *)monthOfDate;

.m file:

@synthesize monthOfDate;

-(NSNumber *) monthOfDate
{
NSDate *date = [self start];
NSDateComponents *components = [[NSCalendar currentCalendar]components: NSCalendarUnitMonth   fromDate:date];
NSNumber *month = [NSNumber numberWithInteger:[components month]];
return month;
}

This is my predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"client == %@ AND monthOfDate == %d", clientMO,int 6];

It returns an error: keypath monthOfDate not found in entity. I presume because it's not an actual attribute? How can I make it detectable?

Update

The .h and .m files above are from the auto-generated Event class (NSmanaged object). I might move my custom code to a category later but so far no db model revisions are necessary.

Below is the FetchedResultsController (in a uiviewcontroller class) setup with mentioned predicate:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sortfix = [[NSSortDescriptor alloc]initWithKey:@"timeStamp" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortfix,nil]];
[fetchRequest setFetchBatchSize:20];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"client == %@ AND monthOfDate == %d", clientMO,int 6];
[fetchRequest setPredicate:predicate];


NSFetchedResultsController *theFetchedResultsController =

[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:managedObjectContext sectionNameKeyPath:sectionKeyPathProperty
                                               cacheName:nil];

//sectionKeyPathProperty is a variable that let's me choose one of multiple transient properties I have created for creating relevant sections.

self.fetchedResultsControllerClient = theFetchedResultsController;
_fetchedResultsControllerClient.delegate = self;

return _fetchedResultsControllerClient;

Upvotes: 0

Views: 488

Answers (1)

BossBols
BossBols

Reputation: 175

This turns out to only be possible when the MO's have already been loaded into memory. (fetched)

In my case with a FRC this is not the case thus I will have to create a separate month attribute that is saved on MO creation.

Thanks to user Run Loop for responding on a 4 year old question!

Upvotes: 1

Related Questions