Re: NSPredicate with CoreData SQL store behavior
Re: NSPredicate with CoreData SQL store behavior
- Subject: Re: NSPredicate with CoreData SQL store behavior
- From: Jesse Grosjean <email@hidden>
- Date: Thu, 16 Mar 2006 15:33:08 -0500
First at some point I "learned" that when fetching on a SQL type
store predicates would only return results from the entities that
were committed to the store. For example if I add a new entity it
would not show up in the fetch results until I saved the store.
This is not the case; the documentation lists the precise
conditions that apply:
<http://developer.apple.com/documentation/Cocoa/Reference/
CoreData_ObjC/Classes/NSManagedObjectContext.html#//apple_ref/occ/
instm/NSManagedObjectContext/executeFetchRequest:error:>
I've created an example where this isn't the case, the fetch will
work correctly after saving the managed object context, but doesn't
work correctly if the changes in the context are not saved. The
failure seems to be related to the fact that my fetch is going across
a relationship. If I fetch directly on the entity that has the
changed attribute then things seems to work as advertised. But for my
application I really need to fetch across relationships because I
want to avoid loading the entity that contains the attribute that I'm
searching on.
I've posted the full example project here:
http://www.hogbaysoftware.com/files/CoreDataFetchTest2.zip
And here's the code that actually tests the problem. This is in a
default "Core Data Application" project with the entities:
Entry
- entryData
EntryData
- title
- entry
- (void)awakeFromNib {
// clear out any old databases
[[NSFileManager defaultManager] removeFileAtPath:[self
applicationSupportFolder] handler:nil];
// insert new entry, entryData into new stack.
NSManagedObject *entry = [NSEntityDescription
insertNewObjectForEntityForName:@"Entry" inManagedObjectContext:[self
managedObjectContext]];
NSManagedObject *entryData = [NSEntityDescription
insertNewObjectForEntityForName:@"EntryData"
inManagedObjectContext:managedObjectContext];
[entry setValue:entryData forKey:@"entryData"];
[managedObjectContext save:nil];
// change value in context
[entryData setValue:@"mytitle" forKey:@"title"];
// try to fetch the changed value
NSError *fetchError = nil;
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init]
autorelease];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Entry"
inManagedObjectContext:managedObjectContext]];
[fetchRequest setPredicate:[NSPredicate
predicateWithFormat:@"entryData.title == %@", @"mytitle"]];
NSArray *results = [managedObjectContext
executeFetchRequest:fetchRequest error:&fetchError];
if ([results count] == 1) {
NSLog(@"OK - found changed value");
} else {
NSLog(@"FAILED - didn't find changed value");
}
}
Thanks and let me know if I just messed something up with this
example, certainly a possibility.
Jesse
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden