Triger SignalR Core method at specific time - asp.net-core-signalr

I am trying to create a scheduled action for signalR hub method. In ASP.NET I did this
private async System.Threading.Tasks.Task<object> ScheduleAction(Action action, DateTime ExecutionTime)
{
var t = System.Threading.Tasks.Task.Run(
async () =>
{
await System.Threading.Tasks.Task.Delay((int)ExecutionTime.Subtract(DateTime.Now).TotalMilliseconds);
action();
});
return t;
}
}
Probably not the best solution but it worked I would pass Hub method as action as well as DateTime on when to trigger and it worked. Now in ASP.NET Core I get error that says I can not access object that has been disposed and now I have problem.
I need a way to trigger a Hub method at specific time.
Thank you

I got the answer on GitHub. It is not possible to manualy call hub method in asp.net core. More details here: Triger SignalR Core server method at specific time

Related

Caching Data ( Redundant API Calls )

I'm new to Flutter, and I've stumbled upon a problem I need advice in how to approach it. I'm building an app which uses navigator and flutter_bloc. Now when I navigate to a certain route I have the bloc state pull some data from an API and show it. What I want to achieve is not call the API every time when I navigate to that route unless the app has been closed previously or a "pull to refresh" action has been made. How do I got about storing the data after the initial API call. I've tried with the hydrated_bloc but either I don't understand the point of the hydrated_bloc well or it's intention is not to do that. Thank you for the advice in advance.
class AppState {
static final AppState _instance = AppState._internal();
factory AppState() => _instance;
ApiResponseCustomClass apiResponse;
AppState._internal() {
// init things here if needed
}
}
A simple way is to just make a state class where you will keep the result in, and then access it via AppState().apiResponse anywhere in your code.
You can use local_storage to maintain your data locally. So every time you need to fetch data from API again and again. and you can clear data before closing the app.

Delay Google Cloud Function

I have a swift iPhone application paired with Google Firebase. Within the application the user will complete an action (press a button). At this point, I would like to schedule a Google Firebase Function to run 45 minutes after the user action. However, I would like this function to be able to be cancelled when another action (press a button) is completed within the iPhone application. I could do this within swift with a timer but that wouldn't work if the user closes the application.
I am not opposed to using a third party scheduler or something of the sorts. Any suggestion welcome.
I have looked at the possible duplicate questions and answers (Cloud Functions for Firebase trigger on time?) However, most of the links in that answer are deprecated and out of date. Also, that is referring to scheduling something repeatedly. For example, every hour, once a day, etc... I am looking to schedule the job 45 minutes after trigger (user action) with the ability to cancel the job within that 45 minute window.
Would a delayed Cloud Task work for you?
You can create a Cloud Task that is delayed and calls your function through an HTTP trigger. The task can be then cancelled if your user performs the second action you desribe. There is a number of dedicated client libraries for Cloud Tasks, as well as a REST API.
There is no built-in way to delay the trigger on a Cloud Function, or to re-trigger it after a certain delay. The best way I can think of using Cloud Functions is to set up a periodic trigger as shown here: Cloud Functions for Firebase trigger on time?. And then in that periodic trigger you determine what jobs have to run.
You could use https://delayedrequest.com as long as you can invoke your Firebase Function via an http request.
I recently had a similar issue that was solved by using Cloud Scheduler and Firestore. I set up a job that would trigger a cloud function every 5 minutes that would check a Firestore collection. It would retrieve the data on when a cloud function should be triggered and trigger it. It also depends how accurate you want your time triggers to be. It's not optimal to have it trigger all the time for no reason but in my case it worked.
Javascript/Typescript allows you to delay using the setTimeout function
// time is in milliseconds
function delay(time:number) {
return new Promise(res => {
setTimeout(() => {
res("VALUE TO RESOLVE");
}, time);
});
}
Then call this by doing const res = await delay(1000);

How to debug API response using MVVM Android Architecture

I was trying to fetch data from a remote API, following MVVM and Google Architecture Pattern with:
LiveData
Retrofit 2
Dagger 2
Observable
Databinding
My question is:
How can I debug the Api response?
In my last project (using Asynctask, retrofit and GSON) I can stop in the moment when the Api response, so I could see the header of the response if it was a 200 response or 404. Then I could see the json of the Api response, etc...
Using this repo I can't see anything of what I said above, for example, I don't know if the data received was ok or if the model is filled with the JSON response.
I don't gonna copy and paste the code because I'm learning with the repo above and I think that my question is more theoretical.
Anybody has felt like me with this new pattern and architecture?
Anybody can help me solving my problem?
Thanks!
I think Stetho is the best tool for tracking API responses and DB changes.
First you have to init it in your Application class:
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}
Then add it to your OkHttp client:
new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor())
.build();
Finally open Chrome, then type
chrome://inspect/#devices

How to call long running web services in c#?

I have one web service method it is taking long time to run. i want to make it asynchronous
and web service client should not wait for web method to complete. How i could implement this on c#.
//Web Method
[WebMethod]
public void StartProcess()
{
//long running method
}
//Web Client
webService.StartProcess();
Generate your service reference or edit it by choosing async option in VS, now the generation process will create two calls and one will be yourMethodAsync that you can call. You can also setup a callback this way.
Also you can use parallel tasks in C# to put a call on a background thread. I prefer to generate the async methods since it is cleaner.
here is a post that will help

Using Reactive Extensions to create a single event?

I'm trying to get my head around .NET Reactive Extensions, and I wonder if they can be used in the follow scenario:
In my WP7 app, I'm using the SterlingDatabase to persist app settings. As a user is modifying the settings, I want to periodically call Database.Flush()
So in my Property Set method, I'd like to kick off a Database.Flush() timer event, and in say 5 seconds, write to the database. If another property is written to, I want to restart the timer.
I know I can do this with a timer object, calling Start() and Stop(), but I wanted to know if I could do this with Rx, to create an Asycn operation that I can basically start and stop, without using a timer?
Use Throttle:
public void AttachFlushes(IObservable<Unit> writes, SterlingDb db)
{
writes.Throttle(TimeSpan.FromSeconds(5)).Do(_ => db.Flush()).Subscribe();
}