0

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?

Community
  • 1
  • 1
priya
  • 305
  • 4
  • 18
  • if i comment the deletion part it works fine. but each time the content gets doubled. when i include deletion only deletion works not addition. so DB becomes empty. – priya Jun 29 '12 at 06:04
  • what are you trying to achieve in `for` loop – Saurabh Passolia Jun 29 '12 at 10:41
  • It would be easier to guess the cause of the problem if you gave information about the error. Use @try/@catch around the code that's failing and log the exception description and it's callStackSymbols. – Phillip Mills Jun 29 '12 at 10:43
  • @samfisher I think the `for` loop allows him to delete duplicates. But I think it could be cleaner to do what I wrote in my answer. – Lorenzo B Jun 29 '12 at 10:46

2 Answers2

0

priya,

First of I would change the

NSLog(@"error");

with a more detail description for the error like:

NSLog(@"My save failed: %@\n%@", [error localizedDescription], [error userInfo]);

Then, does your FavoriteSpecies entity has other relationships with other entitites?

If you want to prevent duplicates you need to handle it in a different way. For example, if your ID is the identifier for your FavoriteSpecies entity, you could set up a fetch request with a predicate and see what it returns. For example:

NSFetchRequest *request1 = [[NSFetchRequest alloc]init];
[request1 setEntity:[NSEntityDescription entityForName:@"FavoriteSpecies" inManagedObjectContext:context]];
[request1 setPredicate:[NSPredicate predicateWithFormat:@"ID == %@", speciesID]];
[request1 setFetchLimit:1]; // it's not necessary to do it since the ID is unique

If you execute the fetch, you will retrieve one or zero objects (if ID is unique)

NSArray *result1 = [context executeFetchRequest:request1 error:&err];
if([result1 count] == 0) {
    // insert here since you haven't that object
} else { 
    // do what you want, for example updating
}

Setting up a predicate is equal to adding the WHERE clause in a SELECT statement.

Hope that helps.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • not sure where i went wrong. I added the deleting code to some other part of my code and now its working fine. thank you very much – priya Jul 02 '12 at 04:21
  • @priya You're welcome. If my asnwer was useful upvote it (if you want). In addition, post your solution as an answer and mark it as answered. Thank you. Cheers. – Lorenzo B Jul 02 '12 at 07:34
0

Actually i made a mistake in my code. The above code was running in aloop. so when i deleted it the insertion was not performing. So i made another method to delete the database and called that method before calling this one. Hence got it working.

priya
  • 305
  • 4
  • 18