I have rewritten my ObjC code to Swift and also started using RealmSwift. In my app I can access Realm object without any problem. But in App Extension (Today widget) I'm always getting 0 results (using same code from shared class).
I was checking App Groups settings for both app and extension, also path for Realm configuration is the same.
Anyone having similar issue? Or is there any tutorial/example how to use RealmSwift in Swift2.0 with App Extension?
Do you set the path for your Realm file for both targets also into the App Group as seen in our Tutorial: Sharing Data between WatchKit & your App, with Realm?
For both targets I'm setting the path this way:
let container = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.martinpilch.test")
let realmURL = container!.URLByAppendingPathComponent("default.realm")
var config = Realm.Configuration()
config.path = realmURL.path
Realm.Configuration.defaultConfiguration = config
That looks fine. Do you really write that in ypur app after ypu setup the configuration? When do you check in your app extension the number of results. If you reset the database file in development on every launch, it could be necessary that you would need to usewrite notifications to sync the processes. If that shouldn't be the case, could you try to extract the user data and ensure that there Is at least one realm file containing the expected data? (Not sure though, how or whether that's actually possible for app extension data.)
I finally found solution @mrackwitz ! I was using different code for migration. Working version looks like this:
let container = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.martinpilch.test")
let realmURL = container!.URLByAppendingPathComponent("default.realm")
var config = Realm.Configuration()
config.path = realmURL.path
config.schemaVersion = 3
config.migrationBlock = { migration, oldSchemaVersion in
if (oldSchemaVersion < 3) {
}
}
Realm.Configuration.defaultConfiguration = config
let _ = try! Realm()
The most important part is let _ = try! Realm() which was missing for widget. You can mark it as closed, now it's solved for me. Thank you for your time!
Okay, that makes sense. Thanks for reporting back. Be aware though that if you not hold anymore a strong reference to your Realm from a thread, that the cached weak accessor will be cleared as well and you pay a little extra cost next time when you access the Realm. So it might make more sense to pass on the reference which you get by your last call.
Hi Everyone I have a similar issue in this ticket, I tried all was written here, but it does not work either, any please help?
Most helpful comment
For both targets I'm setting the path this way: