Dart: indexed_db open version - dart

I need to use indexedDb for local storage.
When opening an indexedDb, the version is passed, and I presume that indicates whether an upgrade is needed. Can someone please explain what happens here and in particular the significance of the version, where the version is obtained from and also what an upgrade is?
For example :
import 'dart:indexed_db' as idb;
final int _iDbVersion = 1;
void fOpenDb(String sDbName) {
var request = window.indexedDB.open(sDbName, _iDbVersion);
request.on.success.add((e) => fDbOnOpened(request.result));
request.on.error.add(fDbOnOpenError);
request.on.upgradeNeeded.add((e) => fDbOnUpgradeNeeded(request.transaction));
}

I found this interesting description which appears to me to be largely correct. :
IndexedDb:
DATABASE
For every origin you can create an infinite number of databases. The only thing you need to create a database is an unique name. A database also has a version, this version will be used to determine the structure of the database. When a database is created for the first time, the version will be an empty string. Each database can only have one version at a time, this means the database can’t exist in multiple versions at once.
VERSION
The set of object stores can be changed, but it can only change by using the Version_change transaction. This transaction will change the version of the database and change the set of object stores as you defined.

Related

How to use Dart's Difference Method and DateTime using a dynamic date?

The application I'm building uses a sort of "Days since" counter using Duration Difference and DateTime, but I want the counter to begin on the day they started using the app by default, and have the option to put in whichever date they desire.
My Question is, how do i dynamically generate the start date of the counter to be the day they start using the app, and would this be easiest achieved with a database?
Ok, so from my point of view a good way to do it seems to use shared_preferences. Shared preferences allow to store some key/value pair for the user, it's persistent and you can update it.
Here's the package: https://pub.dartlang.org/packages/shared_preferences
Here's a good article giving you details about it : https://www.didierboelens.com/2018/04/shared-preferences---how-to-save-flutter-application-settings-and-user-preferences-for-later-re-use/
The idea would be to create a function, checking if the key of first_use_date exists.
If it does do nothing (or you can actually update it)
If not you create it with the date of today.
Of course it's persistent since the app stays installed on the device.
For printing duration I let you check this class : https://docs.flutter.io/flutter/intl/DateFormat-class.html
And especially this method (which is not implemented yet) : https://docs.flutter.io/flutter/intl/DateFormat/formatDurationFrom.html
Hope it's helps !!

Ruby on Rails: What is stale object and it's features?

In my application i got the error as con't update stale object. I don't know about this object. When it will create and why it was called a stale object. I want to know. Is it used in Only in ROR or other languges are also uses, this object and purpose?
According to the documentation:
http://api.rubyonrails.org/classes/ActiveRecord/StaleObjectError.html
Reason of Stale object exception is:
Record is stale when it's being saved in another query after instantiation, for example, when two users edit the same wiki page and one starts editing and saves the page before the other.
Solution: To avoid this exception use the locking system:
http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html
In my application multiple threads to be able to edit the same work -> version -> element record. its detecting error and raising the suitable error.
I typically did to handle the error:
I print a flash message such as "Sorry, please try after someone time, someone else editing this working version element".
element.lock_version = Document::Element.find(element.id).lock_version
element.save!
For example, such as detecting that one edit changed the element's type, where as the other edit changed the element's name by different creator, and then code of the two record should be merged done in one save.
In general, I used to element.reload to fetch a fresh version of the element.
worked fine!!

Not able to import EntityState Added

I'm having an issue with importing cached data. I have a very simple page right now to use as a proof of concept. There is a single table in the back end. I have all CRUD functionality working. When the user makes a change to the local data, I update a record in local storage.
var bundle = em.exportEntities(em.getChanges());
window.localStorage.setItem("waterLevelChanges", bundle);
after the page is loaded I import the entities
var bundle = window.localStorage.getItem("waterLevelChanges");
if (bundle)
em.importEntities(bundle);
This works perfectly if I'm editing an existing record. However, any records that I have added, but not saved to the database won't populate. In the bundle they have an EntityState of "Added". I read that there is an issue if you don't use the temp keys, but I'm letting Breeze use the temp keys and manage them as it likes. I have verified the data is stored in the local cache by looking in the developer tools. I can also see it in the bundle when I debug.
Our tests show that your stated scenario should work fine.
See this DocCode:export/importTests.js test where a new Order is exported, saved to browser storage, and reimported; it's "Added" state is confirmed. The Order type has server-generated, temporary keys.
I think you'll have to create a repro of your failing scenario to convince me that it doesn't work. Perhaps you might start by forking this Todos plunker and modifying it to make your point; a TodoItem also has has server-generated, temporary keys.

Updating core data performance

I'm creating an app that uses core data to store information from a web server. When there's an internet connection, the app will check if there are any changes in the entries and update them. Now, I'm wondering which is the best way to go about it. Each entry in my database has a last updated timestamp. Which of these 2 will be more efficient:
Go through all entries and check the timestamp to see which entry needs to be updated.
Delete the whole entity and re-download everything again.
Sorry if this seems like an obvious question and thanks!
I'd say option 1 would be most efficient, as there is rarely a case where downloading everything (especially in a large database with large amounts of data) is more efficient than only downloading the parts that you need.
I recently did something similiar.
I solve the problem, by assigning an unique ID and a global 'updated timestamp' and thinking about 'delta' change.
I explain better, I have a global 'latest update' variable stored in user preferences, with a default value of 01/01/2010.
This is roughly my JSON service:
response: {
metadata: {latestUpdate: 2013...ecc}
entities: {....}
}
Then, this is what's going on:
pass the 'latest update' to the web service and retrieve a list of entities
update the core data store
if everything went fine with core data, the 'latestUpdate' from the service metadata became my new 'latest update variable' stored in user preferences
That's it. I am only retrieving the needed change, and of course the web service is structured to deliver a proper list. Which is: a web service backed by a database, can deal with this matter quite well, and leave the iphone to be a 'simple client' only.
But I have to say that for small amount of data, it is still quite performant (and more bug free) to download the whole list at each request.
As per our discussion in the comments above, you can model your core data object entries with version control like this
CoreDataEntityPerson:
name : String
name_version : int
image : BinaryData
image_version : int
You can now model the server xml in the following way:
<person>
<name>michael</name>
<name_version>1</name_version>
<image>string_converted_imageData</image>
<image_version>1</image_version>
</person>
Now, you can follow the following steps :
When the response arrives and you parse it, you initially create a new object from entity and fill the data directly.
Next time, when you perform an update on the server, you increase the version count of an entry by 1 and store it.
E.g. lets say the name michael is now changed to abraham, then version count of name_version on server will be 2
This updated version count will come in the response data.
Now, while storing the data in the same object, if you find the version count to be same, then the data update of that entry can be skipped, but if you find the version count to be changed, then the update of that entry needs to be done.
This way you can efficiently perform check on each entry and perform updates only on the changed entries.
Advice:
The above approach works best when you're dealing with large amount of data updation.
In case of simple text entries for an object, simple overwrite of data on all entries is efficient enough. And this also keeps the data reponse model simple.

Google Document List Java API - Updated-min

It seems that using the DocumentQuery where using updatedMin seems to to not be working correctly. This query does find new files and folders that were created since the "when" but fails to return files that were modified, moved, trashed, or anything else since the when.
DocumentQuery myQuery = new DocumentQuery(new URL("https://docs.google.com/feeds/default/private/full/"));
myQuery.setUpdatedMin(when);
DocumentListFeed entries = getDocsService().getFeed(myQuery, DocumentListFeed.class);
I realize that the API might be changing with the upcoming Google Drive, has something changed in the interim?
To retrieve documents that have been edited since a specific date, use the edited-min query parameter.
From a DocumentQuery instance, you can use the addCustomParameter to set such a value.
query.addCustomParameter(
new Query.CustomParameter("edited-min", "<DATE_IN_RFC_3339_FORMAT>");

Resources