I am using core data to store some details. But when I run my app for the second time the contents in the entity gets doubled. and this continues each time I run my app. I tried to delete the objects before I add like this question Core Data: Quickest way to delete all instances of an entity but then the deletion works successfully not the addition part and hence am getting quit in the code (from some other part from where I am accessing the Data base)
I used the below code
-(void)synDBFavSpecies:(NSString *)speciesName:(NSString *)speciesId:(NSString *)Imagename
{
NSManagedObjectContext *context=app.managedObjectContext;
NSError *err;
NSFetchRequest *request1=[[NSFetchRequest alloc]init];
NSEntityDescription *entity1=[NSEntityDescription entityForName:@"FavoriteSpecies" inManagedObjectContext:context];
[request1 setEntity:entity1];
NSArray *result1=[context executeFetchRequest:request1 error:&err];
NSManagedObject *ph1;
for(ph1 in result1 )
{
[context deleteObject:ph1];
NSString *sid= [ph1 valueForKey:@"ID"];
NSLog(@"Sp.ID:%@",sid);
if(sid)
{
[context deleteObject:ph1];
if(![context save:&err])
{
NSLog(@"error");
}
}
}
NSManagedObject *SetDB=[NSEntityDescription insertNewObjectForEntityForName:@"FavoriteSpecies" inManagedObjectContext:context];
[SetDB setValue:speciesName forKey:@"favspecies"];
[SetDB setValue:speciesId forKey:@"ID"];
[SetDB setValue:Imagename forKey:@"speciesimage"];
if(![context save:&err])
{
NSLog(@"error");
}
}
Can any one help me?