Factoring Offline Syncing into your App Design

While most of us are connected to the Internet most of the time, there are still times when we are not online. Just because we are offline doesn’t mean we won’t want to use our apps. Herein lies the problem if you’re developing apps that depend on the Internet to function correctly. We want to check our fitness app part way through a run in the woods, we’d like to play our game apps while sitting on a bus. If your app doesn’t work for everyone, all the time, then your customers will soon find an alternative app that is available offline.

App development

So what do you do about it?

Database synchronization is the key. Luckily, this isn’t something you’re going to have to develop yourself. It’s far easier to use a third-party provider such as Google Firebase or Amazon Cognito for your synchronization needs.

Which provider should I go with?

Looking forward

Choosing which provider to opt for will depend on your own specific requirements. It’s important to think about the future. While your app may currently be aimed solely at mobile devices, will you eventually want to branch out to enable users to install your app on their desktop or laptop machines? Make the right choices now, so you won’t have to make big changes in the future. You should also find an option that allows for some flexibility. While you can predict the future to some extent, it’s hard to visualize what your mobile app development requirements will be in several years time. A provider that allows you to be flexible will help you to grow and expand without limiting you.

Security is paramount

You’ll need your data to be encrypted, you’ll also need data to be sent securely, either via SSL or TLS. Your users will presume any data they send via your app, or store within the app is secure, so you need to make sure it is or you’ll have big problems later. A security breach is the quickest way to lose a good reputation.

Security

Synchronization conflicts need to be addressed

When you’re synchronizing data, there is always the risk of conflicts. For instance, a user has the app open on both their phone and their tablet. How do you deal with this? This data conflict needs to be resolved in the least noticeable way for the user. Check out the providers and do your research. Whose approach will best suit your own needs for conflict resolution?

It’s all about timing

Your users won’t thank you if your app is syncing when they’ve got their mobile data on, racking up a big bill and draining their battery. It’s important to find a provider that makes sensible presumptions and gives options for the timing of synchronization. For instance, you may well want your app to synchronize only when the user is on a wifi connection and not to sync when their mobile device is low on battery.

Facebooktwitterredditlinkedinmail

New R Driver Option in MongoDB 3.6

In a previous post, I discussed some options for using MongoDB with the R Language. While the information in that post is still accurate, MongoDB 3.6 introduces a new R driver option. Unlike the drivers previously discussed, this new R driver is under active development.

New R Driver

The new R driver, mongolite, can be found on GitHub and is easily installed from the Comprehensive R Archive Network (CRAN) using the install.packages("mongolite") command on Windows or OS-X. The driver, or client, is authored by Jeroen Ooms who also has provided some nice documentation in PDF format. Let’s head into RStudio and take a look at using the new R driver.

Accessing MongoDB

Let’s use the same dataset as in the previous post and, with mongoimport, get the data into a MongoDB collection. The database again will be called kenblog and the collection is scores. Here again is a sample document in the collection:

{
   "_id" : ObjectId("5627207b33ff2cf40effc25e"),
   "student" : 2,
   "type" : "quiz",
   "score" : 74
}

After using the install.packages("mongolite") command, we can put the power of R to work. We establish a connection to our database:

> require("mongolite")
Loading required package: mongolite
> connection <- mongo(collection = "scores", db = "kenblog", url = "mongodb://localhost")

The mongo connection method accepts the following arguments:

  • collection
  • db
  • url
  • verbose
  • options

The collection and db arguments allow for the specification of the names of the respective database information. url is the mongo connection string in URI format. If you need additional output from the connection one can set the verbose boolean value to TRUE. Additional connection options, such as SSL information, can also be passed in.

With a connection established, let’s query our scores collection for exam data.

examQuery <- connection$find('{"type": "exam"}')

This brings in our 585 exam documents. We can then create a vector of the exam scores and have a look at their summary.

> exam_scores <- examQuery[c('score')] 
> summary(exam_scores)
     score       
 Min.   : 60.00  
 1st Qu.: 72.00  
 Median : 79.00  
 Mean   : 79.45  
 3rd Qu.: 86.00  
 Max.   :100.00 

Personally, I’m already liking the syntax of this new R driver for doing queries and working with MongoDB in R.

Other methods that will be familiar to MongoDB users are drop() to drop a collection, aggregate for aggregation pipeline operations, and insert for creating information in the database. There are many additional methods that can be used that allow for map-reduce operations and importing or exporting JSON or BSON data, and many more.

Further, with support for features such as indexing, encryption, and authentication, this new R driver is much more robust than previous options.

Wrap Up

Connecting to MongoDB from R is pretty straightforward and simple using the new R driver. It is the new “official” and supported method to leverage the power of R with the flexibility and power of MongoDB.

R is a great statistical language and can definitely be used to query and analyze MongoDB collections. If you are using R in your work today, this new way of connecting with MongoDB is definitely worth a look. If R is still new to you, Learning R: A Step-by-Step Function Guide to Data Analysis is a great way to get started. Or the R For Dummies book may be of interest as well.

This post was updated on 5 Jan 2020.


Follow me on Twitter @kenwalger to get the latest updates on my postings.

There are a few MongoDB specific terms in this post. I created a MongoDB Dictionary skill for the Amazon Echo line of products. Check it out and you can say “Alexa, ask MongoDB for the definition of a document?” and get a helpful response.

Facebooktwitterredditlinkedinmail