2017-02-09 1 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:は、オブジェクトが作成されたか更新されたかを示しません。特定の主キーを持つオブジェクトが存在するかどうかを知る必要がある場合は、+createOrUpdateInRealm:withValue:を使用する前に、+objectInRealm:forPrimaryKey:を使用して既存のオブジェクトを取得できます。

関連する問題