Search code examples
iosobjective-crealm

find if there is a new object by createOrUpdateInRealm in realm


Is there any way to find out that a new object is inserted by createOrUpdateInRealm method or not?

RLMRealm *realm = [RLMRealm defaultRealm];
SampleRealmObject *object     = [[SampleRealmObject alloc] init];
NSDictionary *item = @{@"id": @10, @"name": @"sampleName"};

for (RLMProperty *property in object.objectSchema.properties) {
    if([item objectForKey:property.name])
      {
            [object setValue:[item objectForKey:property.name] forKey:property.name];
       }
    }

// add or update the new object
[SampleRealmObject createOrUpdateInRealm:realm withValue:object];

Solution

  • No, +createOrUpdateInRealm:withValue: does not indicate whether the object was created or updated. If you need to know whether an object with the given primary key exists, you can use +objectInRealm:forPrimaryKey: to retrieve an existing object prior to using +createOrUpdateInRealm:withValue:.