NSFetchRequest with NSPredicate not fetches the expected objects (iPhone)
NSFetchRequest with NSPredicate not fetches the expected objects (iPhone)
- Subject: NSFetchRequest with NSPredicate not fetches the expected objects (iPhone)
- From: Giannandrea Castaldi <email@hidden>
- Date: Wed, 24 Feb 2010 09:21:40 +0100
Hi,
I'm developing an atom feed reader for iPhone and I'm trying to
execute a NSFetchRequest with a NSPredicate in a test where the result
should be an array with two objects but instead is emtpy.
There are two entities: Feed (Atom feed) and Entry. In the model Entry
has a relationship with one Feed and in Feed I have defined a selector
that executes an NSFetchedRequest.
I've defined the model in code then this is the definition of Feed description:
+ (NSEntityDescription*)entityDescription {
NSEntityDescription *feedEntityDescription = [[NSEntityDescription
alloc] init];
[feedEntityDescription setName:@"Feed"];
[feedEntityDescription setManagedObjectClassName:@"Feed"];
NSMutableArray *properties = [NSMutableArray array];
NSAttributeDescription *title = [[NSAttributeDescription alloc] init];
[properties addObject:title];
[title release];
[title setName:@"title"];
[title setAttributeType:NSStringAttributeType];
[title setOptional:NO];
NSAttributeDescription *feedId = [[NSAttributeDescription alloc] init];
[properties addObject:feedId];
[feedId release];
[feedId setName:@"feedId"];
[feedId setAttributeType:NSStringAttributeType];
[feedId setOptional:NO];
NSAttributeDescription *updateTime = [[NSAttributeDescription alloc] init];
[properties addObject:updateTime];
[updateTime release];
[updateTime setName:@"updateTime"];
[updateTime setAttributeType:NSStringAttributeType];
[updateTime setOptional:NO];
[feedEntityDescription setProperties:properties];
return feedEntityDescription;
}
and this that of Entry:
+ (NSEntityDescription*)entityDescriptionWithFeed:(NSEntityDescription*)feedDescription
{
NSEntityDescription *entityDescription = [[NSEntityDescription alloc] init];
[entityDescription setName:@"Entry"];
[entityDescription setManagedObjectClassName:@"Entry"];
NSMutableArray *properties = [NSMutableArray array];
NSAttributeDescription *text = [[NSAttributeDescription alloc] init];
[properties addObject:text];
[text release];
[text setName:@"text"];
[text setAttributeType:NSStringAttributeType];
[text setOptional:NO];
NSAttributeDescription *authorUsername = [[NSAttributeDescription
alloc] init];
[properties addObject:authorUsername];
[authorUsername release];
[authorUsername setName:@"authorUsername"];
[authorUsername setAttributeType:NSStringAttributeType];
[authorUsername setOptional:NO];
NSAttributeDescription *publishTime = [[NSAttributeDescription alloc] init];
[properties addObject:publishTime];
[publishTime release];
[publishTime setName:@"publishTime"];
[publishTime setAttributeType:NSDateAttributeType];
[publishTime setOptional:NO];
NSRelationshipDescription *feed = [[[NSRelationshipDescription
alloc] init] autorelease];
[properties addObject:feed];
[feed setName:@"feed"];
[feed setDestinationEntity:feedDescription];
[feed setMinCount:1];
[feed setMaxCount:1];
[feed setOptional:NO];
[feed setDeleteRule:NSNullifyDeleteRule];
[entityDescription setProperties:properties];
return entityDescription;
}
This is the selector in Feed that should load the entries:
-(NSArray*)sortedEntries {
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Entry" inManagedObjectContext:[Persistence
singleton].managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
// NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"feed.feedId like %@", self.feedId];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"feed ==
self", self];
[request setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"publishTime" ascending:NO];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];
[sort release];
NSError *error;
NSArray *result = [[Persistence singleton].managedObjectContext
executeFetchRequest:request error:&error];
if (!result) {
NSLog(@"error: %@", [error localizedDescription]);
}
return result;
}
This is my failing test:
- (void) testEntityRelationshipWithEntries {
[Persistence initSingletonWithDataStoreName:@"testEntityRelationshipWithEntries"];
persistence = [Persistence singleton];
[persistence resetDataStore];
Feed *feed01 = [Feed feed];
feed01.title = @"feed01";
feed01.feedId = @"id01";
feed01.updateTime = @"update time";
[persistence commit];
feed01 = [persistence findFirstOfEntity:@"Feed" predicate:@"title
like[cd] 'feed01'"];
STAssertEquals(feed01.sortedEntries.count, 0u, @"Intially shouldn't
have entries");
Entry *entry01 = [self entryWithContent:@"gcastaldi71: Hello world!"
publishTime:[NSDate dateWithTimeIntervalSinceNow:-1000.0]];
Entry *entry02 = [self entryWithContent:@"gcastaldi71: Hello world
2!" publishTime: [NSDate dateWithTimeIntervalSinceNow:-2000.0]];
[feed01 addEntry:entry01];
[feed01 addEntry:entry02];
[persistence commit];
NSArray *result = [NSArray arrayWithObjects:entry01, entry02, nil];
STAssertEqualObjects(feed01.sortedEntries, result, @"Should contains
the just added entries");
}
Note for the test:
- code and test use the same context in the Persistence singleton
- with [persistence commit] is executed a context save
I've verified at the end of my test the db and there are the data correctry:
sqlite> select * from zentry;
1|1|1|1|288677024.234666|gcastaldi71|Hello world!
2|1|1|1|288676024.240294|gcastaldi71|Hello world 2!
sqlite> select * from zfeed;
1|2|1|update time|feed01|id01
sqlite> .schema
CREATE TABLE ZENTRY ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT
INTEGER, ZFEED INTEGER, ZPUBLISHTIME TIMESTAMP, ZAUTHORUSERNAME
VARCHAR, ZTEXT VARCHAR );
CREATE TABLE ZFEED ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT
INTEGER, ZUPDATETIME VARCHAR, ZTITLE VARCHAR, ZFEEDID VARCHAR );
...
Any suggestion?
Thanks.
Jean
_______________________________________________
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