2017-02-09 64 views
-1

有没有什么方法可以找出createOrUpdateInRealm方法是否插入新对象?通过在领域createOrUpdateInRealm查找是否有新对象

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]; 
+0

使用'id'获取对象,如果不存在则使用'add'或使用'update'。 –

+0

@SachinVas我知道这种方法,但我会问这个方法可能给我们的东西。 –

回答

0

不,+createOrUpdateInRealm:withValue:不指示对象是否被创建或更新。如果您需要知道是否存在具有给定主键的对象,则可以使用+objectInRealm:forPrimaryKey:在使用+createOrUpdateInRealm:withValue:之前检索现有对象。