Realm-cocoa: reason: 'Can only delete an object from the Realm it belongs to.'

Created on 25 Apr 2016  Â·  3Comments  Â·  Source: realm/realm-cocoa

Goals

What do you want to achieve?

Expected Results

What did you expected to happen?

Actual Results

What did happened instead?
e.g. the stack trace of a crash

Steps to Reproduce

What are steps we can follow to reproduce this issue?

Code Sample

Provide a code sample or test case that highlights the issue.
If relevant, include your model definitions.
For larger code samples, links to external gists/repositories are preferred.
Alternatively share confidentially via mail to [email protected].
Full Xcode projects that we can compile ourselves are ideal!

Version of Realm and Tooling

In the CONTRIBUTING guidelines, you will find a script,
which will help determining these versions.

Realm version: 0.98.6

Xcode version: 9.3

iOS/OSX version: ios

Dependency manager + version: cocopod 1.0bate

RMUserInfo *rm_userInfo = [[RMUserInfo alloc] initWithWKUserInfo:userInfo];
[kRealmManager transactionWithBlock:^{
[kRealmManager deleteObject:rm_userInfo];
}];

。。。。。。。。。
// cheeseBook stored in Realm

// Delete an object with a transaction
[realm beginWriteTransaction];
[realm deleteObject:cheeseBook];
[realm commitWriteTransaction];

I'll have to table to find object by ID ??

According to the primary key can be deleted??

T-Help

Most helpful comment

@menhui222 Use objectForPrimaryKey: method to retrieve object by primary key. Then pass the object to the deleteObject: method. So the idiom for deleting by primary key is like the following:

[realm deleteObject:[RMUserInfo objectForPrimaryKey:primaryKey]];

All 3 comments

//RMUserInfo *rm_userInfo = [[RMUserInfo alloc] initWithWKUserInfo:userInfo];
RLMResults *results = [RMUserInfo objectsWhere:[NSString stringWithFormat:@"userId == '%@'", userInfo.userId]];
//primaryKey: @"userId"
RMUserInfo *rm_userInfo = results.lastObject;
[kRealmManager transactionWithBlock:^{
[kRealmManager deleteObject:rm_userInfo];
}];

This is right

but deleted a object by primary key ?

@menhui222 Use objectForPrimaryKey: method to retrieve object by primary key. Then pass the object to the deleteObject: method. So the idiom for deleting by primary key is like the following:

[realm deleteObject:[RMUserInfo objectForPrimaryKey:primaryKey]];

thank you!

Was this page helpful?
0 / 5 - 0 ratings