Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Realm Mobile Platform - Explore Real Time Data Synchronization Capabilities

DZone's Guide to

Realm Mobile Platform - Explore Real Time Data Synchronization Capabilities

The Realm mobile platform enables real time two-way data sync with its object server. Learn what this means for mobile app development in this overview.

Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Since its announcement in September of 2016, Realm mobile platform has been a highly awaited technology and its first stable release in January of 2017 proved its worth. Its proficiency to allow two-way data sync between the Realm Object Server and the client side databases and event handling capacity have enabled the development of futuristic mobile apps.

Realm at a Glance

  • Realm is a savvy mobile platform that is developed after adequate understanding of the latest mobile app development challenges. It also holds the caliber to expand the developer’s vision for upcoming mobile trends. Following are few of its strength characteristics:

  • Freedom for Hosting: Realm Mobile Platform can be hosted on-premises as well as on the cloud.
  • Object-based: Realm is object-based; hence object itself reflects the database. Developers can easily keep themselves away from ORM related difficulties.
  • Offline-first capabilities: Realm can deliver extensive offline-first experiences due to the embedded Realm Mobile Database on the device.
  • Easy to integrate: It can easily be integrated with existing infrastructure.
  • It supports server side business logic.

Explore Realm’s Real Time Data Sync Capabilities Through a “Hello World” Application

  • We have created a Hello World mobile application that enables the user to draw any sketch or to write text by using virtual pencils. We will explore the Realm mobile platform’s real time data sync capabilities through it.

Process Flow

  • Our application ‘Hello World’ is being used by two users situated at different geographical locations through their iOS devices >> They are sharing the sketches by drawing them through virtual pencils on their individual devices >> While one user is drawing the sketch in his device, the other user is able to view the sketch development process in real time (the same shape and color codes are visible to another person).

How to utilize Realm’s real time data sync capabilities can be explained in three steps:

  • Realm Object Server Configuration.
  • Realm Application User Authentication.
  • Real Time Sync Process.

Step 1: Realm Object Server Configuration

  1. Download “Mac OS Bundle” OR “Linux OS Bundle.”
  2. Open “realm-mobile-platform” folder and Start Realm Object Server by double-clicking the file “start-object-server.command.”
  3. This will open one terminal window and start the Realm server.
  4. It will automatically open the dashboard as displayed in the image below. This dashboard is a browser based application which provides few required information like Dashboard, Realms, Users, Functions, etc.realm_dashboard
  5. You can create the desired number of users here.

Step 2: Realm Application

  • Now run the application. In the application, we are authenticating the user to connect to the Realm server by providing their credential to our application. The authentication process has been described below in technical terms.

- (void)logIn
{
 // The base server path
 // Set to connect to local or online host
 NSURL *authURL = [NSURL URLWithString:[NSString stringWithFormat:@“http://%@:9080”, kIPAddress]];
 NSLog(@“authURL:::::::::::%@“,authURL);
 
 // Creating a debug credential since this demo is just using the generated access token
 // produced when running the Realm Object Server via the `start-object-server.command`
 // RLMSyncCredentials *credential = [RLMSyncCredentials credentialsWithUsername:@“demo@realm.io”
 // password:@“password”
 // register:NO];
 
 RLMSyncCredentials *credential = [RLMSyncCredentials credentialsWithUsername:kUserName
 password:kPassword
 register:NO];
 
 
 // Log the user in (async, the Realm will start syncing once the user is logged in automatically)
 [RLMSyncUser logInWithCredentials:credential
 authServerURL:authURL
 onCompletion:^(RLMSyncUser *user, NSError *error) {
 if (error) {
 self.activityIndicatorView.hidden = YES;
 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@“Login Failed” message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
 [alertController addAction:[UIAlertAction actionWithTitle:@“Retry” style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
 [self logIn];
 self.activityIndicatorView.hidden = NO;
 }]];
 [self presentViewController:alertController animated:YES completion:nil];
 
 //[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
 }
 else { // Logged in setup the default Realm
 // The Realm virtual path on the server.
 // The `~` represents the Realm user ID. Since the user ID is not known until you
 // log in, the ~ is used as short-hand to represent this.
 NSURL *syncURL = [NSURL URLWithString:[NSString stringWithFormat:@“realm://%@:9080/~/Draw”, kIPAddress]];
 NSLog(@“Login syncURL:::::::::::%@“,syncURL);
 RLMSyncConfiguration *syncConfig = [[RLMSyncConfiguration alloc] initWithUser:user realmURL:syncURL];
 RLMRealmConfiguration *defaultConfig = [RLMRealmConfiguration defaultConfiguration];
 defaultConfig.syncConfiguration = syncConfig;
 [RLMRealmConfiguration setDefaultConfiguration:defaultConfig];
 dispatch_async(dispatch_get_main_queue(), ^{
 //self.window.rootViewController.view = [DrawView new];
 // [self.view addSubview:[DrawView new]];
 self->dView = [DrawView new];
 self->dView.frame = CGRectMake(0, 120, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-120);
 self->dView.backgroundColor=[UIColor whiteColor];
 [self.view addSubview:self->dView];
 // [self->dView bringSubviewToFront:self->_btnClear];
 
 });
 }
 }];
}

Step 3: Real Time Syncing

  1. Whenever the user performs any action on our Realm Hello World application (e.g. the user starts drawing on an iOS device), the application will sync the details with the Realm server (here, the application will sync the color code coordinate (x,y points) to the Realm server in real time). As described below, all the coordinates are being added to the Realm array, which eventually will be synced to the Realm server.
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    // Create a draw path object
    self.drawPath = [[DrawPath alloc] init];
    self.drawPath.color = self.currentColorName;
    
    // Create a draw point object
    CGPoint point = [[touches anyObject] locationInView:self.canvasView];
    DrawPoint *drawPoint = [[DrawPoint alloc] init];
    drawPoint.x = point.x;
    drawPoint.y = point.y;
    
    // Add the draw point to the draw path
    [self.drawPath.points addObject:drawPoint];
    
    // Add the draw path to the Realm
    RLMRealm *defaultRealm = [RLMRealm defaultRealm];
    [defaultRealm transactionWithBlock:^{
    [defaultRealm addObject:self.drawPath];
    }];
    
    [self.canvasView setNeedsDisplay];
    }
  2. The application will down sync the data from the server and display it on the screen of another authenticated user, who is already connected to the Realm server.
  • Because of Realm Mobile Platform’s distinctive potential, development of functionalities such as two-way data sync, offline first capability, and endpoint computing are no more complex, complicated, or high priced features in the context of development, implementation, and maintenance. As a developer, I am eagerly looking forward to Realm’s further advancements.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
mobile ,realm ,mobile app development ,data sync

Published at DZone with permission of Shrenik Shah, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}