k-lpmg / RealmWrapper

Safe and easy wrappers for RealmSwift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Occurring UI lag while insertion process

batuhansk opened this issue ยท comments

Hi @k-lpmg!

I'm trying to insert a batch of data (~10k, 15k). UI gets laggy while insertion process, and i'm using the different thread instead of main thread.

Some details about getting lag of UI:

Bad scroll experience of sliding table / collection view. UI is not freezing entirely. It's happening for a second, really short time but affects UI experience.

You can see in below;

ekran resmi 2019-02-20 02 42 12

ekran resmi 2019-02-20 02 39 25

and last one;

i'm calling that method like that;

ekran resmi 2019-02-20 02 43 23

Hi @strawb3rryx7!
Thank you very much for finding this issue.

Issue

transaction function in RealmManageable is calling refresh of the realm.
This code is being called on the completionQueue parameter of the transaction.
The default value of completionQueue is set to main queue.

Since the transaction of realm runs in writeQueue, if you set writeQueue to non main queue, it does not affect UI, but after the transaction is done, refresh is executed in completionQueue, so UI stopped for a very short time.

Solution

I take this issue seriously and will release the 1.2.0 version after fixing the issue through testing.
Until 1.2.0 is released, in 1.1.4 version, completionQueue will also fix the UI hang for a moment if you put non main queue.

func insertAll(_ friendships: [Friendship]) {
    rm.transaction(writeQueue: DispatchQueue.realmBg, isSync: false, writeHandler: { (realm) in
        realm.add(friendships, update: true)
    }, completionQueue: DispatchQueue.realmBg)
}

Thanks for your quick action @k-lpmg โœ‹๐Ÿผ I applied your hot fix. Then I'll check out when it'll be released, and there is an another issue but I'm not sure is it related with this issue.

Realm file size getting large quickly, when these batch insertions are done, I'm checking the documents & file size from General -> iPhone Storage, I see 40 MB and above.

c5fcc27e-841c-4732-aff8-7021713d73a9

Bump! I've been a chance to try your hotfix now. Also the problem is still exists.

UPDATE:

I called the insertAll(_:) method in DispatchQueue.realmBg.async. It looks fine right now. But I'm not sure is it best way to handle it.

I released version 1.2.0 which is a revised version of this issue.
In addition, the function to insert multiple realm objects at once is added to Example.

You no longer need to set the completionQueue in your insertAll method because of this issue. ๐Ÿ˜

Thanks for your all effort. Do you have any idea about database size which I mentioned in above? ๐Ÿ˜Š

Sorry. I have no idea about database size. ๐Ÿ˜