ObjectStore put syntax - dart

I'm having trouble with the new syntax (r18915) for idb.ObjectStore.put. Could someone please help?
example is below which results in error as follows :
AsyncError: ‘Error:DataError: DOM IDBDatabase Exception 0’
Stack trace: #0 ObjectStore._put_2(file:///E:/b/build/slave/dartium-win-full-trunk/build/src/build/Release/obj/
global_intermediate/webkit/bindings/dart/indexed_db/ObjectStore.dart:141:3) #1
ObjectStore.$dom_put(file:///E:/b/build/slave/dartium-win-full-trunc/build/src/build/Release/obj/
global_intermediate/webkit/bindings/dart/indexed_db/ObjectStore.dart:137:18) #2
ObjectStore.put(file:///E:/b/build/slave/dartium-win-full-trunc/build/src/build/Release/obj/
global_intermediate/webkit/bindings/dart/indexed_db/ObjectStore.dart:9:27)
The code I'm using which was working but has been modified for new release as follows :
Future fDbAddOrUpdateClient(String sKey1, ClassClientData clClientData) {
idb.Transaction oDbTxn = ogDb1.transaction(sgStoreClient, 'readwrite');
idb.ObjectStore oDbStoreClient = oDbTxn.objectStore(sgStoreClient);
Completer completer = new Completer();
var oDbReqPut = oDbStoreClient.put(
{'sKey': sKey1,
'sNameTitle' : clClientData.sNameTitle,
'sNameFamily' : clClientData.sNameFamily,
'sNameGiven1' : clClientData.sNameGiven1,
'sNameGiven2' : clClientData.sNameGiven2
})
.then((val){
completer.complete(val);
return;
})
.catchError((e){
window.alert("${e}");
return;
});
}

Apart from the future you specify your function returns but never does, only a completed value (as mentioned in other comment), the future syntax appears correct. The error itself actually indicates that the data being passed to your objectstore.put command is invalid.
See IndexedDB Exceptions. You may need to verify the data that is being passed to the map.

Thanks for the help. The main problem appeared to be with Database not opening or something related such as ObjectStore not being created, but no error shown.
Hopefully the following is better code (r19425) :
Future future = fDbAddOrUpdateClient(sKey, clClientData)
.catchError((oError) => window.alert("${oError}"));
}
Future fDbAddOrUpdateClient(String sKey1, ClassClientData clClientData) {
idb.Transaction oDbTxn = ogDb1.transaction(sgStoreClient, 'readwrite');
idb.ObjectStore oDbStoreClient = oDbTxn.objectStore(sgStoreClient);
return oDbStoreClient.put(fMapClient(sKey1, clClientData));
}

Related

trying to store image as base64 and using it

I have a block of code that I found online and it seems to be working and not working at the same time. I think its probably my lack of understanding but I cant seem to get it to work the way I want it.
selectPicture() {
let context = imagepicker.create({
mode: "single" // use "multiple" for multiple selection
});
var imageBase64
context
.authorize()
.then(function() {
return context.present();
})
.then(function(selection) {
selection.forEach(function(selected) {
imageSourceModule.fromAsset(selected).then((imageSource) => {
imageBase64 = imageSource.toBase64String("jpg",60);
console.log("Image saved successfully!")
console.log(imageBase64)
console.log("test test") //runs fine
this.image = "~/assets/images/account/camera.png" //cant seem to run
console.log("test test 2")
}).catch(function (e) {
// process error
console.log("got error 1")
});
})
}).catch(function (e) {
// process error
console.log("got error 2")
});
},
Within the imageSourceModule.fromAsset(selected).then((imageSource), I was trying to save the base64 info in another variable but cant seem to do anything within other than console log a string. When I run this.image = "~/assets/images/account/camera.png" (just a placeholder, even calling a method does not work too) for example it catches an error.
What could the problem be? thank you!
UPDATE
I changed console.log("got error 1") to log the actual update and what i got was:
undefined is not an object (evaluating 'this.image = "~/assets/images/account/camera.png"')*
I now think that theres a problem with my understanding calling variable outside. My variable 'image' is within the script at
data() {
return {
image : ""
}
}
first of all check what this variable is, because you do not use es6 arrow functions, so this is probably not the vue instance.
the second thing: when you change vue-variables asynchronously use the $set method, like: this.$set(this, 'image', '~/assets/images/account/camera.png')

C# - Xamarin - HttpClient - Operation is not valid due to the current state of the object - iOS

I'm working on a cross platform library that makes HTTP requests. It's working fine on Android, but when I try to use it on iOS I'm getting an exception and I can't figure out how to fix it.
Here is my code:
// method from cross platform library
Task.Factory.StartNew(delegate
{
try
{
var client = new HttpClient();
// some other setup stuff
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.post, "http://myurl.com...");
var task = client.SendAsync(request);
task.Wait(); // Exception thrown on this line
var response = task.Result;
var responseString = response.Content.ReadAsStringAsync().Result;
}
catch (Exception e)
{
}
}
On task.Wait(); I get a System.AggregateException with an inner exception of System.InvalidOperationException that says Operation is invalid due to the current state of the object.
Trying to find some solutions, I found that the issue could be cause by calling this on the UI thread. But that's the whole point of wrapping this all in Task.Factory.StartNew.
I've tried everything I know to do and have yet to solve the issue. Any help would be very appreciated.
Edit:
I decided to try my solution on an iPhone simulator. It's an iPhone 6 simulator running iOS 10. My physical device is the same. It works on the simulator, but not the physical device for some reason... very strange.
Edit 2:
Thanks to #YuriS for finding a solution.
From: https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec
What you can do is:
1) Go to References of ios Project
2) Edit References
3) Check 'System.Net.Http'
Behaviour for android is the same.
There can be few problems described here:
https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec
https://bugzilla.xamarin.com/show_bug.cgi?id=17936
"Operation is not valid" error at Xamarin.iOS project with HttpClient
http://motzcod.es/post/78863496592/portable-class-libraries-httpclient-so-happy
Seems all post pointing on System.Net.Http
Regardless of the problem there is a better ways doing this.One of them:
public static async Task PostRequest()
{
try
{
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://myuri");
//request.Headers.Add("", "");
var response = await client.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
}
}
If you want to wait till function completes you call
await PostRequest();
If you don't need to wait then just omit "await" in the call or use
PostRequest.ContinueWith((t)=>
{
});
Also you need to handle an exception within the function, so probably returning just Task is not the best. I was just basing my answer on original function signature

