tags:

views:

79

answers:

3

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];

}

A: 

A little hard to answer precisely with this information, but my educated guess would be that you do not create new poi instance every time and keep adding the same reference to your array.

Vincent Osinga
neha
A: 

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:

POI1 *poi = (POI1 *)[NSEntityDescription insertNewObjectForEntityForName:@"POI1" inManagedObjectContext:managedObjectContext];

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:

for(NSInteger i=0;i<[Actors count];i++)
{
        POI1 *poi = (POI1 *)[NSEntityDescription insertNewObjectForEntityForName:@"POI1" inManagedObjectContext:managedObjectContext];
        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];
}

This will create a new POI1 at each pass so that the each element of the Actors array will have a corresponding POI1 instances containing its data.

TechZen
Please check my edited question, Sir... Thank you...
neha
Dat's great! It worked... Thank you so much...!
neha
Please hit the checkmark next to my answer so everyone will know the questioned was answered.
TechZen
A: 

Make sure that you're saving your managedObjectContext after doing all of those inserts(probably after each insert), otherwise the information will never leave temporary memory.

jessecurry
neha