Coder Social home page Coder Social logo

realmeasy's Introduction

RealmEasy

Realm is already a fantastic product, that makes persistence in iOS so much easier. RealmEasy tries to make this even easier ...

Why RealmEasy?

I created RealmEasy to solve two problems.

  • As of Realm 0.87 it is no longer possible to set the default Realm to be an inMemoryRealm. This means that you would have to either use the real default.realm for your unit testing, or pass the realm around all over your code (no longer use 'defaultRealm').

  • I often found myself writing model object methods that would sometimes be called as part of some other transaction, and sometimes they would be called outside any transaction. So I either had to wrap all these calls in beginTransacion boilerplate code, or pass some information about wether or not I was already in a transaction.

Getting and setting your default realm

Simply call

    RLMRealm realm* = RealmEasy.realm;

instead of

    RLMRealm realm* = RLMRealm.defaultRealm;

And to use an in memory realm call...

    [ RealmEasy useInMemoryRealm:@"UnitTest" ];

And to make sure your RLMObject's use the right default Realm, simply inherit from RealmEasyObject instead of RLMObject

@interface MyModelObject : RealmEasyObject 

instead of

@interface MyModelObject : RLMObject 

Creating a new transation - or use the current transaction

You can always call RealmEasy transaction, even if you are already in a transaction!

    [ RealmEasy transaction:^{
        [RealmEasy.realm addObject:account ];
    } ];

Use in UnitTest

Simply add something like this to your application: didFinishLaunchingWithOptions: in your AppDelegate

#if TESTING
    [ RealmEasy useInMemoryRealm:@"UnitTest" ];
#endif

This test actually works...

- (void)testRealmEasy
{
    Account *account = [[ Account alloc] init ];
    account.username = @"realmy";
    account.password = @"RealySecret";
    account.name     = @"My Realm Name";
    [ RealmEasy transaction:^{
        [RealmEasy.realm addObject:account ];
    } ];
    
    [ RealmEasy transaction:^{
        Account *a1 = [ Account findByUsermame:@"realmy" ];
        a1.name = @"Another Name";
        
        [ RealmEasy transaction:^{
            Account *a2 = [ Account findByUsermame:@"realmy" ];
            a2.name = @"Yet Another Name";
        } ];
    } ];
    Account *a3 = [ Account findByUsermame:@"realmy" ];
    
    XCTAssertEqualObjects(a3.name, @"Yet Another Name" );
}

Installation

Simply copy RealmEasy*.* to your project.

Status

The current status of RealmEasy is : "It works for me...". :-)

This is still just a quick experiment. There are no guarantee that it works in all cases, and that I have caught all places where the old defaultRealm was used.

Any ideas, fixes and additions are welcome. Please just open a GitHub issue, or pull request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.