NSFetchRequest returns correct number of objects, but each object contains nil attributes
Posted
by BU
on Stack Overflow
See other posts from Stack Overflow
or by BU
Published on 2010-03-09T01:15:02Z
Indexed on
2010/03/09
1:21 UTC
Read the original article
Hit count: 497
Hi, I can't figure out why this is happening. I can add to the context. But when I retrieve the objects, it returns the correct number of objects but the attributes of the objects are null.
I am adding 3 instances with this code:
+(BOOL)addStoreWithID:(NSNumber *)ID Latitude:(NSNumber *)latitude Longitude:(NSNumber *)longitude Name:(NSString *)name {
Stores *store = (Stores *)[NSEntityDescription
insertNewObjectForEntityForName:@"Stores"
inManagedObjectContext:[[SharedResources instance] managedObjectContext]];
store.ID = ID;
store.Latitude = latitude;
store.Longitude = longitude;
store.Name = name;
NSError *error;
if(![[[SharedResources instance] managedObjectContext] save:&error])
{
//Handle the error
return NO;
}
return YES;
} I get the result: 2010-03-07 19:19:37.060 GamePouch_iPhone[11337:207] Store name is Starbucks (gdb) continue 2010-03-07 19:19:37.933 GamePouch_iPhone[11337:207] Store name is Dunkin Donuts (gdb) continue 2010-03-07 19:19:38.717 GamePouch_iPhone[11337:207] Store name is Krispy Kreme
I have confirmed that this code is visited three times and none of the attributes are nil.
Then when I try to retrieve it, I use the following code:
+(NSMutableArray *)fetchAllObjects {
NSFetchRequest *request;
request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Stores" inManagedObjectContext:[[SharedResources instance] managedObjectContext]];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error;
NSMutableArray *array = [[[SharedResources instance] managedObjectContext] executeFetchRequest:request error:&error];
[request release];
[sortDescriptor release];
[sortDescriptors release];
for(int i=0;i<3;i++)
{
Stores *tempStore = (Stores *)[array objectAtIndex:i];
NSLog(@"store name is %@",[tempStore Name]);
}
return array;
}
I get the result: 2010-03-07 19:21:00.504 GamePouch_iPhone[11337:207] store name is (null) (gdb) continue 2010-03-07 19:21:01.541 GamePouch_iPhone[11337:207] store name is (null) (gdb) continue 2010-03-07 19:21:02.503 GamePouch_iPhone[11337:207] store name is (null)
Thanks a lot for reading. Any help would be much appreciated.
Thanks Bakhtiyar uddin
© Stack Overflow or respective owner