Skip to content

REQUESTS - Offline Availability

Offline Availability

  • Have you ever found a case where you have a cache, but it just doesn’t care about the user’s current network status?
  • Have you ever wanted to ensure data is available for your app the next time it launches, but the user is disconnected?

When you are building a mobile app, you need to consider offline availability. This is especially true for apps that are used in the field or in areas where network connectivity is spotty.

Offline should not be considered a “cache”. If online connectivity is detected, the offline middleware will keep making calls to your handler. Consider using the (Caching Middleware)[./caching] if you are looking for a performance based cache setup

Request Handler Example

public class MyRequestHandler : IRequestHandler<MyRequest, IReadOnlyList<MyThingData>>
{
[OfflineAvailable]
public async Task<IReadOnlyList<MyThingData>> Handle(MyRequest request, CancellationToken cancellationToken)
{
// each return will overwrite the previous return value
}
}

Controlling HOW the offline data is stored

Often, there are times you want to store data based on the parameter arguments within your request. Trying to build these keys for you can be a bit tricky, so we have a simple interface to help you out instead.

The offline middleware looks at your IRequest implementation. You can also implement the IRequestKey interface which is part of the Shiny.Mediator.Contracts library. This allows you to ‘build’ a key from possible arguments in your request object.

Example

public record MyRequest(string Arg1, int Arg2) : IRequest<IReadOnlyList<MyThingData>>, IRequestKey
{
public string GetKey() => $"{this.Arg1}-{this.Arg2}";
}