I then have a class var that stores the start time so we can determine the write time
var startTime = Date()
and then a function that creates 10,000 Thing objects and stores them in an array, once completed the startTime is populated and then that array is written to Realm. When the write completes, the endTime is calculated and printed to console.
I believe the issue with your code is writing each object separately within a tight loop on the main thread- if you need that functionality (which may be more memory safe) this would work noting it’s an asynchronous write.
Started working after pod update. But I didn’t seen any difference in performance with writeAsync and write. Still getting same results like as mentioned in the beginning
Well, it’s working great for me. I would propose it’s time for a split-half search to eliminate some variables. Please create a brand new macOS XCode project using my podfile from above (changing the project name)
Then copy and paste my Thing object from above and then the code (func writeABunchOfThings) in the new project. I would add a button to run the code itself.
Run the project.
If it works and behaves correctly, something in your original project is causing the issue
If it does not work, then you’ve got an environment issue; maybe a config issue or some other issue - possibly event corrupted files or a hardware problem.
I have followed those steps on three different Macs; a 2019 iMac, 2017 iMac and a 2013 Macbook Pro 15". The results are roughly the same.
My apologies but I am struggling trying to understand what you’re attempting to do with that code.
The .create function creates a single realm object so this
.create([RLMIngredient].self
won’t work as [].self is an array and not a Realm object. It’s unclear what the purpose of that would be even if it did work.
Why? Why do you need an array of dictionaries for? Where does data come from? Is it encoded? Why not just pass data to a Realm object when it’s instantiated, let the object decode it and assign properties and then write it out to Realm?
Your new question is
And the answer is you cannot insert and array of json objects to Realm. You can only insert Realm objects into Realm.
I feel the thread is getting off topic.
The initial issue was Realm write performance. Using my examples above, we’re not able to replicate that issue which would indicate the problem lies elsewhere in your code that was not included.
Working with JSONSerialization, arrays etc are off topic for this discussion and to prevent the thread from being a mile-long, it should probably be posed as another question (it’s really a separate topic)
Happy to help but it’s important to keep threads on a single topic so it may help future readers that are looking for help about this particular subject matter.
So, post a separate question with the code you’re stuck on an we’ll take a look!