live query modelObject being garbage collected

11 views
Skip to first unread message

Julian Paas

unread,
Feb 7, 2015, 4:00:09 PM2/7/15
to mobile-c...@googlegroups.com
I have an iOS/swift project with two views that both use CBLUITableSource to represent two different lists of CBLModel objects, games and players. Each type of CBLModel object holds a reference to another POSO which is lazily calculated because its expensive to do so. Its the stats for the game or the player. Both of my view controllers implement couchTableSource(source: CBLUITableSource!, cellForRowAtIndexPath indexPath: NSIndexPath) and in both cases they call CBLModel(forDocument: row.document) to get an instance of the model object.

On the game list, I get the behaviour I want which is that each game instance is instantiated only once and I only ever have to calculate the stats once.

However on the players list, the modelObjects seem to get garbage collected because I frequently get new instances of player objects and have to recalculate the stats. The only obvious difference between the two lists is that on the game list I do:

    let game = Game(forDocument: row.document)

and on the players list I do:

    let player = CBLModel(forDocument: row.document) as Player

because there are different types of players and I want the CBLModelFactory to figure out which one it is.

Has anyone seen this, or have any idea why this is happening?

Jens Alfke

unread,
Feb 8, 2015, 2:12:47 AM2/8/15
to mobile-c...@googlegroups.com

On Feb 7, 2015, at 1:00 PM, Julian Paas <julia...@osaaru.com> wrote:

However on the players list, the modelObjects seem to get garbage collected because I frequently get new instances of player objects and have to recalculate the stats. 

This is intentional. CBLDocument only keeps a weak reference to its associated model object*. And CBLDatabase only keeps strong references to the latest 50 or so CBLDocuments that have been accessed. This keeps huge numbers of objects from piling up in memory if you have a big database.

If you want specific model objects to stay in memory, you'll need to keep your own strong references to them, like from your own controllers or something like that.

—Jens

* Exception: If a model has unsaved changes, the database keeps a strong reference to it until it's saved, so the changes aren't lost.
Reply all
Reply to author
Forward
0 new messages