title: Having only one instance of an entity in Core Data
body: <p>I am making an application where you need to log in with a 4 digit password but there can only be one password at a time. I am trying to save it to core data but whenever the user adds a new password it just adds it to the long list. How can I restrict an entity to only have one instance of itself?</p>
  
  <p>Here is my code just in case it will help:</p>
  
  <pre><code>-(BOOL)savePassword:(NSString*)password{
      AppDelegate * appDelegate = [[AppDelegate alloc]init];
      NSManagedObjectContext * context = [appDelegate managedObjectContext];
      AppData * appData = (AppData*)[NSEntityDescription insertNewObjectForEntityForName:@"AppData" inManagedObjectContext:context];
      appData.password = password;
      NSError *error;
      if (![context save:&amp;error]) {
          NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
      }
  
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"AppData" inManagedObjectContext:context];
      [fetchRequest setEntity:entity];
  
      NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&amp;error];
      if (fetchedObjects == nil) {
          NSLog(@"There was an error:%@",error);
      }
  
      for (AppData * adata in fetchedObjects) {
          NSLog(@"Password:%@",adata.password);
      }
      return YES;
  }
  </code></pre>
  
  <p>Thanks!</p>