How to use Primary key in Realm in iOS (Objective c)
<p>I Declared email as primary key in a Realm model.</p>

<pre><code>+ (NSString *)primaryKey
{
   return @"email";
}


- (void)insertUserWithFirstName:(NSString*)firstname lastName:     (NSString*)lastname email:(NSString*)email address:(NSString*)address gender:(NSString*)gender mobile:(NSString*)mobile department:(NSString*)department
{
   RLMRealm *realm = [RLMRealm defaultRealm];
   [realm beginWriteTransaction];

   Employee *employeeInfo = [[Employee alloc]init];
   employeeInfo.firstName = firstname;
   employeeInfo.lastName = lastname;
   employeeInfo.email = email;
   employeeInfo.address = address;
   employeeInfo.gender = gender;
   employeeInfo.mobile = mobile;
   employeeInfo.department = department;

   [realm addObject:employeeInfo];
   [realm commitWriteTransaction];
}
</code></pre>

<p>After entering a duplicate email app crashes.</p>

<blockquote>
  <p>Terminating app due to uncaught exception 'RLMException', reason: 'Can't set primary key property 'email' to existing value .</p>
</blockquote>

<p>How to user Primary key in Realm?</p>

<p>How to prevent this crash?</p>

<p>Please help me.</p>