Created
November 12, 2018 21:12
-
-
Save vialyx/3466b628532df037b341aeeb694c5b68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import RealmSwift | |
// TODO: - Move to the separated file Flight.swift (Realm folder) | |
class Flight: Object { | |
@objc dynamic var airline: String = "" | |
@objc dynamic var flight: String = "" | |
@objc dynamic var actualTime: String = "" | |
override static func primaryKey() -> String? { | |
return "flight" | |
} | |
} | |
final class Database { | |
func save(flights: [FlightData]) { | |
let realm = try! Realm() | |
try! realm.write { | |
flights.forEach { flight in | |
let object = Flight() | |
object.airline = flight.Airline | |
object.flight = flight.Flight | |
object.actualTime = flight.ActualTime | |
realm.add(object, update: true) | |
} | |
} | |
} | |
func get() -> [FlightData] { | |
let realm = try! Realm() | |
return realm.objects(Flight.self).map { object in | |
return FlightData(Airline: object.airline, | |
Flight: object.flight, | |
ActualTime: object.actualTime) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment