Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm slow on updating multiple objects

Tags:

ios

swift

realm

In my app the user can select multiple contacts in a collectionview. when he selects the property "isSelected" will me set to true and the collectionview refreshes the selected cell. Here I can recognize a small delay between selection and the highlighting of the cell. But in the next step I create a group with the selected contacts and in the end I set the property "isSelected" to false. This takes an non acceptable amount of time for 50 objects (5 seconds) and needs to be tuned.

Here is my code to unselect all the selected contacts:

for contact in self.selectedContacts {
            try! self.realm.write{
                contact.isSelected = false;
                self.realm.add(contact, update: true)
            }
        }

Is it possible to perform a batch-update at once?

like image 696
netshark1000 Avatar asked Oct 31 '15 12:10

netshark1000


1 Answers

Try putting the for loop inside the write block:

try! self.realm.write {
    for contact in self.selectedContacts {
        contact.isSelected = false;
        self.realm.add(contact, update: true)
    }
}
like image 57
joern Avatar answered Oct 07 '22 23:10

joern