React Native

We can use LiteSync on React Native apps on both Android and iOS using the react-native-litesync plugin


Instructions

Install the required plugins on your app:

# using yarn yarn add react-native-litesync react-native-udp # using npm npm install react-native-litesync react-native-udp

Then run:

cd ios && pod install && cd ..

To install the free version of LiteSync native libraries, execute the following:

wget http://litesync.io/download/litesync.aar wget http://litesync.io/download/litesync-free-ios-native-libs.tar.gz tar zxvf litesync-free-ios-native-libs.tar.gz lib mv lib node_modules/react-native-litesync/platforms/ios/ mv litesync.aar node_modules/react-native-litesync/platforms/android/

When moving to the full version just copy the libraries to the respective folders as done above, replacing the existing files.



Example Code

var SQLite = require('react-native-litesync')

on_error = (err) => {
  console.log("Error:", err);
}

on_success = () => {
  console.log("SQL executed");
}

on_db_open = () => {
  console.log("The database was opened");
}

// open the database
var uri = "file:test.db?node=secondary&connect=tcp://server:port";
var db = SQLite.openDatabase({name: uri}, on_db_open, on_error);

db.on('error', on_error);  // this should be the first callback

db.on('not_ready', () => {
  // the user is not logged in. show the login screen (and do not access the database)
  ...
});

db.on('ready', () => {
  // the user is already logged in or the login was successful. show the main screen
  ...
});

db.on('sync', () => {
  // the db received an update. update the screen with new data
  show_items();
});

insert_items = () => {
  db.transaction((tx) => {
    // CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, name, done, row_owner)
    tx.executeSql("INSERT INTO tasks (name,done) VALUES ('Learn React Native',1)", []);
    tx.executeSql("INSERT INTO tasks (name,done) VALUES ('Use SQLite',1)", []);
    tx.executeSql("INSERT INTO tasks (name,done) VALUES ('Test LiteSync',0)", []);
  }, () => {
    // success callback = transaction committed
    show_items();
  }, on_error);
}

show_items = () => {
  db.executeSql('SELECT * FROM tasks', [], (result) => {
    // Get rows with Web SQL Database spec compliance
    var len = result.rows.length;
    for (let i = 0; i < len; i++) {
      let task = result.rows.item(i);
      console.log(`Task: ${task.name}, done: ${task.done}`);
    }
    // Alternatively, you can use the non-standard raw method
    /*
    let tasks = result.rows.raw();  // shallow copy of the rows Array
    tasks.forEach(row => console.log(`Task: ${task.name}, done: ${task.done}`));
    */
  });
}


Sample Project

Here is a sample React Native application that uses LiteSync:

Download


Extract it to a folder and then run:

yarn cd ios && pod install && cd .. wget https://litesync.io/download/litesync.aar wget https://litesync.io/download/litesync-free-ios-native-libs.tar.gz tar zxvf litesync-free-ios-native-libs.tar.gz lib mv lib node_modules/react-native-litesync/platforms/ios/ mv litesync.aar node_modules/react-native-litesync/platforms/android/

Then you can run the app on the simulator with either of these:

react-native run-android react-native run-ios


For more information check the react-native-litesync plugin page