1

I'm using Akavache's GetAndFetchLatest method and I have created dependency services to communicate with Akavache's method. I'm calling akavache from service layer successfully when i directly reference. For subscribing

  MyMod result = null;
                var cache = BlobCache.LocalMachine;
                var cachedPostsPromise = cache.GetAndFetchLatest(
                    "mykey",
                    () => GetInfo(),
                    offset =>
                    {
                      //some condition
                    });

                    cachedPostsPromise.Subscribe(subscribedPosts => {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                           //do sothing.

                        });
                });

                result = await cachedPostsPromise.FirstOrDefaultAsync();
                return result;

It works.But how an I call subscribe on service layer with interface/dependency service?

Dhruv
  • 90
  • 10
  • What are you trying to achieve exactly? Why not just return the cachedPostsPromise? With the FirstOrDefaultAsync call you're only really going to return the cached value and not the updated value from GetInfo. Is that what you want? – Shane Neuville Dec 09 '18 at 18:25

2 Answers2

0

I think you are new to reactive programming. Understanding the basic principles helps when using Akavache. Maybe this intro helps.

To answer your question, place code like this in your "repository" class:

public override IObservable<MyClass> Get(string key)
{
    var cachedObservable = blobCache.GetAndFetchLatest<MyClass>(key,
        () => GetFromServerAsync(key));
    return cachedObservable ;
}

And in the caller:

private void getNewData()
{
    var myClassObservable = myRepository.Get("the key");
    myClassObservable.Subscribe(handleNewMyClass);
}

private void handleNewMyClass(MyClass newClass)
{
    //handle the new class
}

Note that handleNewMyClass() is called twice:

  1. first with the MyClass from cache
  2. then with the MyClass that was fetched (from the server)

Using this approach you can simply place the repository class in your IoC Container.

Jacco Dieleman
  • 1,316
  • 14
  • 14
0

Please find the the sample code :

var result = BlobCache.LocalMachine;
            var cachedPostsPromise = cache.GetAndFetchLatest(
                "mykey",
                () => ViewModelLocator.GetInstance<IYourServiceName>().MethodName(),
                offset =>
                {
                  //some condition
                });

                cachedPostsPromise.Subscribe(subscribedPosts => {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                       //Your piece of code

                    });
            });

            result = await cachedPostsPromise.FirstOrDefaultAsync();
            return result;

Please note the there any anything inside subscribed will be called twice : first set of data will be cache and second set will be freshly fetched from server.You have to manage according.

Dhruv
  • 90
  • 10