Realm-cocoa: Preloaded realm file (Exception error: 'In-memory identifier must not be empty')

Created on 10 Dec 2015  路  9Comments  路  Source: realm/realm-cocoa

Hi All,

Any help with my issue, Im using a preloaded realm file but when im trying to read the record inside the file it throws an exception. Please see my screenshots.

Thanks.

ViewController
screen shot 2015-12-10 at 3 44 59 pm

Realm Class
screen shot 2015-12-10 at 3 48 16 pm

Exception Error
screen shot 2015-12-10 at 3 44 37 pm

Realm File
preloaded.realm.zip

T-Help

Most helpful comment

Hi, I stumbled across this thread via Google after having the same issue and wanted to share the solution for other Xcode-newcomers like myself who might come across this.

When you drag a file into Xcode, you might assume it's also going to be included in the app, but that's not the case: you sometimes need to explicitly go into "Build Phases" and explicitly add the .realm file in the "Copy Bundle Resources" section. I'm not sure why sometimes dragging a file into Xcode seems to do this for you automatically and sometimes it doesn't.

All 9 comments

The only possibility for that exception to be thrown based on the code in the screenshots you've shared is if NSBundle.mainBundle().pathForResource("preloaded", ofType: "realm") is returning nil.

However, since you're not setting a value for inMemoryIdentifier, we should instead be throwing a more useful exception along the lines of "A Realm Configuration must specify a path or an in-memory identifier.".

Hi @jpsim,

Please any recommendation on how can I debug this issue so that I can have idea on how to fix this.

Thanks.

Please any recommendation on how can I debug this issue so that I can have idea on how to fix this.

What does NSBundle.mainBundle().pathForResource("preloaded", ofType: "realm") return? If it returns nil, then your main bundle doesn't include a preloaded.realm file. Is this file being copied into the bundle? Is the main bundle where you're calling this code the same bundle containing the preloaded.realm file? Are the file name and extension correct? Are you calling this from a command line app where there is no bundle?

Yup when I checked the return of the NSBundle it returns nil, yes I just copied the realm file to main bundle. And I'm calling it in the simulator.

Yup when I checked the return of the NSBundle it returns nil, yes I just copied the realm file to main bundle. And I'm calling it in the simulator.

A realm file is just a regular file, so there's nothing specific about Realm causing this. There must be a configuration problem. Did you check any of the other tips I provided?

Yes i've tried all your tips, i added the file in the main bundle. But the error still occur.

This is the result when I print the NSBundle return

screen shot 2015-12-10 at 5 43 18 pm

Did you resolve this? I see you closed the ticket.

Yes @jpsim, my bad. Thanks!

Hi, I stumbled across this thread via Google after having the same issue and wanted to share the solution for other Xcode-newcomers like myself who might come across this.

When you drag a file into Xcode, you might assume it's also going to be included in the app, but that's not the case: you sometimes need to explicitly go into "Build Phases" and explicitly add the .realm file in the "Copy Bundle Resources" section. I'm not sure why sometimes dragging a file into Xcode seems to do this for you automatically and sometimes it doesn't.

Was this page helpful?
0 / 5 - 0 ratings