Latest1.1
Homepagehttps://github.com/Roobiq/RBQSafeRealmObject
LicenseMIT
Platformsios 8.0, requires ARC
DependenciesRBQSafeRealmObject, RealmSwift, RealmUtilities
Authors

RBQSafeRealmObject

This project is deprecated as of Realm 2.2 due to native support for passing objects across threads.

Realm released "Thread-safe references" which conceptually act the same as RBQSafeRealmObject but support all types, including objects without primary keys, Realms, results, and lists:

  1. Initialize a ThreadSafeReference with the thread-confined object.
  2. Pass that ThreadSafeReference to a destination thread or queue.
  3. Resolve this reference on the target Realm by calling Realm.resolve(_:). Use the returned object as you normally would.

For example:

let realm = try! Realm()
let person = Person(name: "Jane") // no primary key required
try! realm.write {
  realm.add(person)
}
let personRef = ThreadSafeReference(to: person)
DispatchQueue(label: "com.example.myApp.bg").async {
  let realm = try! Realm()
  guard let person = realm.resolve(personRef) else {
    return // person was deleted
  }
  try! realm.write {
    person.name = "Jane Doe"
  }
}

See the release blog post for more details.


Thread-Safe Representation Of A Realm Object

Version
License
Language Objective-C | Swift 3
Platform

This class allows you to create a thread-safe representation of a given RLMObject subclass. For example, this enables performing queries on a background thread, and passing results as RBQSafeRealmObject to the main-thread for display.

Note: It will only work with an RLMObject that has a primary key.

Both an Objective-C and Swift version are availble. Swift 2.0 support added in version 0.4.

How To Use

Objective-C

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    RLMObjectSubclass *anObject = [RLMObjectSubclass objectForPrimaryKey:@"key"];

    RBQSafeRealmObject *safeObject = [RBQSafeRealmObject safeObjectFromObject:anObject];

    dispatch_async(dispatch_get_main_queue(), ^{
        RLMObjectSubclass *mainThreadObject = [safeObject RLMObject];

        // Do Something...
    });
});

Swift 3

DispatchQueue.global().async {
    let anObject = Object()

    let safeObject = SafeObject(object: anObject)

    DispatchQueue.main.async {
        let mainThreadObject = safeObject.object()
    }
}

Installation

RBQSafeRealmObject is available through CocoaPods. To install
it, simply add the following line to your Podfile:

Objective-C

pod "RBQSafeRealmObject"

Swift

pod "SafeRealmObject"

CocoaPod Linting

Linting using CocoaPods 1.1.0.rc.2 (with Xcode 8 fixes) needs FORK_XCODE_WRITING to be set to get the build to work. One warning comes through from Realm core.

FORK_XCODE_WRITING=true pod lib lint SafeRealmObject.podspec --allow-warnings

Latest podspec

{
    "name": "SafeRealmObject",
    "version": "1.1",
    "summary": "Thread-safe representation of a Realm Swift Object",
    "description": "SafeRealmObject offers a thread-safe class that represents a Realm Swift Object subclass with a primary key and can be used across threads.",
    "homepage": "https://github.com/Roobiq/RBQSafeRealmObject",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Roobiq": "[email protected]"
    },
    "social_media_url": "http://twitter.com/Roobiq",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/Roobiq/RBQSafeRealmObject.git",
        "tag": "v1.1"
    },
    "source_files": "*.swift",
    "requires_arc": true,
    "dependencies": {
        "RBQSafeRealmObject": [],
        "RealmSwift": [
            ">=0.99.1"
        ],
        "RealmUtilities": [
            ">=0.2.1"
        ]
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This