Realm Data Insertion Took Over 7Mins for Big Size JSON

Realm data Insertion took over 7mins for big size json

insertUserData method method opens transactions so many times in the loop. To commit transaction is a little bit expensive operation.

Can you try to put out to open/commit a transaction outside of the loop?
In other words, open the transaction before entering the loop, and commits the transaction once after the end of the loop. Like the following:

if results.count > 0 {
if let users = results.array {
let realm = try! Realm()
try realm.write {
for user in users{
let userList=UserList()
userList.start=user["Start”].stringValue
userList.end=user[“End”].stringValue
userList.name=user[“Name”].stringValue
userList.address =user[“Address”].stringValue
realm.add(userList,update: true)
}
}
}
}

Maximum call stack size exceeded error

It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you hit the call stack limit.

This is almost always because of a recursive function with a base case that isn't being met.

Viewing the stack

Consider this code...

(function a() {
a();
})();

Here is the stack after a handful of calls...

Web Inspector

As you can see, the call stack grows until it hits a limit: the browser hardcoded stack size or memory exhaustion.

In order to fix it, ensure that your recursive function has a base case which is able to be met...

(function a(x) {
// The following condition
// is the base case.
if ( ! x) {
return;
}
a(--x);
})(10);

Android - Storing 'large' data locally

The SQLite is binary format that is relatively fast.
Use SQLite or Realm. Imho, Realm (based on SQLite) engine might be better in your case (it faster that pure SQLite).

Realm vs Sqlite for mobile development

Nowadays there is a Firebase Database from Google. Imho, you can you this format too because of it has both server and offline interactions. Because of both SQLite formats must be stored in APK file. But in order to work inside the application it is needed that sqlite-file have to be copied from assets (or raw) directory into database (workable) directory. That's why all data would be dublicated(!). Firebase Database do not have this disadvantage.

https://firebase.google.com/docs/database/android/start/

JSON and XML formats are too huge (and memory and machine time consumed) to work with them.

Oh. forgot! it is possible to integrate your code directly inside the code. Just create a class to work:)



Related Topics



Leave a reply



Submit