No return of "yield return WWW" method in the second run - debugging

I'm tracking the following method in Unity3D environment:
(called through StartCoroutine)
private IEnumerator post(string s) {
Debug.Log("Posting to Server: " + s);
WWWForm form = new WWWForm();
form.AddField("data", s);
WWW yaPoster = new WWW(YMCA_URL,form);
Debug.log("11111");
yield return yaPoster;
Debug.log("22222");
if(yaPoster.error != null) {
Debug.Log(yaPoster.error);
}
else {
Debug.Log(yaPoster.text);
}
yaPoster = null;
}
It works well for every first time (saw yaPoster.text from log console), but never can see the "11111" or "22222" in the followed calls.
(Not) surprisingly, things run well on my iPod device with the built iOS Xcode project.
Any specific settings I should apply for my Unity3D environment so that I don't need to debug my code only with the real device?

Looks like it should work just fine!
A wild guess: Turn off Collapse in the Console? Could be that you're just not seeing it the second time because identical messages are not being shown if Collapse is enabled
If you want to keep Collapse enabled, change your Debug.Logs to include Time.time like this:
Debug.Log(Time.time + ": " + "11111"); //adding some uniqueness to keep it from collapsing
(Sorry, don't have enough rep to post this as a comment)

Related

Responsive asynchronous search-as-you-type in Java 8

I'm trying to implement a "search as you type" pattern in Java.
The goal of the design is that no change gets lost but at the same time, the (time consuming) search operation should be able to abort early and try with the updated pattern.
Here is what I've come up so far (Java 8 pseudocode):
AtomicReference<String> patternRef
AtomicLong modificationCount
ReentrantLock busy;
Consumer<List<ResultType>> resultConsumer;
// This is called in a background thread every time the user presses a key
void search(String pattern) {
// Update the pattern
synchronized {
patternRef.set(pattern)
modificationCount.inc()
}
try {
if (!busy.tryLock()) {
// Another search is already running, let it handle the change
return;
}
// Get local copy of the pattern and modCount
synchronized {
String patternCopy = patternRef.get();
long modCount = modificationCount.get()
}
while (true) {
// Try the search. It will return false when modificationCount changes before the search is finished
boolean success = doSearch(patternCopy, modCount)
if (success) {
// Search completed before modCount was changed again
break
}
// Try again with new pattern+modCount
synchronized {
patternCopy = patternRef.get();
modCount = modificationCount.get()
}
}
} finally {
busy.unlock();
}
}
boolean doSearch(String pattern, long modCount)
... search database ...
if (modCount != modificationCount.get()) {
return false;
}
... prepare results ...
if (modCount != modificationCount.get()) {
return false;
}
resultConsumer.accept(result); // Consumer for the UI code to do something
return modCount == modificationCount.get();
}
Did I miss some important point? A race condition or something similar?
Is there something in Java 8 which would make the code above more simple?
The fundamental problem of this code can be summarized as “trying to achieve atomicity by multiple distinct atomic constructs”. The combination of multiple atomic constructs is not atomic and trying to reestablish atomicity leads to very complicated, usually broken, and inefficient code.
In your case, doSearch’s last check modCount == modificationCount.get() happens while still holding the lock. After that, another thread (or multiple other threads) could update the search string and mod count, followed by finding the lock occupied, hence, concluding that another search is running and will take care.
But that thread doesn’t care after that last modCount == modificationCount.get() check. The caller just does if (success) { break; }, followed by the finally { busy.unlock(); } and returns.
So the answer is, yes, you have potential race conditions.
So, instead of settling on two atomic variables, synchronized blocks, and a ReentrantLock, you should use one atomic construct, e.g. a single atomic variable:
final AtomicReference<String> patternRef = new AtomicReference<>();
Consumer<List<ResultType>> resultConsumer;
// This is called in a background thread every time the user presses a key
void search(String pattern) {
if(patternRef.getAndSet(pattern) != null) return;
// Try the search. doSearch will return false when not completed
while(!doSearch(pattern) || !patternRef.compareAndSet(pattern, null))
pattern = patternRef.get();
}
boolean doSearch(String pattern) {
//... search database ...
if(pattern != (Object)patternRef.get()) {
return false;
}
//... prepare results ...
if(pattern != (Object)patternRef.get()) {
return false;
}
resultConsumer.accept(result); // Consumer for the UI code to do something
return true;
}
Here, a value of null indicates that no search is running, so if a background thread sets this to a non-null value and finds the old value to be null (in an atomic operation), it knows it has to perform the actual search. After the search, it tries to set the reference to null again, using compareAndSet with the pattern used for the search. Thus, it can only succeed if it has not changed again. Otherwise, it will fetch the new value and repeat.
These two atomic updates are already sufficient to ensure that there is only a single search operation at a time while not missing an updated search pattern. The ability of doSearch to return early when it detects a change, is just a nice to have and not required by the caller’s loop.
Note that in this example, the check within doSearch has been reduced to a reference comparison (using a cast to Object to prevent compiler warnings), to demonstrate that it can be as cheap as the int comparison of your original approach. As long as no new string has been set, the reference will be the same.
But, in fact, you could also use a string comparison, i.e. if(!pattern.equals(patternRef.get())) { return false; } without a significant performance degradation. String comparison is not (necessarily) expensive in Java. The first thing, the implementation of String’s equals does, is a reference comparison. So if the string has not changed, it will return true immediately here. Otherwise, it will check the lengths then (unlike C strings, the length is known beforehand) and return false immediately on a mismatch. So in the typical scenario of the user typing another character or pressing backspace, the lengths will differ and the comparison bail out immediately.

xamarin non-english dot and comma calculations (Xamarin multi-language support)

We are developing a mobile application using Xamarin cross platform development for Android and iOS, which is reading data from a Bluetooth device that transfers data as a float i.e. 12.22 and it will support multiple languages.
When it's set to English all our calculations are fine, but when set to non-english the calculations go completely wrong. I know what is wrong in non-english languages it's treating the "." as an "," which is means a 12.22 becomes 12,22. Therefore, screwing up our calculations.
I have found similar problems, but they force the complete language to English which means we lose the translations and other fixes look very complex for what must be a simple fix.
Is there a easy fix to this problem, like setting a flag in the Xamarin multi-language support
Update :
With the BLE transfer code, we can see the string but when it convert it we get 6000000 for French or 60 for English.
private async Task<double>
GetDatastoreValue(Plugin.BLE.Abstractions.Contracts.IService service, string param)
{
try
{
var datastore_param_char = await service.GetCharacteristicAsync(Guid.Parse(datastore_param_uid));
var datastore_param_val = await service.GetCharacteristicAsync(Guid.Parse(datastore_value_uid));
await datastore_param_char.WriteAsync(Encoding.ASCII.GetBytes(param));
byte[] result = await datastore_param_val.ReadAsync();
string rslt = Encoding.ASCII.GetString(result);
Debug.WriteLine("GetDatastoreValue -> result " + result);
double val = Convert.ToDouble(rslt);
Debug.WriteLine("GetDatastoreValue -> val -> "+ val);
return val;
}
catch
{
return 0.0;
}
}
instead of using this
double val = Convert.ToDouble(rslt);
do this
double val = Double.Parse(rslt, CultureInfo.InvariantCulture);
this will force it to evaluate "123.45" using the decimal separator, regardless of what the user's local culture might be set to

How to get Clutter.Actor via its name

I am using GJS, how do I get Clutter.Actor via its name. For example, if I wanted to get GNOME Shell's top panel, how do I get its Clutter.Actor via its name "panel"?
My research ended up somewhere along Clutter.Stage which is where Actor(s) can go be appended to, however by the way I see things, there can be multiple Stages setup so I might also have to find which Stage it is the Actor I am trying to find is at. For now I want to know how I can get an Actor via its name.
I have seen from a code; Main.layoutManager.panelBox to get the GNOME Shell's top panel, however that doesn't seem applicable to my case since it's a third party Actor I am trying to get, and the way I wish to get Actor(s) is via the name since I may be working with different third party Actor(s).
There is one way that I can get this that I know of; Main.layoutManager.panelBox.get_parent().get_children() and I can just get the specific Actor via its index, but I don't think this is the best way to approach this, considering how dynamic things are, secondly, I find this way kinda sloppy so..
I was able to get the name via Looking Glass (Alt + F2 -> lg -> picker). For now, the specific Actor I am trying to get is the DashtoDock's, just for info.
Thank you~ Hope someone can help.
Unfortunately, it seems like what you're looking for is a searchByName() function, but you'll have to implement that yourself I think. Something like (untested):
function searchByName(topActor, name) {
let children = topActor.get_children();
for (let i = 0; i < children.length; i++) {
if (child.name === name) {
return child;
} else if (child.get_n_children()) {
let result = searchByName(child, name);
if (result) {
return result;
}
}
}
return false;
};
Then call it on Main.layoutManager.uiGroup where Dash to Dock is
const Main = imports.ui.main;
let dashToDock = searchByName(Main.layoutManager.uiGroup, "dashtodockContainer");

How can I listen for the deletion of a ProjectItem via DTE?

I've got a designer that relies on the existence of other solution items. If one of those items is deleted the designer crashes and you have to edit as XML to fix. Not exactly user friendly.
I do, however, have the DTE object representing the instance of Visual Studio, as well as the ProjectItems I am dependent on.
Is it possible to, somewhere in the depths of the DTE, register a listener for the deletion of that ProjectItem? And, if so, How would I do it?
It looks like the culprit here is garbage collection. I found the following two event sets behaved identically.
Events2 events2 = dte.Events as Events2;
if (events2 != null)
{
this.projectItemsEvents = events2.ProjectItemsEvents;
this.projectItemsEvents.ItemAdded += this.ProjectItemsEvents_ItemAdded;
this.projectItemsEvents.ItemRemoved += this.ProjectItemsEvents_ItemRemoved;
this.projectItemsEvents.ItemRenamed += this.ProjectItemsEvents_ItemRenamed;
}
this.csharpProjectItemsEvents =
dte.Events.GetObject("CSharpProjectItemsEvents") as ProjectItemsEvents;
if (this.csharpProjectItemsEvents != null)
{
this.csharpProjectItemsEvents.ItemAdded += this.CSharpProjectItemsEvents_ItemAdded;
this.csharpProjectItemsEvents.ItemRemoved += this.CSharpProjectItemsEvents_ItemRemoved;
this.csharpProjectItemsEvents.ItemRenamed += this.CSharpProjectItemsEvents_ItemRenamed;
}
The key to both was making sure to keep a reference to the events object in the subscriber. Once I added the reference, they behaved like I expected.
private ProjectItemsEvents projectItemsEvents;
private ProjectItemsEvents csharpProjectItemsEvents;
Check out this FAQ article which explains how to register for ProjectItems events (including ItemDeleted).

Conversion of Gdk Events in Mono

I'm trying to intercept events using Gdk.Window.AddFilter(Gdk.FilterFunc) in Mono. So far, I have been able to hook up the filter function, but now I am trying to use the events in the filter function.
This is what I have in the filter function so far:
private Gdk.FilterReturn FilterFunction(IntPtr xEvent, Gdk.Event evnt)
{
if (evnt.Type == Gdk.EventType.KeyPress)
{
Gdk.EventKey eventKey = (Gdk.EventKey)evnt; // fails here
if (eventKey.Key == this.key && eventKey.State == this.modifiers)
{
this.OnPressed(EventArgs.Empty);
}
}
return Gdk.FilterReturn.Continue;
}
How can I convert the Gdk.Event to Gdk.EventKey? I have tried casting it, but that doesn't seem to work.
Edit: Oops! The problem was that I had accidentally added a semicolon to the if statement, making it an empty statement. For some reason, the Gdk.Event does not correspond to the XEvent, so I am now pursuing a solution that uses the XEvent instead.
Why don't you try printing out the type so you can see what it really is? (it may not be EventKey)
Like:
Console.WriteLine (evnt.GetType ());
(or pause it in a debugger and examine it to see the type)

Resources