Save new VM to cache on demand and without LoadRequest/WebLoadRequest

Dec 6, 2011 at 12:12 PM
Edited Dec 6, 2011 at 12:14 PM

I have a VM that stores new items that the user creates, i wanted to save this VM to cache, and load when application starts, the scenario i m pointing is allowing the user to save new items without internet connection and have AGFX working like a kind of storage, so the user can work with the app ofline, save to cache, then when it get's internet, it uploads to the server. but i m dont ' know how i can force the VM to be saved since im not using a loadrequest. I think CachePolicy.Forever would be a good start no ?

When i reload the app my VM always tries to get a new LoadRequest.. and in my scneario i dont need to go to this step, i just want to deserialize the data from the cache... and force it to save to cache sometimes.. Is that possible with the current version of AgFx?! 

any ideias are welcome

some code:

	
 [CachePolicy(CachePolicy.Forever)]
    public class NewReceiptsRepository : ModelItemBase<RepositoryLoadContext>
    {
       

        public NewReceiptsRepository()
        {
          
        }
        public NewReceiptsRepository(string id)
            : base(new RepositoryLoadContext(id))
        {
        }

        public void SaveItem(Receipt r)
        {

            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    GlobalLoading.Instance.IsLoading = true;
                    this.Items.Add(r);
                });

            SaveImageToIsolatedStorage(r);
       
            
        }

        public void SaveImageToIsolatedStorage(Receipt r)
        {
            if (isoStorageService == null)
                isoStorageService = ServiceLocator.Current.GetInstance<IStorageService>();


            isoStorageService.Write(System.IO.Path.Combine(imgPath, r.FileName), r.Img);
        }


        private BatchObservableCollection<Receipt> _items = new BatchObservableCollection<Receipt>();
        public BatchObservableCollection<Receipt> Items
        {
            get { return _items; }
            set
            {
                if (_items != null)
                {
                    _items.Clear();

                    if (value != null)
                    {
                        foreach (var wp in value)
                        {
                            _items.Add(wp);
                        }
                    }
                }
                RaisePropertyChanged("Items");
            }
        }



        public class ReceiptsRepositoryDataLoader : IDataLoader<RepositoryLoadContext>
        {

            static RelativeTimeConverter con = new Microsoft.Phone.Controls.RelativeTimeConverter();

            public LoadRequest GetLoadRequest(RepositoryLoadContext loadContext, Type objectType)
            {
                return null;

            }

            public object Deserialize(RepositoryLoadContext loadContext, Type objectType, System.IO.Stream stream)
            {


                var vm = new NewReceiptsRepository(loadContext.Url);


                return vm;
            }



        }
    }
}

 

Coordinator
Dec 6, 2011 at 11:38 PM

Hi -

You can save your view model via DataManager.Current.Save(), but your DataLoader must support IDataOptimizer (this tells AgFx how to persist items).  This will let you push things into the cache, and they'll get the same cache policy as the VM normally would.

But that won't push them to the server.  If I understand what you're asking, you're asking for AgFx to sync between the cache and the server, and that's not something it knows how to do.

You'll need to write some other code to queue uploads to the server.  What you could do is have a list of "items to upload", which just stores the IDs of the items you've created, so that the code then walks that list and does a LoadFromCache for each item, then does the work to push their values up to the server.  Then, if you do a refresh, the item in the cache should be replaced with the one that came down from the server.

 

Shawn