It seems that you are mutating messages
in forEach
, this could be the cause of the crash.
Try something like this:
let realm = try Realm() let allMessages = realm.objects(Message.self) let results = allMessages.filter("(state == 1) AND (dateSent <= %@)", dateSent) let messages = Array(results) try realm.write ({ messages.forEach { message in message.state = .seen } })
This may not be the best solution, since you are loading all messages into memory, but should work.
source share