Realm-cocoa: Realm 0.101: fails querying objects from read-only db in background threads

Created on 9 May 2016  路  7Comments  路  Source: realm/realm-cocoa

I ran into this pretty severe issue which appeared in Realm 0.101 (or maybe 0.100, didn't test with previous version): using a database in read-only mode only yields query results from the main thread. Querying the database from a background threads yields empty results sets.

Expected Results

In Realm version prior to 0.100 this used to work just fine (I have a read-only DB in the application with static contents, alongside a read-write DB with dynamic contents).

Actual Results

With Realm 0.101, querying the read-only DB in a background thread returns empty result sets

Steps to Reproduce

Attached is a sample project that demonstrates the issue. It creates a simple DB, then closes it, then opens it read-only from two different queues (main and background) and only the main queue read returns query results for the objects written to the database.

With attached project:

$ pod install
$ open RealmStaticDBCrash.xcworkspace

Then build & run for simulator (iPhone 6S Plus, iOS 9.3). Test results are displayed in Xcode console.

Code Sample

See attached project.
RealmStaticDBCrash.zip

Version of Realm and Tooling

Realm version: 0.101.0

Xcode version: 7.3.1 OS X 10.11.4

iOS/OSX version: iOS simulator 9.3

Dependency manager + version: CocoaPods 0.39.0

Blocked T-Bug

Most helpful comment

I've made a PR against the core library which fixes this. The file mapping sharing added in 0.101.0 was incorrect for read-only Realms.

All 7 comments

I consistently get:

Creating new Realm DB at /Users/tgoyne/Library/Developer/CoreSimulator/Devices/E9230E3B-77D7-4C2D-A79E-9D960797B1C7/data/Containers/Data/Application/4FDF672A-18B5-476F-8F64-3FA068543C64/Library/Caches/static.realm
Creation complete, DB will autoclose now
[mainQueue=true thread <NSThread: 0x7fda79c045b0>{number = 1, name = main}]: opening read-only DB from /Users/tgoyne/Library/Developer/CoreSimulator/Devices/E9230E3B-77D7-4C2D-A79E-9D960797B1C7/data/Containers/Data/Application/4FDF672A-18B5-476F-8F64-3FA068543C64/Library/Caches/static.realm
[mainQueue=true thread <NSThread: 0x7fda79c045b0>{number = 1, name = main}]: got 2 objects from r/o instance
[mainQueue=false thread <NSThread: 0x7fda7c0060c0>{number = 3, name = (null)}]: opening read-only DB from /Users/tgoyne/Library/Developer/CoreSimulator/Devices/E9230E3B-77D7-4C2D-A79E-9D960797B1C7/data/Containers/Data/Application/4FDF672A-18B5-476F-8F64-3FA068543C64/Library/Caches/static.realm
[mainQueue=false thread <NSThread: 0x7fda7c0060c0>{number = 3, name = (null)}]: got 2 objects from r/o instance

Tested both debug and release with the same version of Xcode etc.

Look at this:

Creating new Realm DB at /Users/fpillet/Library/Developer/CoreSimulator/Devices/A9728FD8-BF11-4FA7-AF65-43B496259A11/data/Containers/Data/Application/F948F540-7AD5-42D2-A2B1-90ED82C7B5D9/Library/Caches/static.realm
Creation complete, DB will autoclose now
[mainQueue=true thread <NSThread: 0x7f9aa9c05ac0>{number = 1, name = main}]: opening read-only DB from /Users/fpillet/Library/Developer/CoreSimulator/Devices/A9728FD8-BF11-4FA7-AF65-43B496259A11/data/Containers/Data/Application/F948F540-7AD5-42D2-A2B1-90ED82C7B5D9/Library/Caches/static.realm
[mainQueue=false thread <NSThread: 0x7f9aa9e15cb0>{number = 3, name = (null)}]: opening read-only DB from /Users/fpillet/Library/Developer/CoreSimulator/Devices/A9728FD8-BF11-4FA7-AF65-43B496259A11/data/Containers/Data/Application/F948F540-7AD5-42D2-A2B1-90ED82C7B5D9/Library/Caches/static.realm
[mainQueue=true thread <NSThread: 0x7f9aa9c05ac0>{number = 1, name = main}]: got 2 objects from r/o instance
[mainQueue=false thread <NSThread: 0x7f9aa9e15cb0>{number = 3, name = (null)}]: got 0 objects from r/o instance

Interleaved calls are the secret sauce to reproduce the bug, I think. If both instances are open at the same time, seems to cause the issue.

Yep, that looks like it. By tossing in a sleep(1) between opening the Realm and querying it I got:

Creating new Realm DB at /Users/tgoyne/Library/Developer/CoreSimulator/Devices/E9230E3B-77D7-4C2D-A79E-9D960797B1C7/data/Containers/Data/Application/9BCD271C-7DD7-42BD-895C-15E11BF89D26/Library/Caches/static.realm
Creation complete, DB will autoclose now
[mainQueue=true thread <NSThread: 0x7ffc8af02c10>{number = 1, name = main}]: opening read-only DB from /Users/tgoyne/Library/Developer/CoreSimulator/Devices/E9230E3B-77D7-4C2D-A79E-9D960797B1C7/data/Containers/Data/Application/9BCD271C-7DD7-42BD-895C-15E11BF89D26/Library/Caches/static.realm
[mainQueue=false thread <NSThread: 0x7ffc8ae04000>{number = 3, name = (null)}]: opening read-only DB from /Users/tgoyne/Library/Developer/CoreSimulator/Devices/E9230E3B-77D7-4C2D-A79E-9D960797B1C7/data/Containers/Data/Application/9BCD271C-7DD7-42BD-895C-15E11BF89D26/Library/Caches/static.realm
[mainQueue=true thread <NSThread: 0x7ffc8af02c10>{number = 1, name = main}]: got 2 objects from r/o instance
[mainQueue=false thread <NSThread: 0x7ffc8ae04000>{number = 3, name = (null)}]: got 0 objects from r/o instance

That's it. "got 0 objects" where you should have 2.

I've made a PR against the core library which fixes this. The file mapping sharing added in 0.101.0 was incorrect for read-only Realms.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings