Getting a unique object from a indexedDB in Dart - dart

I'm trying to get information from an indexedDB, and I want to get just the object from one store by key, and not all the objects from the database. In some examples of javascript and IndexedDB they use the method get() to get the value depending of the key. In DART there is not such method but there is getObject().
How do I get the value of the object when using getObject(key)?
Thanks a lot in advance!

Actually it has getObject(key) method, take a look here https://api.dartlang.org/apidocs/channels/stable/#dart-dom-indexed_db.ObjectStore#id_getObject
The other way for fetching data is Cursor. Here is a good tutorial https://www.dartlang.org/docs/tutorials/indexeddb/#getting-data

So all needed to do was to create a callback. This is how I got the objects value:
var id = "example";
var trans = _db.transaction('myDB', 'readwrite');
var store = trans.objectStore(_TODOS_STORE);
// Get the value from one object ! and render it
var request = store.getObject(id).then((val){functionFor(val);});
Futures need from callbacks to be able to use their values.

Related

Qmetry- How to store and access Array of array list of strings

In Qmetry,Trying to save array of arraylist string and access same in another test case.
Array of array list :
ArrayList<ArrayList<String>> my_list
store(my_list, "array_list_1");
//Accessing saved list
Object list_details = getBundle().getObject(array_list_1);
System.out.println("++++ saved list details++++" + list_details);
I am able to print list_details content. Till this there is no issue. But when I try to get the first index within the arraylist, not able to use .get(0) method. Below is the code.
ArrayList<String> list_details1 = list_details.get(0);
When tried typecasting , got an error 'java.lang.String cannot be cast to java.util.ArrayList'
typecast:
ArrayList<ArrayList<String>>list_details1 = (ArrayList<ArrayList<String>>)list_details;
Need to know is it the right way to store and access arraylist ? Please suggest.
if you want store in file, you can use .csv or .xls to save ArrayList<ArrayList> my_list, if ArrayList<ArrayList> my_list you query from database, you can set as static variable, then you can use in different method

How to access map keys through index? Dart

dartques = {'Color':[], 'Fruits':[], 'Hobbies':[]};
How to access the values using index in map?
I need to access only key or value using index.
Just like we do in list
=>list[1]
you can get it like this
var element = dartques.values.elementAt(0);
also for Dart 2.7+ you can write extension function
extension GetByKeyIndex on Map {
elementAt(int index) => this.values.elementAt(index);
}
var element = dartques.elementAt(1);
For accessing the values by index, there are two approaches:
Get Key by index and value using the key:
final key = dartques.keys.elementAt(index);
final value = dartques[key];
Get value by index:
final value = dartques.values.elementAt(index);
You may choose the approach based on how and what data are stored on the map.
For example if your map consists of data in which there are duplicate values, I would recommend using the first approach as it allows you to get key at the index first and then the value so you can know if the value is the wanted one or not.
But if you do not care about duplication and only want to find a value based on Index then you should use the second approach, as it gives you the value directly.
Note: As per this Answer By Genchi Genbutsu, you can also use Extension methods for your convenience.
Important:
Since the default implementation for Map in dart is LinkedHashmap you are in great luck. Otherwise Generic Map is known for not maintaining order in many programming languages.
If this was asked for any other language for which the default was HashMap, it might have been impossible to answer.
You can convert it to two lists using keys and values methods:
var ques = {'Color':['a'], 'Fruits':['b'], 'Hobbies':['c']};
List keys = ques.keys.toList();
List values = ques.values.toList();
print (keys);
print (values);
The output:
[Color, Fruits, Hobbies]
[[a], [b], [c]]
So you can access it normally by using keys[0], for example.

How to send multiple array in JSON in asp.net mvc?

I have a 3 <tr></tr> in my table. user can edit them and on the save button I send the data to the server.
first is main and other all is child of the main. When someone click on new button a new child is created.
now I am thinking to maintain the information like this
var minf = {};
minf.main = $("#tr" + curSplidId).find('input,select').serialize();
res.each(function(n) {
var i = $(this).find('input,select').serialize();
minf[n] = i;
});
All I am trying to do is getting main object and array of childs in JsonResult, I have tried to use Dictionary for JSON.stringify values.
None of these works.
Someone please help me to get it done. Through my testing I found in a case it's sending me querystring in my minf object (I does Stringify) but I am not sure how to handle it on JSONResult as some kind of dictionary stuff where I can read it through the keys.
suppose you have a 3 tr. the best approach is to use a loop on all the tr and create a object same to the DTO that is using in action and make a list of it.
var MainList=[];
$('tr').each(function(){
var MainDTO={};
MainDTO.Column1=$(this).find('td').html();
MainDTO.Column2=$(this).find('td').html();
MainList.add(MainDTO)
});
and pass this MainList directly to action. no serialise required
I think for converting data to json and read that json on server. I make the whole process hard for JS and server to handle.
I change the strategy to serialize all the element and send it to server. it's work fine.
now if I need to read id then I can parse the id which is 3 and come in comma based values.

Connecting remote search results with local database using CoreData

Assume we have simple data model with single entity User; simple tableView_friends with fetchedResultsController_friends for show users - friends.
Assume we have search bar for searching all (not only friends) users in service, and for every typed in it character we perform search request to server, which return to us somehow filtered by character User objects. Some of this objects can already be inside local database. By app logic we don't really must save all this results in local database forever (but ok, we can, we can clear local database time to time); on other hand, if we will perform any action on some searched user, we must store this user. We want to show list of searched user in other tableView_search with fetchedResultsController_search.
Question: should I use same context for fetchedResultsController_friends and fetchedResultsController_search? If no, how can I handle situation, when I wish to edit searched user, which already exists in database and probably already local edited? If yes, how can I setup predicate for fetchedResultsController_search (server perform its own logic for search by character, which can be changed) for show exactly same result as from server?
We recently implemented a search feature in our application and had a similar issue, We had local data in core data and also remote data from our API.
You have a few options that we explored:
Save your data into core data from the API as it is retreived and
then the fetched results controller will do the rest
Manage the merge of the data yourself, you can still use NSFetchedResults controller to an extent but need to do more work
We didn't want to save all of the information returned from the API unless it was needed (the user selected it), so we come up with a simple solution that worked for our app. This may not work directly for your app, you may need a completely different solution or change some of the things we done to suit.
Firstly, To explain what we are dealing with, we had a Article entity in core data which contains around 25 properties, the API returns article objects as JSON data with the same data.
What we decided to do was to create a class which represents a simple version of an article (just enough data to show in a list view and reference it later in the API or core data) which looked something like this:
class SearchResult: NSObject {
var id:String?
var title:String?
var imageUrl:String?
var url:String?
// core data entity
init(article:Article) {
self.id = content.contentId
self.title = content.title
self.featuredImageURL = content.absoluteImagePath()
self.urlAlias = content.urlAlias
self.publishedAt = content.publishedAt
}
init(articleDictionary:NSDictionary) {
self.id = articleDictionary.objectForKeyNotNull("id") as? String
self.title = articleDictionary.objectForKeyNotNull("title") as? String
self.url = articleDictionary.objectForKeyNotNull("url") as? String
if let imageUrl = articleDictionary.objectForKeyNotNull("imageUrl") as? String {
self.imageUrl = imageUrl
}
}
}
Now using this, we can create once of these from either the core data results or from the API results. Our tableview datasource is just an array
var dataSet = [SearchResult]()
We use the NSFectchResultsController delegate methods to add/remove/re-order core data elements from the dataSet after the initial load and when we get API data we'll do something like:
dataSet = Array(Set(apiResponseArray + dataSet))
This will take an array of SearchResult items from the API, merge them with the current result set and remove duplicates. casting to a set and then back to an array will give you an array of unique results as a Set is made of unique values only.
See this reference which should help with how the delegate methods would work

How to traverse an array in reverse direction in dust.js?

Is there any simple solution, or it can be accomplished only by defining a custom helper?
The best solution is probably to have the server send the array in reverse order to begin with. In most cases, the "Dust way" is to have the server send data in the format that it will be presented by Dust. If you don't have control over how the data is sent, though, you will either need a helper, or you can manipulate the data (using JavaScript) before passing it to dust.render.
var data = getData();
var data.arrayToReverse = reverseArray(data.arrayToReverse);
dust.render('myDustTemplate', data, function(err, out) {
// Show the result.
});
You would need to write the getData and reverseArray methods, but this way you could get the reversed array without a helper.

Resources