Realm is a mobile database that functioned similar to SQLite and Coredata. It supports iOS and Android platforms. (Yay)

Why Realm? Realm VS Coredata 

From Realm VP Product, Tim Anglade


  1. Realm is much easier to use — equivalent Realm apps will save you literally thousands of lines of code and weeks of work. We were surprised the advantage was so big, but it came up consistently in user interviews.

  2. Consistent view across threads — accessing Realm from a background thread looks the same as accessing Realm from the UI thread (see docs). there is no need to maintain multiple contexts and merge them manually, no inconsistent views, and no headaches trying to reconcile your data. It just works™; the only limitation, as of realm-cocoa 0.90, is that object instances cannot be passed across threads, but this is actually something that will be relaxed in an upcoming release

  3. Much faster — 2–30x faster depending on the operation, and much much faster in others. If you look on Twitter, you’ll see people report similar speedups in their apps.

  4. Built-in encryption — AES256+SHA2 encryption with a moderate performance hit of ~10%.

  5. Support available — while there is obviously a wealth of SO questions and articles written about Core Data, there simply is no vendor you can turn to for quick, official responses to questions, bugfixes or feature requests. In comparison, the entire team at Realm is available daily, and support is our #1 priority above all else. We are available on the following channels, and you can see our response times for yourself: on Github,StackOverflowTwitteremail or in person

Now to be clear there are things Core Data has that Realm doesn’t:

  • Stronger battle-testing. Core Data runs on SQLite, probably one of the most heavily tested open-source pieces of software ever. Realm has been running in production since 2012, and is now deployed on literally millions of devices, with no major known bugs at this time, but we are still pre-1.0 software, with a long way to go to achieve the same stability.
  • Tons of small features that we are working tirelessly to add every week — you can always find an up-to-date list on Github

****I am going to use Realm for my project soon so I’ll update further if I encounter any problems or confusions in the future. The followings are my own notes from their documentation site as I learn things through writing things down so things are pretty similar.****

— Initiate model

@interface Student : RLMObject
@property NSString id;
@property NSString *name;
@end
@implementation Student
//set default values 
+(NSDictionary *) defaultPropertyValues{
      return @{@"id": @0, @“name": @“no name" };
}

+(NSString *) primaryKey{
      return @“id";
}

@end

— Writes (Add, Update, Delete)

Student *firststu = [[Student alloc] init];
firststu.name = @“Harry Potter”;

// Get the default Realm 
RLMRealm *realm = [RLMRealm defaultRealm];

// Add to Realm with transaction
[realm beginWriteTransaction]; //START

//Add
[realm addObject:firststu];

//Update
firststu.name = @"Emma Watson”;

//Update or create if obj not found
[Student createOrUpdateInRealm: realm withObject:firststu];

//Delete An Object
[realm deleteObject:firststu];

//Delete All Objects
[realm deleteAllObjects];
[realm commitWriteTransaction]; //END

— Query

//Retrieve All Objects
RLMResults *students = [Student allObjects];

//Retrieve An Object By Predicates
RLMResults *TStudent = [Student objectsWhere:@"name BEGINSWITH ’T'”];

OR

NSPredicate *pred = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@“, @“T”];
TStudent = [Student objectsWithPredicate:pred];

//Retrieve Object By Order
RLMResults *sortedStudent = [[Student objectsWhere:@"name BEGINSWITH ’T'"] sortedResultsUsingProperty:@"name" ascending:YES];

Comment

The concept of realm environment is not hard to understand. I just tried to create a table with pre-populate records. As this platform is considered relatively new for now so you might notice you won’t find any much info in stackoverflow yet.

[Update 12May2015] For those who wished to use Swift for project, you need to ensure your project min target 8.0. Because of this reason, I have to use ObjC framework and convert codes in Swift. (Naming convention is different in both platforms: RLMObject vs Object)

   // copy over old data files for migration
    NSString *defaultRealmPath = [RLMRealm defaultRealmPath];
    NSString *v0Path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"default-v0.realm"];
    [[NSFileManager defaultManager] removeItemAtPath:defaultRealmPath error:nil];
    [[NSFileManager defaultManager] copyItemAtPath:v0Path toPath:defaultRealmPath error:nil];

        
    // trying to open an outdated realm file without first registering a new schema version and migration block
    // with throw
    @try {
        [RLMRealm defaultRealm];
    }
    @catch (NSException *exception) {
        NSLog(@"Trying to open an outdated realm a migration block threw an exception.");
    }

    // now that we have called `setSchemaVersion:withMigrationBlock` opening an outdated Realm will automatically
    // perform the migration and opening the Realm will succeed
    [RLMRealm defaultRealm];
    
    // print out all migrated objects in the default realm
    NSLog(@"Migrated objects in the default Realm: %@", [[Word allObjects] description]);

sample1

Realm browser

You cannot create new record as this is only meant for browsing purpose only. I hope they will improve on this in the future. (Such inconvenient if I need to pre-populate records)

Ream Browser Sample

References

https://realm.io/docs/cocoa/

http://bsktapp.com/blog/why-is-realm-great-and-why-are-we-not-using-it/

https://www.quora.com/Why-would-you-use-Realm-over-Core-Data

http://www.sebastiandobrincu.com/blog/5-reasons-why-you-should-choose-realm-over-coredata

Advertisements

One thought on “New Choice for Mobile Database? Realm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s