Realm.beginwrite() - commitwrite(). fails to save any changes and does not throw an exception!


I am currently batch loading about 2500 records and am using just a single write transaction to wrap the creation of the records and this results in no data being saved at all without an error being thrown.

As soon as I move the transaction to within the outermost loop (50 records) then the data gets saved.

Surely there should be some sort of exception thrown during the commitWrite() call ?


There should be an error, you should wrap it in a do…catch block, so you can check what is the error.
Something like this:

  do {
               //do stuff here
                try self.realm.commitWrite()
            } catch {


I’ll give that a try that


Our app imports several million rows of data, processes and saved them as realm objects, so 2500 records is easily done.

If you haven’t already, take a look at the documentation in the Threading section. It’s not a threading issue but the example code is invaluable when working with large datasets. Be sure to incorporate autoreleasepools and background DispatchQueue’s to keep the app responsive while reading that data in.

if you don’t get the issue resolved, perhaps adding a code snippet to your question would help us spot the issue.


I have not found the issue yet - the record show before the commitWrite() but after the commitWrite() there are no records saved and no exception thrown. It’s also only specific types of record that fail to save. Anyway I don’t have time to figure it out at the moment - but seems strange that simply adding a commitWrite() in the outer loop fixes the problem. The whole job is running in an Operation in a background queue so no interference with the main thread - and my queries to check what is in the database are all from the same background thread.