realmswinject

Combine Swinject and Realm


thanks for that Framework. I really like the idea and I'm eager to use it! However, I'm currently trying to get this up and running with an app that uses realm as well. I initially tought, It might be a good idea to create a realmService which I inject to my models and which handles all of the realm write stuff.

Sadly, I can not make my mind up on how to do this properly. The Wether App example is great, but it doesn't cover any realm models. Any hint to point me into the correct direction or something? I tried via constructor and property but I just can't get it to work. I guess, I'm missing something conceptual.

Thanks, I'm eager to learn from you :)

Cheers


Solution

  • I just forked the Weather example app and added Realm in there, using Swinjects DI mechanism. Registering the service component pairs could look like this:

    container.register(WeatherFetcher.self) { r in
        WeatherFetcher(networking: r.resolve(Networking.self)!)
        WeatherFetcher(networking: r.resolve(Networking.self)!,
                       realm: r.resolve(Realm.self)!)
    }
    container.register(Realm.Configuration.self) { _ in
        // not really necessary if you stick to the defaults everywhere
        return Realm.Configuration()
    }
    container.register(Realm.self) { r in
        try! Realm(configuration: r.resolve(Realm.Configuration.self)!)
    }