Search code examples
iosswiftrealm

Realm in today extension


I installed Realm manually, and it is working on my main app. But on the today extension, i can't even import RealmSwift. I apologise if this question is a newbie question, but I've been searching the web and I can't find a proper updated version. I have read https://realm.io/news/tutorial-sharing-data-between-watchkit-and-your-app/ but it's written a year ago and it does not work anymore.

So how do I go about using realm to share data between the main app and the extension?

Thanks for the help!


Solution

  • Even though that article is quite old (It's not even using Realm Swift! It's using the Objective-C version bridged to Swift!), the principles of using Realm with iOS 8 extensions haven't changed to the point where the information in there has become outdated.

    The main points to consider:

    1. For all intents and purposes, a Today Extension is a wholly separate app from your main app. It needs to be able to be fully self-contained in its operation.
    2. As long as you're using the dynamic framework version, you can link one copy of RealmSwift.framework to both your main app, and the extension.
    3. You need to make sure you've set your Realm file to be saved in the containerURLForSecurityApplicationGroupIdentifier URL. This is the only place where your main app and the extension can share files.
    4. Because the Today extension is technically a separate app, make sure any Object subclasses you've made are included in both your main app, and your extension's 'Compile Sources' build phases.

    Please let me know if you need any more clarification. :)