I'm using Realm and I really miss the feature of deleting objects using thier Ids, the way we've right now is:
realm.write(() => {
// Create a book object
let book = realm.create('Book', {id: 1, title: 'Recipes', price: 35});
// Delete the book
realm.delete(book);
// Delete multiple books by passing in a `Results`, `List`,
// or JavaScript `Array`
let allBooks = realm.objects('Book');
realm.delete(allBooks); // Deletes all books
});
But I see that it'd be handy to delete by just passing the id:
realm.write(() => {
// Create a book object
let book = realm.create('Book', {id: 1, title: 'Recipes', price: 35});
// Get the book id
let bookId = book.id;
// Delete the book
realm.delete('Book', bookId);
// or
realm.delete(bookId);
});
Agreed, this would be useful.
Right now the easiest way to do this:
realm.delete(realm.objectForPrimaryKey('Book', bookId));
When I do realm.delete(realm.objectForPrimaryKey('Book', bookId));
I get this error https://github.com/realm/realm-js/issues/1031
Excellent @alazier your solution has been worked
@lossen Could you please this
`
realm.write( () => {
realm.delete(realm.objectForPrimaryKey('Book', bookId));
})
`
I am getting below error when I am using objectForPrimaryKey to delete.
realm.write(() => {
realm.delete(realm.objectForPrimaryKey(
PostSchema.schema.name,
currentPostItem?.id || 0,
));
});
Error: Argument to 'delete' must be a Realm object or a collection of Realm objects.
md5-1ebdf66080fcabde9176d44e80bd19fc
const currentDeletedItem = realm
.objects(PostSchema.schema.name)
realm.write(() => {
realm.delete(currentDeletedItem);
});
Right now the easiest way to do this:
realm.delete(realm.objectForPrimaryKey('Book', bookId));
After I upgrade realm from 2.29.2 to 5.0.3, this piece of code don't works now( I just did the same thing in my project, it works fine all the time until I upgrade realm to 5.0.3), just like @saravanakumargn mentioned above there is an error occurred 'Argument to 'delete' must be a Realm object or a collection of Realm objects' , I called object.isValid() and the return value is true, but when I pass this object to realm.delete, error occurred. @alazier do you have any idea about this?
I am getting below error when I am using objectForPrimaryKey to delete.
realm.write(() => { realm.delete(realm.objectForPrimaryKey( PostSchema.schema.name, currentPostItem?.id || 0, )); });Error: Argument to 'delete' must be a Realm object or a collection of Realm objects.So my solution is
const currentDeletedItem = realm .objects(PostSchema.schema.name) realm.write(() => { realm.delete(currentDeletedItem); });
hi @saravanakumargn ,do you have any idea why we can't use objectForPrimaryKey to delete object now?
Right now the easiest way to do this:
realm.delete(realm.objectForPrimaryKey('Book', bookId));
@alazier What if I have to delete multiple items having same ids?
@TaraSinghDanu
If the id is a primary key, per definition you cannot have multiple objects. For the case where id is not a primary key, you can do something like:
realm.delete(realm.objects('Book').filtered("id = $1", id);
Most helpful comment
Right now the easiest way to do this: