Inserting Data into Realm Db with Progress

How to update progressView when downloading something in realm

First: defining a let constant with a value of 0 will always stay 0. You can't dynamically change that to reflect the state of your download.

As a Realm engineer pointed out in this post "Realm has no way to know the total amount of data."
Estimating the progress should be done in your code.

You could try something like this:

    func estimateProgress() {

let dataString = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."

let data = dataString.data(using: .utf8)
let count = Float((data?.count)!)

let percentage: Float = count / 100

var progress: Float = 0

if progress < count {
progress += percentage
} else {
progress = count
}

progressView.setProgress(progress, animated: true)
print(progress)

}

Please note that this solution is for updating the UI and letting the user know that the saving to Realm is in progress. Since it uses the amount of bytes in the data, the larger the data, the longer it will take for the progress bar to fill. So it is good for UI, but it is not the actual state of progress.

And if you feel the progress bar is filling to slow/fast you can always tweak the percentage.

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)
}
}
}
}

How to track progress in MongoDB Realm on initial sync?

You can specify SyncConfiguration's OnProgress property:

config = new SyncConfiguration("myPart", user)
{
OnProgress = progress =>
{
Console.WriteLine($"Progress: {progress.TransferredBytes}/{progress. TransferableBytes}");
}
}

Alternatively, you can use the synchronous realm.GetInstance method and immediately register the progress observable:

var realm = Realm.GetInstance(config);
realm.GetSession().GetProgressObservable(...)

The OnProgess property is equivalent to ProgressDirection.Download and ProgressMode.ForCurrentlyOutstandingWork.

Update realm file with new data

This may or may not help but you could do one of a few things.

If your file contains primary keys for each object, as you are reading in the file, ignore the ones that match ones you already read in.

Another option is to version it - attach a version number to each object and only read in objects that are after the last read version.



Related Topics



Leave a reply



Submit