The area is slow when updating multiple objects

In my application, the user can select multiple contacts in the collection. when he selects the "isSelected" property, I will be set to true, and collectionview will update the selected cell. Here I can recognize a slight delay between the selection and the highlight of the cell. But in the next step, I create a group with the selected contacts, and in the end I set the "isSelected" property to false. This takes an unacceptable amount of time for 50 objects (5 seconds) and needs to be configured.

Here is my code to deselect all 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 immediately?

+6
source share
2 answers

Try putting a for loop inside a write block:

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

You must add the switching selection logic outside the recording block. to speed up the process of updating Realm.

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

All Articles