Android
Android

Reputation: 65

iOS sqlite storing null value without calling setValue

I am trying to store product in sqlite. When i setValue for database entity then i store correct value.

But problem is that i make a check if product is already in database then it should not add same product again. But next time it store NULL value in database.

Although i am not calling [addToFav setValue:@"My Value"];

Simply its check in for loop. When my loop runs it won't go in first condition where i make counter if(count == 0)

I don't know where from its storing NULL value. While i am not calling it any where else. So how its storing NULL value in database ?

- (IBAction)buttonAddToFavourite:(id)sender {
int count = 0;
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error=nil;
NSManagedObject *addToFav = [NSEntityDescription insertNewObjectForEntityForName:@"Favourite" inManagedObjectContext:context];
NSString *dish = [results objectForKey:@"id"];
fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Favourite" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

for (NSManagedObject *info in fetchedObjects) {
    NSString *id1 = [info valueForKey:@"dishid"];

    if ([id1 isEqualToString:dish]) {
        count = count +1;
    }
}
if (count == 0) {
    [addToFav setValue:[results objectForKey:@"id"] forKey:@"dishid"];
    [CSNotificationView showInViewController:self
                                   tintColor:[UIColor greenColor]
                                       image:[UIImage imageNamed:@"sucess"]
                                     message:@"Saved As Favourite."
                                    duration:2.0f];

    [self.permanentNotification setShowingActivity:YES];

}
else if (count > 0){
    [CSNotificationView showInViewController:self
                                   tintColor:[UIColor redColor]
                                       image:[UIImage imageNamed:@"warning"]
                                     message:@"Dish Already Added."
                                    duration:2.0f];

    [self.permanentNotification setShowingActivity:YES];

       }

if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

}

Upvotes: 1

Views: 255

Answers (1)

Eugene
Eugene

Reputation: 10045

- (IBAction)buttonAddToFavourite:(id)sender {
  NSError *error = nil;
  NSString *dishId = results[@"id"];

  NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Favourite"];
  [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"dishid == %@", dishId]];
  NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

  if (fetchedObjects.count) {
    [CSNotificationView showInViewController:self
                                   tintColor:[UIColor redColor]
                                       image:[UIImage imageNamed:@"warning"]
                                     message:@"Dish Already Added."
                                    duration:2.0f];

    [self.permanentNotification setShowingActivity:YES];
  }
  else {
    Favourite *obj = [NSEntityDescription insertNewObjectForEntityForName:@"Favourite" inManagedObjectContext:self.managedObjectContext];
    obj.dishid = dishId;

    [CSNotificationView showInViewController:self
                               tintColor:[UIColor greenColor]
                                   image:[UIImage imageNamed:@"sucess"]
                                 message:@"Saved As Favourite."
                                duration:2.0f];
    [self.permanentNotification setShowingActivity:YES];

    if (![context save:&error]) {
      NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
  }
}

Upvotes: 1

Related Questions