What is the difference between Java – insert() and createObject()?

What is the difference between Java – insert() and createObject()? … here is a solution to the problem.

What is the difference between Java – insert() and createObject()?

I have a setChatsList() method and it has a huge code:

public void setChatsList(final ChatsModel chatsModel) {
    Realm realm = Realm.getDefaultInstance();

realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(@NonNull Realm realm) {

ChatsModel realmChats = realm.createObject(ChatsModel.class);
            Response realmResponse = realm.createObject(Response.class);
            Item realmItem = realm.createObject(Item.class);
            Message realmMessage = realm.createObject(Message.class);
            Attachment realmAttachment = realm.createObject(Attachment.class);
            Video realmVideo = realm.createObject(Video.class);

Response response = chatsModel.getResponse();
            RealmList<Item> items = new RealmList<>();
            Integer itemCount = response.getCount();
            RealmList<Item> itemList = response.getItems();

if (itemList != null) {
                for (Item item : itemList) {
                    Message message = item.getMessage();

realmMessage.setId(message.getId());
                    realmMessage.setDate(message.getDate());
                    realmMessage.setOut(message.getOut());
                    realmMessage.setUserId(message.getUserId());
                    realmMessage.setReadState(message.getReadState());
                    realmMessage.setTitle(message.getTitle());
                    realmMessage.setBody(message.getBody());
                    realmMessage.setRandomId(message.getRandomId());
                    RealmList<Attachment> attachments = message.getAttachments();
                    RealmList<Attachment> attachmentList = new RealmList<>();

if (attachments != null) {
                        for (Attachment attachment : attachments) {
                            String type = attachment.getType();
                            Video video = attachment.getVideo();

realmVideo.setAccessKey(video.getAccessKey());
                            realmVideo.setCanAdd(video.getCanAdd());
                            realmVideo.setCanEdit(video.getCanEdit());
                            realmVideo.setComments(video.getComments());
                            realmVideo.setDate(video.getDate());
                            realmVideo.setDescription(video.getDescription());
                            realmVideo.setDuration(video.getDuration());
                            realmVideo.setId(video.getId());
                            realmVideo.setOwnerId(video.getOwnerId());
                            realmVideo.setPhoto130(video.getPhoto130());
                            realmVideo.setPhoto320(video.getPhoto320());
                            realmVideo.setPhoto640(video.getPhoto640());
                            realmVideo.setPlatform(video.getPlatform());
                            realmVideo.setTitle(video.getTitle());
                            realmVideo.setViews(video.getViews());

realmAttachment.setType(type);
                            realmAttachment.setVideo(realmVideo);

attachmentList.add(realmAttachment);
                        }
                        realmMessage.setAttachments(attachmentList);
                    }

realmResponse.getItems().add(item);
                }
            }

realmResponse.setCount(itemCount);
            realmChats.setResponse(realmResponse);
        }
    });
}

Works fine!

Just read the official documentation about the method insert(), which is also used for storage in the database. I overridden the setChatsList() method:

public void setChatsList(final ChatsModel chatsModel) {
    Realm realm = Realm.getDefaultInstance();
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(@NonNull Realm realm) {
            realm.insert(chatsModel);
        }
    });
}

To my surprise, it works just fine and has less code!

But I’m sure that not everything is going so smoothly, I think there is something wrong somewhere.

Question: What is the difference between insert() and createObject()?

Solution

insert()

Save unmanaged objects to Realm (managed objects are empty operations) without creating a managed agent object as a return value.

createObject()

Create a managed object in Realm and return the managed object’s proxy.

copyToRealm()

Save the unmanaged object to Realm, returning the proxy to the managed object you created.


The main difference between insert() and copyToRealm() is whether or not to return a proxy; This means that inserting many items is more efficient by reusing a single unmanaged object and calling insert() on it with the correct parameters.

However, if you want to establish relationships between objects, you usually need createObject().

P.S. insert/copyToRealm/createObject(clazz) has corresponding insertOrUpdate, copyToRealmOrUpdate, and createObject(clazz, primaryKeyValue) is used for objects with primary keys.

Related Problems and Solutions