2017-02-09 66 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:之前檢索現有對象。