Why is should.be.type() failing with "TypeError: (intermediate value).should.be.type is not a function"

I do not understand why the following test is failing with the error:
TypeError: (intermediate value).should.be.type is not a function
describe('#Option object', function() {
it('returns value as whatever type was passed to the constructor', function() {
var o = function() {
this.getValue = function() {
return new Date();
}
};
var i = new o();
i.getValue().should.be.type('Date');
})
});
I've read [most] of the Should.js documentation but I must be missing something. Can anyone tell me what is wrong with my test?
Actually only one thing wrong. You read not should.js docs, but unit.js docs - it is not related to should.js at all.
Correct link.
Correct code will be:
i.getValue().should.be.instanceOf(Date);
or
i.getValue().should.be.Date();

Using websockets/Server sent events with Angular dart

I was trying to use angular dart with websockets/server sent events and could not find any documentation/examples (there are some for angularJS but that seems very different for such things). A few things I tried also did not work.
Does anyone know how to do this?
Here is one version of what I tried and the error:
#NgController (
selector: "ACdistribution",
publishAs : "dstbn")
class ACDstbnController{
List <WtdPres> distbn;
void updateDstbn(List<WtdPres> newdstbn){
distbn = newdstbn;
}
final dstbnsrc = new EventSource("../dstbns")
..onMessage.listen((event){
List wps = JSON.decode(event.data);
List <WtdPres> newdistbn = wps.map((wp) => new WtdPres.fromJson(wp));
updateDstbn(newdistbn);
});
}
The error I got in pub build was:
web/provingground.dart:55:5:
'updateDstbn' is only available in instance methods.
updateDstbn(newdistbn);
^^^^^^^^^^^
There are limitations on what you can do on initializers for final fields.
Can you try to put this code inside the constructor
var dstbnsrc;
ACDstbnController() {
dstbnsrc = new EventSource("../dstbns")
..onMessage.listen((event){
List wps = JSON.decode(event.data);
List <WtdPres> newdistbn = wps.map((wp) => new WtdPres.fromJson(wp));
updateDstbn(newdistbn);
});
}

Dart HttpRequest polling

I have a web app that have a Timer that fires a poll to get data every 3 seconds. It works fine for about 2.5 minutes then Chromium crashes.
My request Dart looks like this
HttpRequest.getString('data/get_load_history_recent.json')
.then((e) => _recentHistoryResponse(e))
.catchError((e) => _recentHistoryError(e));
Can you think of any reasons why this would happen? I assume it's a memory leak...
Edit:
Here is my _recentHistoryResponse()
void _recentHistoryResponse(String data)
{
Map obj = JSON.decode(data);
if(obj['status'] == 'success')
{
List processes = obj['data']['processes'];
List newItems = new List();
List oldIdsArray = new List();
int length = appDataDic.load_history_list.length;
for(HistoryDataVO oldVO in appDataDic.load_history_list)
{
oldIdsArray.add(oldVO.loadID);
}
for(Map process in processes)
{
HistoryDataVO dataVO = new HistoryDataVO();
dataVO.loadID = process['loadID'];
dataVO.time = process['time'];
dataVO.loadType = process['loadType'];
dataVO.fileName = process['fileName'];
dataVO.label = process['label'];
dataVO.description = process['description'];
dataVO.count = process['count'];
dataVO.progress = process['progress'];
dataVO.loadTask = process['loadTask'];
// Check if the item is currently in the list
if(length >= 1)
{
if(!LoadHistoryHelper.exists(oldIdsArray, dataVO.loadID))
{
dataVO.isNew = true;
}
}
newItems.add(dataVO);
}
appDataDic.load_history_list.clear();
appDataDic.load_history_list.addAll(newItems);
}
}
I have commented out the exists check !LoadHistoryHelper.exists(oldIdsArray, dataVO.loadID)) (because this seemed like the obvious place) but it the VM still crashes.
Also, I have taken this same code and put it into an isolated app with the only real difference in the poll check is appDataDic.load_history_list is just an #observable List, not an ObservableList.
Edit 2 :
Ok, so I have discovered that Map obj = JSON.decode(data); causes the crash. I was reading in a Javascript forum that timeouts cause the memory to not be released (I had never thought of this but it makes sense), is this true? Can any one think of a better way to do this? Can I directly call the garbage collection? I'm running out of ideas.
There's another question here, suggesting a memory leak in HttpRequest; however I'm not able to find anything in the Dart issue tracker. If you think this might be a real memory leak, it might be worth raising a bug.

Resources