Search code examples
androidrealmrealm-mobile-platform

How to know whether realm.commitTransaction() succefull


In my application I'm inserting user like below

realm.beginTransaction();
realm.copyToRealmOrUpdate(user);
realm.commitTransaction();

I want to display and message if data inserted correctly. How to check if data insertion works successfully or not.

I know if I use realm.executeTransactionAsync() method I can get callback for onSuccess() and onError(). But It does not make any sense to insert one object asynchronously just to get access in to onSuccess().


Solution

  • The line realm.copyToRealmOrUpdate(user); will return an object of type user which is managed by realm signifying that it got inserted. If that object is null that would imply that the transaction did not succeed in which case it would have crashed with an exception.