victor-yn / realm-ready

RealmSwift implementation examples for the iOS-starter-kit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

realm-logo

starter kit badge super cool badge

Realm Ready

This repo covers a basic ready-to-go implementation of RealmSwift in the iOS-starter-kit.

The main purpose is to offer a fast -yet efficient- implementation of the persistance feature in the starter kit; for any further information, please check the official Realm Documentation.

Installation

This 3-min installation is made for the release 1.5.0 of the starter kit.

  1. Add pod 'RealmSwift','~> 3.3.1' in your Podfile and make the magic happen (pod install)

  2. Download the files

  3. Examples files are inside the Examples folder,

    1- Copy all functions from DataManager in your DataManager.

    2- In Xcode, go to starterkit/core/data/provider/, right-click on provider and Add files to "starterkit"

    3- Add the complete realm folder from Examples. Tick Create groups (instead of Create reference). If Create groups is not visible, click on Options at the bottom.

  4. All files are added, your starter-kit now supports persistance. At this point, it will try getting data from the cache; if empty, it will try fetching data from the database and store it in the cache; if empty, fetch data from the network and store it in both cache and database.

From now on you are ready to go.



TO GO FURTHER



Example files

  • DataManager
  • Realm
    • RProvider
    • mapper
      • ItemRMapper
    • dto
      • ItemRDto

R will now stand for Realm, AF for Alamofire. ItemDto : ItemRDto / ItemAFDto

It will show you the implementation for fetching / saving and clearing data.

Models

Models will be persisted as DTOs.

mermaid graph

Persisting objects (DTOs) must subclass Object

Realm model properties must have the @objc dynamic var attribute to become accessors for the underlying database data.

Please refer to this Realm property cheatsheet.

Queries

let itemRDtos = realm.objects(ItemRDto.self) //this will not load the fetched objects into memory

All queries (including queries and property access) are lazy in Realm. Data is only read when the properties are accessed.

Filter

Please check the NSPredicate Cheatsheet for the predicate string syntax.

var filteredItemRDtos = itemRDtos.filter("title = 'example' AND description BEGINSWITH 'I'")

More information

Sorting

var sortedItemRDtos = itemRDtos.sorted(byKeyPath: "title", ascending: true)

More information

Useful quick tips

  • Always set a primary key for any DTO model you are going to use.
   override static func primaryKey() -> String? {
   	return "id"
   }

Realm will update the existing stored data with the new ones by looking for its primary key.

Without this, you will have to delete all the persisted data and then persist the new ones; huge performance costs.

  • Always force update: true when updating/saving persisted data.
   try realm.write {
   	realm.add(dto, update: true)
   }

This will update existing dto objects in the database, looked up by its primary key. If it does not exist, it will be created and added to the database.

  • Delete the Realm file from disk
   do {
   	try FileManager.default.removeItem(at: Realm.Configuration.defaultConfiguration.fileURL!)
   } catch {}

More information about deleting Realm files

Migration

Check the Migration folder

About

RealmSwift implementation examples for the iOS-starter-kit