PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 13, 2022

[FIXED] How to subscribe new value in Akavache?

 September 13, 2022     akavache, cross-platform, xamarin, xamarin.forms     No comments   

Issue

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?


Solution

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.



Answered By - Dhruv
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing