Do I need to use an explicit autostart pool when using Realm in GCD, even in ARC

The Realm doc says:

You can also see this issue when accessing Realm using Grand Central Dispatch. This can happen when Realm enters the dispatch queue of the auto-calculation pool, because these pools cannot be depleted for some time after the execution of your code. Intermediate versions of the data in the Realm file cannot be reused until the RLMRealm object is deleted. To avoid this problem, you should use an explicit autostart pool when accessing the kingdom from the dispatch queue.

Does this mean that we should use an explicit autostart pool every time in GCD even in ARC? Can someone send sample code? This is very important, but the official documentation does not particularly emphasize this.

+3
source share
2 answers

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.

+2
source

To avoid this problem, you should use an explicit autostart pool when accessing the kingdom from the dispatch queue.

Does this mean that we should use an explicit autostart pool every time in GCD even in ARC?

I disagree with the current accepted answer, in any background thread (especially threads such as GCD) you should always force close the Realm instance when it is no longer needed to avoid saving the version. In iOS, forcibly closing an instance of Realm is possible using autoreleasepool { ... } .

So for background threads it It is usually recommended that you always use an explicit autostart pool.

 dispatch_async(queue) { autoreleasepool { let realm = try! Realm() //... } } 

It is also advisable to minimize the number of transactions you make from background threads, so instead you should try to have 1 transaction instead of N.

 // Break up the writing blocks into smaller portions // by starting a new transaction realm.beginWrite() for idx1 in 0..<1000 { // 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() 
0
source

Source: https://habr.com/ru/post/1013281/


All Articles