I have a problem that whenever I’m inserting data using coredata, everything’s going fine. But while retrieving, I’m getting the same object all the time retrieved. I’m inserting objects of actors with multiple attribues like id,name,address etc. in add method, I can see everything getting inserted(which actually I’m retrieving from an xml file). my set methods are like:=
[poi setActorCity:[NSString stringWithFormat:@"%@",[poi1 objectAtIndex:j]]];
where, poi is an object of my managedObjectClass POI1 . Are those a problem? & j index is simply for keeping track of xml values from poi1 array. Please help…
- (void)addEvent
{
[actorsArray removeAllObjects];
NSEntityDescription *entity1 = [NSEntityDescription entityForName:@”POI1″ inManagedObjectContext:self.managedObjectContext];
POI1 *poi = (POI1 *)[NSEntityDescription insertNewObjectForEntityForName:@”POI1″ inManagedObjectContext:managedObjectContext];
for(NSInteger i=0;i<[Actors count];i++)
{
NSMutableArray *poi1=[[NSMutableArray alloc]init];
poi1=[Actors objectAtIndex:i];
for(int j=0;j<[poi1 count];j++)
{
if(j==1)
{
[poi setActorName:[NSString stringWithFormat:@"%@",[poi1 objectAtIndex:j]]];
} //Like this it inserts for every attribute
}
[actorsArray insertObject:poi atIndex:i];
[poi release];
}
[self saveAction]; //saving the managedObjectContext
}
This’ my fetch method…
-(void)fetchResult
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity1 = [NSEntityDescription entityForName:@”POI1″ inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity1];
NSArray *items = [self.managedObjectContext
executeFetchRequest:fetchRequest error:&error];
for(NSInteger k=0;k<[items count];k++)
{
POI1 *_poi=[[POI1 alloc]init];
_poi = [items objectAtIndex:k];
NSString *str=[NSString stringWithFormat:@"%@",[_poi actorName]]; //This' for testing... Shows me same name every time..,
}
[fetchRequest release];
}
It sounds like you have a problem with your fetch. Check your predicate. If it returns the same object the most likely cause is that your predicate is written such that it only finds that one object.
Edit01:
This line is your problem:
Despite the fact that the class called is ‘NSEntityDescription’ this method returns a managed object instance. Right now you create a single
POI1instance and then just keep assigning it different attributes. You’re seeing the same values because you’ve only created, populated and saved one object.Move the object creation inside the loop:
This will create a new
POI1at each pass so that the each element of theActorsarray will have a correspondingPOI1instances containing its data.