This is a general "best practices" question on the pros and cons of multiple asynchronous calls that need to be made in rapid succession.
In my specific instance I need to make 4 asynchronous calls from a silverlight application.
As it stands, we're simply displaying an indeterminate progress spinner until, in the callback, the spinner is hidden.
Now, the hypothetical I put forth. Is it better to make the 4 multiple calls in an "onion" fashion? Or is it better to make them one after another?
They're independent of each other in that the result of Call 1 does not affect 2, 3 or 4 and so on.
Scenario A:
Call 1 made ->
Call 2 made in Call 1 callback ->
Call 3 made in Call 2 callback ->
Call 4 made in Call 3 callback
Scenario B:
Call 1 made
Call 2 made
Call 3 made
Call 4 made
If the result of the previous calls have no effect on the subsequent ones than why wait? It's going to take longer if you do them in series rather than in parallel.
Related
I'm developing an application in which race conditions could occur when calling the Google Sheets API. This can happen due to multiple separate users executing the same use case with significantly different parameters. All requests from these users are handled separately and asynchronously.
For simplicity's sake I'll refer to these calls as call A and call B. Both being completely different and asynchronous requests. The values written in both calls will be completely different, hence the output in the read calls will return completely different values.
Normal situation:
Write call A
Read call A
Write call B
Read call B
Simultaneous calls:
Write call A
Write call B
Read call A (this now unintentionally fetches the result written by call B)
Read call B
When both actions are executed simultaneously a condition can occur that the write of call B could race the read of call A. This results in malformed output in the read of call A as the parameters of B are used.
Obviously I should improve the applications design by using a queue mechanism, but I'm hoping multiple users have bumped into this problem and that the API has a solution to this issue.
I've been nosing around in the API documentation and couldn't find support for certain cases other than having multiple duplicate sheets and randomising the IDs within my application.
Just want to understand the usage of busy indicator does it alternate to timeout/putting wait etc.
for example have following line of code in mainfunct()
1. busy.show();
2. callcustom(); --asynch function without callback this is calling xmlhttpRequest etc.
3. busy.hide();
4. callanothercustom(); -- asynch function without callback
now question is
does line 4 will be executed only when busy.hide() completes and
line 3 only when line 2 is completed while without busy all (2,4)
will be called inside mainfunct() without waiting line 2 to
complete...
when busy.hide() is being called is there any timer setup which
holds until line 2 finishes and then hide and call line 4.
A busyIndicator's show and hide functions only control when to display the indicator and when to hide the indicator. They have no effect what-so-ever on anything else going on in your code.
In other words your code is basically:
callcustom()
callanothercustom()
In your customcode you can still make sure that callanothercustom will be called only when it's finished by adding your own callback... I assume this is AJAX inside of it, so: jQuery ajax success callback function definition
function callcustom() {
$.ajax({
url : 'example.com',
type: 'GET',
success : callanothercustom
})
}
And then in callanothercustom you can busy.hide...
Or any other combination of business logic - it really depends on what's going on in your code.
In my opinion, the only main use case for using a busy indicator is a Long running synchronous task that's blocking UI. Let's say greater than 2 seconds long. Hopefully, these are few are far between.
If you have asynch tasks, then the UI is not blocked and user can interact. If you are relying on the results for next steps as you imply above, then you must have a callback/promise to trigger the next steps. If you want the user to be blocked until the async task is complete, then treat it as a synch task and show the Busy.
Be aware, use of Busy Indicator is now seen mostly as an anti-pattern. Its basically yelling at your user "See how slow this app is!". Sometimes you cannot avoid dead time in your app, such as fetching a large block of data to generate a view, but there are many ways to mitigate this. An example may be to -- get something on the view as fast as possible (< 1 sec), and then backfill with larger data. Just always ask yourself WHY you need this Busy, and can I work out a way to avoid it, but not leave the user wondering what the app is doing.
I’m working with silverlight 4.0 WCF Ria services and I want to invoke simultaneously two methods of my DomainService.
In my GUI i have two buttons:
The first one invokes method1 which takes many time to achieve treatment (there is an iteration in treatment and there is a counter variable).
The second one invokes method2 which return a counter value indicating progression of first treatment.
When I invoke method1 then method2
I can obtain the result of method2 only when method1 complete
But I want to follow progression of treatment whenever I clicked on button2
Thank you for your help
Please correct me if I understood the question wrong. When you click button 1, I would call both Method1 and Method2 with same 'on complete event' which will wait for both the method to complete. On the other hand, when button 2 clicked, you need to call only method 2, with different on complete event, with this, method 1 is never called and you will get your progression indicator directly.
I am currently using silverlight to build a control for a CRM form.
There are quite a few asynchronous calls being made where I am retrieving various sets
of data. The problem I am facing however is sometimes the calls are made in a random order
and I need to retrieve the data in a particular order for data binding reasons.
Any idea?
Basically I have 3 Methods -
Retrieve Accounts
Retrieve Opporunities
Retrieve Leads
Each have call backs but I want each to respond with results from callback before moving to the next.
Make the calls synchronous, or
in each call back, invoke the next call
My purpose is as follows: I want to create a deferred calling method. I might need to create a block for this, but I'm not sure. Can you explain me how to create a block for this?
This is the code:
- (IBAction)buyItem:(id)sender {
BOOL purchase = ... /*call purchase method use block*/
}
In this method I make a purchase and after the purchase has completed successfully I want to get result in my variable purchase.
To be more clear:
Step 1: Call the buyItem method.
Step 2: Wait for a response to the purchase (I've omitted the actual methods for the purchase)
Step 3: After the StoreKit object return a response about the purchase, write a value into the variable purchase.
Step 4: After writing the value into purchase, my method buyItem completes execution (go to the } and release).
My question is not about StoreKit specifically (meaning the StoreKit response method - this is an example only). The purchase variable is an example, too. For this variable I will use a data model and it will change after the deferred method executes.
Thanks all!
You do need a block, but maybe a different kind than you're thinking about. You don't need a 'closure', you need a function that will block until it receives the response from the store kit/server.
You can just write a normal buyItem method, but inside you either make a synchronous call to store kit, or if that's not possible, you use threading techniques to achieve what you want. For example, you could wait on a condition variable and then signal it when the store kit call returns.
For both cases, you'll want to perform the 'buyItem' call on a thread other than the UI thread, otherwise your UI will freeze. Given that constraint (if you're even dealing with a UI), I would say this entire approach doesn't make sense. Instead, you'll want to launch the store kit call, set some indicator or spinner or something in your UI, and then when the store kit call returns, unset the spinner or whatever. Make sense?