Example #1
0
        /// <summary>
        /// Creates the <see cref="MonoRestRequestAsyncContext"/> for main thread of Unity to invoke callback.
        /// </summary>
        /// <param name="request">The <see cref="IRestRequest"/> to send.</param>
        /// <param name="callback">
        /// The Callback function to be executed upon completion providing access to the asynchronous handle.
        /// </param>
        /// <returns>
        /// The <see cref="MonoRestRequestAsyncContext"/> for main thread of Unity to invoke callback.
        /// </returns>
        private MonoRestRequestAsyncContext CreateContext(IRestRequest request, Action <IRestResponse, RestRequestAsyncHandle> callback)
        {
            MonoRestRequestAsyncContext context = new MonoRestRequestAsyncContext(request, callback);

            contexts.Add(context);
            return(context);
        }
Example #2
0
        /// <summary>
        /// Synchronizes data between threads.
        /// </summary>
        public void Synchronize()
        {
            if (contexts == null)
            {
                return;
            }

            IList <MonoRestRequestAsyncContext> removedContexts = new List <MonoRestRequestAsyncContext>();

            for (int i = 0, length = contexts.Count; i < length; i++)
            {
                MonoRestRequestAsyncContext context = contexts[i];

                if (context.IsRequestCompleted)
                {
                    context.IsRequestCompleted = false;

                    if (context.Callback != null)
                    {
                        context.Callback.Invoke(context.Response, context.AsyncHandle);
                    }
                    else if (context.DownloadDataCallback != null)
                    {
                        context.DownloadDataCallback.Invoke(context.DownloadedData, context.AsyncHandle);
                    }

                    removedContexts.Add(context);
                }
            }

            RemoveCompletedContexts(removedContexts);
        }