0
votes

I Declared email as primary key in a Realm model.

+ (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];
}

After entering a duplicate email app crashes.

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

How to user Primary key in Realm?

How to prevent this crash?

Please help me.

2
put some code ... where does crash occurs put that code - EI Captain v2.0
You have to handle that exception with try...catch - EI Captain v2.0
Actually i need a response like "Email already exist". But app crash reason with 'Can't set primary key property 'email' to existing value . - Vignesh
catch (NSException *exception) { NSLog(@"email already exists in Database"); if ([realm inWriteTransaction]) { [realm cancelWriteTransaction]; } } catch part not executing. - Vignesh

2 Answers

2
votes

use try... catch to handle exception like

    RLMRealm *realm = [RLMRealm defaultRealm];
    @try {
             [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 addOrUpdateObject:employeeInfo];
             [realm commitWriteTransaction];
     }
     @catch (NSException *exception) {
         NSLog(@"exception");
         if ([realm inWriteTransaction]) {
            [realm cancelWriteTransaction];
         }
     }

As suggested by @Konstantin, you can also use [realm addOrUpdateObject:employeeInfo]; to update your data with primary key...

1
votes

Primary key means that this is unique key though all entities. If keys are not unique, Realm crashes.

That is documentation: Override +primaryKey to set the model’s primary key. Declaring a primary key allows objects to be looked up and updated efficiently and enforces uniqueness for each value. Once an object with a primary key is added to a Realm, the primary key cannot be changed.

In your case it's better to create new entity in realm and do migration to this new entity.

Or you can just do dirty hack: add new property and write emails in it, and in email (which is Primary Key) write any unique value.