Skip to content

Commit

Permalink
Use Realm
Browse files Browse the repository at this point in the history
  • Loading branch information
mrackwitz committed May 11, 2016
1 parent ea95e53 commit 7a681fd
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
14 changes: 9 additions & 5 deletions SwinjectSimpleExample/City.swift
Expand Up @@ -6,10 +6,14 @@
// Copyright © 2015 Swinject Contributors. All rights reserved.
//

import Foundation
import RealmSwift

struct City {
let id: Int
let name: String
let weather: String
class City: Object {
dynamic var id: Int = 0
dynamic var name: String = ""
dynamic var weather: String = ""

override static func primaryKey() -> String {
return "id"
}
}
10 changes: 9 additions & 1 deletion SwinjectSimpleExample/SwinjectStoryboard+Setup.swift
Expand Up @@ -7,6 +7,7 @@
//

import Swinject
import RealmSwift

extension SwinjectStoryboard {
class func setup() {
Expand All @@ -15,7 +16,14 @@ extension SwinjectStoryboard {
}
defaultContainer.register(Networking.self) { _ in Network() }
defaultContainer.register(WeatherFetcher.self) { r in
WeatherFetcher(networking: r.resolve(Networking.self)!)
WeatherFetcher(networking: r.resolve(Networking.self)!,
realm: r.resolve(Realm.self)!)
}
defaultContainer.register(Realm.Configuration.self) { _ in
Realm.Configuration()
}
defaultContainer.register(Realm.self) { r in
try! Realm(configuration: r.resolve(Realm.Configuration.self)!)
}
}
}
26 changes: 23 additions & 3 deletions SwinjectSimpleExample/WeatherFetcher.swift
Expand Up @@ -9,9 +9,11 @@
import Foundation
import Alamofire
import SwiftyJSON
import RealmSwift

struct WeatherFetcher {
let networking: Networking
let realm: Realm

func fetch(response: [City]? -> ()) {
networking.request { data in
Expand All @@ -23,11 +25,29 @@ struct WeatherFetcher {
private func decode(data: NSData) -> [City] {
let json = JSON(data: data)
var cities = [City]()
for (_, j) in json["list"] {
if let id = j["id"].int {
cities.append(City(id: id, name: j["name"].string ?? "", weather: j["weather"][0]["main"].string ?? ""))
try! realm.write {
for (_, j) in json["list"] {
if let city = createOrUpdateDecodedCity(j) {
realm.add(city, update: true)
cities.append(city)
}
}
}
return cities
}

private func createOrUpdateDecodedCity(json: JSON) -> City? {
guard let id = json["id"].int else {
return nil
}
let city: City
if let existingCity = realm.objectForPrimaryKey(City.self, key: id) {
city = existingCity
} else {
city = City(value: ["id": id])
}
city.name = json["name"].string ?? ""
city.weather = json["weather"][0]["main"].string ?? ""
return city
}
}
16 changes: 14 additions & 2 deletions SwinjectSimpleExampleTests/WeatherFetcherSpec.swift
Expand Up @@ -10,6 +10,7 @@
import Quick
import Nimble
import Swinject
import RealmSwift
@testable import SwinjectSimpleExample

class WeatherFetcherSpec: QuickSpec {
Expand Down Expand Up @@ -48,17 +49,28 @@ class WeatherFetcherSpec: QuickSpec {
var container: Container!
beforeEach {
container = Container()

container.register(Realm.Configuration.self) { _ in
var config = Realm.Configuration()
config.inMemoryIdentifier = "SwinjectSimpleExample"
return config
}
container.register(Realm.self) { r in
try! Realm(configuration: r.resolve(Realm.Configuration.self)!)
}

// Registrations for the network using Alamofire.
container.register(Networking.self) { _ in Network() }
container.register(WeatherFetcher.self) { r in
WeatherFetcher(networking: r.resolve(Networking.self)!)
WeatherFetcher(networking: r.resolve(Networking.self)!,
realm: r.resolve(Realm.self)!)
}

// Registration for the stub network.
container.register(Networking.self, name: "stub") { _ in StubNetwork() }
container.register(WeatherFetcher.self, name: "stub") { r in
WeatherFetcher(networking: r.resolve(Networking.self, name: "stub")!)
WeatherFetcher(networking: r.resolve(Networking.self, name: "stub")!,
realm: r.resolve(Realm.self)!)
}
}

Expand Down
12 changes: 11 additions & 1 deletion SwinjectSimpleExampleTests/WeatherTableViewControllerSpec.swift
Expand Up @@ -9,6 +9,7 @@
import Quick
import Nimble
import Swinject
import RealmSwift
@testable import SwinjectSimpleExample

class WeatherTableViewControllerSpec: QuickSpec {
Expand All @@ -27,7 +28,16 @@ class WeatherTableViewControllerSpec: QuickSpec {
container.register(Networking.self) { _ in MockNetwork() }
.inObjectScope(.Container)
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
var config = Realm.Configuration()
config.inMemoryIdentifier = "SwinjectSimpleExample"
return config
}
container.register(Realm.self) { r in
try! Realm(configuration: r.resolve(Realm.Configuration.self)!)
}
container.register(WeatherTableViewController.self) { r in
let controller = WeatherTableViewController()
Expand Down

0 comments on commit 7a681fd

Please sign in to comment.