All Projects → Kaakati → Realm-and-Swift-Codable

Kaakati / Realm-and-Swift-Codable

Licence: other
How to implement Swift 4 Codable with Realm Database

Projects that are alternatives of or similar to Realm-and-Swift-Codable

realm-mapper-ios
Realm + ObjectMapper
Stars: ✭ 28 (-17.65%)
Mutual labels:  realm, realmswift
realms-ios
Safe method for Realm
Stars: ✭ 22 (-35.29%)
Mutual labels:  realm, realmswift
RealmSwiftService
Support Realm CRUD functions. Support Dao Object and Protocol Extension.
Stars: ✭ 12 (-64.71%)
Mutual labels:  realm, realmswift
DailyFeed
iOS client for newsapi.org
Stars: ✭ 128 (+276.47%)
Mutual labels:  realm, realmswift
Swift-Viper-Weather-App
iOS app with Clean Architecture
Stars: ✭ 20 (-41.18%)
Mutual labels:  realm, realmswift
json2codable
A command line tool to generate a Swift Codable struct from a JSON document
Stars: ✭ 19 (-44.12%)
Mutual labels:  codable, decodable
realm-tester
Writing tests using Realm Java
Stars: ✭ 14 (-58.82%)
Mutual labels:  realm
AlamofireCodable
An Alamofire extension which converts JSON response data into swift objects using Codable
Stars: ✭ 47 (+38.24%)
Mutual labels:  codable
FTAPIKit
Declarative and generic REST API framework using Codable.
Stars: ✭ 18 (-47.06%)
Mutual labels:  codable
QuickDB
A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.
Stars: ✭ 16 (-52.94%)
Mutual labels:  codable
erxes-android-sdk
erxes Android SDK, for integrating erxes into your android application
Stars: ✭ 23 (-32.35%)
Mutual labels:  realm
ash
Simple iOS app using MVVM and Codable
Stars: ✭ 24 (-29.41%)
Mutual labels:  codable
RealmAndCharts-example
Simple example of how to use RealmSwift and Charts together - iOS 10 & Swift 3
Stars: ✭ 20 (-41.18%)
Mutual labels:  realmswift
CodablePersist
Store and Cache Anything Codable
Stars: ✭ 18 (-47.06%)
Mutual labels:  codable
electron-react-ts-rxdb-realm-sqlite
Demo of Native Databases with Electron and ReactJS. Realm, SQLite and RxDB ( with LevelDB/IndexedDB/InMemory adapters)
Stars: ✭ 27 (-20.59%)
Mutual labels:  realm
RealmSearchView
SearchView with result list powered by Realm
Stars: ✭ 23 (-32.35%)
Mutual labels:  realm
WXKDarkSky
A pure-Swift Codable layer over the Dark Sky API.
Stars: ✭ 21 (-38.24%)
Mutual labels:  codable
keycloak-home-idp-discovery
Keycloak: Home IdP Discovery - discover home identity provider or realm by email domain
Stars: ✭ 42 (+23.53%)
Mutual labels:  realm
aaf-easyphotomap
📷 Easy Photo Map is a photomap application that displays the location of the photo on the map using the location information included in the photo.
Stars: ✭ 36 (+5.88%)
Mutual labels:  realm
AC-iOS-Codeable-and-UserDefaults
No description or website provided.
Stars: ✭ 16 (-52.94%)
Mutual labels:  codable

Realm & Swift Codable

Visits Badge alt text alt text

How to implement Swift 4 Codable with Realm Database in your Models?

This guide will demonstrate the implementation method for Realm Object containing a List, with Swift 4 Codable.

JSON

{
    "id": 732,
    "name": "Vendor Name",
    "logo": ".../thumb/missing.png",
    "kitchens": [
      {
        "id": 36,
        "name": "Sandwiches"
      },
      {
        "id": 37,
        "name": "Fast Food"
      }
    ]
  }

Model

import RealmSwift

class VendorsList : Object, Decodable {
    @objc dynamic var id : Int = 0
    @objc dynamic var name : String?
    @objc dynamic var logo : String?
    // Create your Realm List.
    var kitchensList = List<VendorKitchens>()
    
    override class func primaryKey() -> String? {
        return "id"
    }
    
    private enum CodingKeys: String, CodingKey {
        case id
        case name
        case logo
        // Set JSON Object Key
        case kitchensList = "kitchens"

    }
    
    public required convenience init(from decoder: Decoder) throws {
        self.init()
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.id = try container.decode(Int.self, forKey: .id)
        self.name = try container.decode(String.self, forKey: .name)
        self.logo = try container.decode(String.self, forKey: .logo)
        // Map your JSON Array response
        let kitchens = try container.decodeIfPresent([VendorKitchens].self, forKey: .kitchensList) ?? [VendorKitchens()]
        kitchensList.append(objectsIn: kitchens)
        
    }
    
}


class VendorKitchens : Object, Decodable {
    @objc dynamic var id : Int = 0
    @objc dynamic var name : String?
    
    override class func primaryKey() -> String? {
        return "id"
    }

    private enum CodingKeys: String, CodingKey {
        case id
        case name
    }
}

URLSession

guard let getUrl = URL(string: "https://Your_Endpoint/.../") else { return }
        URLSession.shared.dataTask(with: getUrl) { (data, response, error) in
            guard let data = data else { return }
            do {
                let decoder = JSONDecoder()
                let getData = try decoder.decode([VendorsList].self, from: data)
                    for data in getData {
                        print(data.id)
                        print(data.name)
                        print(data.logo)
                    }
                    for kitchen in data.kitchensList {
                        print(kitchen.name)
                    }
                }
            } catch let err {
                print("Err", err)
            }
            }.resume()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].