Skip to content

Instantly share code, notes, and snippets.

@moflo
Last active March 22, 2016 05:52
Show Gist options
  • Save moflo/253231bba96dadfc3f85 to your computer and use it in GitHub Desktop.
Save moflo/253231bba96dadfc3f85 to your computer and use it in GitHub Desktop.
RealmORM, realm.io ORM in Swift 2.0 with simple CoreData like NSFetchController features, sorting Results<object> by a key
//
// RealmORM.swift
//
// Copyright © 2016 Mobile Flow LLC. All rights reserved.
//
import UIKit
import Realm
import RealmSwift
class RealORMFetchObject {
var sections = Dictionary<String, Array<RealmORM>>()
var sortedSections = [String]()
let managedObjectContext : NSObject? = nil
let fetchRequest : NSObject? = nil
init() {
}
func reset() {
self.sections.removeAll(keepCapacity: true)
self.sortedSections.removeAll(keepCapacity: true)
}
func fetchResultsBySection(realm: Realm, sectionKey: String, classObject: RealmORM.Type) -> Int {
self.reset()
let objects = classObject.fetchAll(realm)
for item in objects {
// NOTE: assumes section key type of String, need to refactor for NSDate, Int64, etc.
let value :String = (item.valueForKey(sectionKey) ?? "") as! String
if self.sections.indexForKey(value) == nil {
self.sections[value] = [item]
}
else {
self.sections[value]?.append(item)
}
}
self.sortedSections = Array(self.sections.keys).sort{ $0 < $1 }
return self.sortedSections.count
}
func objectAtIndexPath(indexPath :NSIndexPath) -> RealmORM? {
guard sortedSections.count > indexPath.section else { return nil }
let tableSection = sections[sortedSections[indexPath.section]]
guard tableSection != nil && tableSection!.count > 0 else { return nil }
let tableItem = tableSection![indexPath.row]
return tableItem
}
func numberOfRowsInSection(section :Int) -> Int {
guard sortedSections.count > section else { return 0 }
guard sections.count > 0 else { return 0 }
return sections[sortedSections[section]]?.count ?? 0
}
}
class RealmORM: Object {
dynamic var uuid :String = NSUUID().UUIDString
dynamic var createdAt = NSDate()
func save(realm: Realm) {
try! realm.write {
realm.add(self)
}
}
class func fetchAll(realm: Realm) -> Results<RealmORM> {
return realm.objects(self).sorted("createdAt", ascending: true)
}
}
@moflo
Copy link
Author

moflo commented Mar 22, 2016

   func testFetchObject() {

        // Testing RealORMFetchObject

        let realm = try! Realm()

        let testClass1 = testClass()
        testClass1.cat = "CATEGORY1"
        testClass1.save(realm)
        let testClass2 = testClass()
        testClass2.cat = "CATEGORY2"
        testClass2.save(realm)
        let testClass3 = testClass()
        testClass3. cat = "CATEGORY2"
        testClass3.save(realm)

        let fetchController = RealORMFetchObject()

        let sections = fetchController.sections

        XCTAssertEqual(sections.count, 0, "sections count should be zero initially")

        let rows = fetchController.numberOfRowsInSection(0)

        XCTAssertEqual(rows, 0, "rows should be zero initially")

        let object = fetchController.objectAtIndexPath(NSIndexPath(forItem: 0, inSection: 0))

        XCTAssertNil(object, "object should not exist initially")

        let fetchResult = fetchController.fetchResultsBySection(realm, sectionKey: "cat", classObject: testClass.self)

        let sections2 = fetchController.sections

        XCTAssertEqual(sections2.count, fetchResult, "sections count not equal")

        let rows1 = fetchController.numberOfRowsInSection(0)
        XCTAssertEqual(rows1, 1, "first section should equal 1")

        let rows2 = fetchController.numberOfRowsInSection(1)
        XCTAssertEqual(rows2, 2, "first section should equal 1")

        let object2 = fetchController.objectAtIndexPath(NSIndexPath(forItem: 0, inSection: 1))

        XCTAssertNotNil(object2, "object should not exist initially")



    }
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment