New Posts New Posts RSS Feed: foreach loop with async calls, last one to call onsuccess
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

foreach loop with async calls, last one to call onsuccess

 Post Reply Post Reply
Author
siko View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Mar-2012
Posts: 27
Post Options Post Options   Quote siko Quote  Post ReplyReply Direct Link To This Post Topic: foreach loop with async calls, last one to call onsuccess
    Posted: 24-Oct-2012 at 3:13am
Hi,
 
Sorry if the subject is confusing, but here's what I want and I'm a bit lost on how to accomplish that:
 
I have an OperationResult LoadAsync that does the following:
return Coroutine.StartParallel(InitializeCore)
                .AsOperationResult()
                .ContinueWith(
                    or => Coroutine.Start(InitializeApplicationStorage, op => op.OnComplete(onSuccess, onFail))
                              .AsOperationResult()
                              .Execute()
                );
the private IEnumerable<INotifyCompleted> InitializeCore() executes a few 
yield return EntityManager.insertentitytypehere.Where(q => predicatehere).ExecuteAsync();
When the above routine finishes, the InitializeApplicationStorage routine is executed and here lies my problem; I have to do something like this:
	foreach (var questionObject in qoWithImages)
            // ReSharper restore LoopCanBeConvertedToQuery
            {
                var o = questionObject;
 
                yield return EntityManager.InvokeServerMethodAsync(
                    typeName, methodName,
                    callBack =>
                    {
                        //omitted for brevity => writing the received byte[] to isolated storage
                    },
                    null,
                    new object[] { questionObject.image_name, questionObject.test_id }
                    );
            }
I want the main LoadAsync onSuccess to be called when the last callback has finished, with other words, to continue when all the async functions have completed.
 
How can I do that?
 
Thanks again!
 
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 24-Oct-2012 at 11:16am
If I read this correctly, you simply nest the above two coroutines in an outer coroutine like so:

        public OperationResult LoadAsync(Action onSuccess, Action<Exception> onFail)
        {
            return Coroutine.Start(LoadAsyncCore, op => op.OnComplete(onSuccess, onFail))
                .AsOperationResult();
        }

        private IEnumerable<INotifyCompleted> LoadAsyncCore()
        {
            yield return Coroutine.StartParallel(InitializeCore);
            yield return Coroutine.Start(InitializeApplicationStorage);
        }

Back to Top
siko View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Mar-2012
Posts: 27
Post Options Post Options   Quote siko Quote  Post ReplyReply Direct Link To This Post Posted: 24-Oct-2012 at 2:08pm
Oh Marcel!
 
I was almost there. Had the same thought, make LoadAsyncCore, two yield return... Just that I forgot to call it with Coroutine.Start... had still the old StartParallel in place.... :-(
 
Now with this in place => :-)))
 
Thank you so much, again!

Edited by siko - 24-Oct-2012 at 2:09pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down