You should not use an explicit autocomplete pool every time. This is more relevant for scenarios where you perform many concurrent transactions and can easily run the risk of tracking many immediate versions. Or, if you want you to close the Realm file for the duration of your application, releasing all open accessors for it.
Documents at this moment are more understood as an awareness of technical limitations and a hint on how to solve this as soon as you encounter a problem such as general best practice. Of course, you can always do this, and it will not necessarily hurt you (à la "If you have a hammer, everything looks like a nail"), but you do not have to do it.
An additional complication of what exactly this means is not required for everyone. Understanding explicit autocomplete pools requires a deeper understanding of ARC, which is not a general requirement. If you have ideas on how to solve this better, your feedback is more than welcome.
The Using Realm Through Threads section provides an example for this, putting a million objects in the background:
dispatch_async(queue) { autoreleasepool { // Get realm and table instances for this thread let realm = try! Realm() // Break up the writing blocks into smaller portions // by starting a new transaction for idx1 in 0..<1000 { realm.beginWrite() // Add row via dictionary. Property order is ignored. for idx2 in 0..<1000 { realm.create(Person.self, value: [ "name": "\(idx1)", "birthdate": NSDate(timeIntervalSince1970: NSTimeInterval(idx2)) ]) } // Commit the write transaction // to make this data available to other threads try! realm.commitWrite() } } }
As a rule, it makes sense to create objects in such quantities as it is in a separate autoreleasepool, since you cannot predict with ARC when the release of the object will occur, and therefore you have an explicit point in time when they will be the last, which makes your program more deterministic to you and other people.
source share