{"id":791,"date":"2017-12-28T23:44:33","date_gmt":"2017-12-28T20:44:33","guid":{"rendered":"http:\/\/enisnecipoglu.com\/?p=791"},"modified":"2018-03-08T18:05:37","modified_gmt":"2018-03-08T15:05:37","slug":"xamarin-forms-saving-data-without-sql","status":"publish","type":"post","link":"https:\/\/enisnecipoglu.com\/en\/xamarin-forms-saving-data-without-sql\/","title":{"rendered":"Xamarin Forms Saving Data without SQL"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-796\" src=\"http:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/XamarinFormsSavableObject.jpg\" alt=\"Xamarin Forms Saving Data wirhout SQL any SQL SQLite SavableObject Plugin Xamarin, Xamarin Froms save data\" width=\"750\" height=\"500\" srcset=\"https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/XamarinFormsSavableObject.jpg 750w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/XamarinFormsSavableObject-300x200.jpg 300w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/XamarinFormsSavableObject-600x400.jpg 600w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><br \/>\nHi Everyone,<\/p>\n<h1>Xamarin Forms Android SQLite Problem<\/h1>\n<p>As most of you knows SQLite has some problems in Android 7 Nougat. And I&#8217;ve presented you a different method to save your data to local storage in device.<\/p>\n<h1>The Logic of my Plugin<\/h1>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-792\" src=\"http:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/savableobject.gif\" alt=\"enis necipoglu xamarin forms savableobject data save no sql\" width=\"368\" height=\"656\" \/><br \/>\nFirsly,<\/p>\n<p>We should know\u00a0<strong>Application.Current.Properties<\/strong> is a dictionary in Xamarin Forms which is able to save to device. You can add a value with using,\u00a0<strong>Application.Current.Properties.Add(&#8220;MyData&#8221;,&#8221;Hello, My name is Enis!&#8221;);<\/strong> and when you use\u00a0<strong>Application.Current.SavePropertiesAsync()<\/strong> that dictionary saves all values with keys. And you can call them later like\u00a0<strong>Application.Current.Properties[&#8220;MyData&#8221;]\u00a0<\/strong>. UUgh. It&#8217;s easy way to save little datas. But if you have too much data or some Collecton to save. It&#8217;s not usable! Don&#8217;t worry, it&#8217;s possible but it&#8217;s too complicated. That&#8217;s why I&#8217;ve written some class to manage Application Properties. Your models will be saved in Xamarin Forms Layer with its properties via using Save() method.That means it works for every platform. And than it&#8217;ll be restored when you call Load() method (Mostly in constructor.)<\/p>\n<p>&nbsp;<\/p>\n<h1>Let&#8217;s make an sample<\/h1>\n<p>Firstly you should download the plugin which named as\u00a0<strong><a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms.SavableObject\/\">Xamarin.Forms.SavableObject<\/a>\u00a0<\/strong>and you can find my plugin&#8217;s source codes from this <a href=\"https:\/\/github.com\/enisn\/Plugin.LocalData\">Git.Hub link<\/a>. Plugin should be installed only Portable Layer. It&#8217;s enough to work. Most important dependency is <strong>Newtonsoft.JSON <\/strong> v.9.0.1, if you don&#8217;t have, please install that package first.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-793\" src=\"http:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/savableObject-nuget.png\" alt=\"savableObject-nuget enis necipoglu visual studio nuget Xamarin - Xamarin Forms \" width=\"1016\" height=\"595\" srcset=\"https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/savableObject-nuget.png 1016w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/savableObject-nuget-300x176.png 300w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/savableObject-nuget-768x450.png 768w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/savableObject-nuget-600x351.png 600w\" sizes=\"(max-width: 1016px) 100vw, 1016px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Before start coding. I&#8217;ve not supported any pattern except <strong>MVVM.<\/strong> It&#8217;ll works any pattern successfully, but it&#8217;s not easy to use with as MVVM.<\/p>\n<p>We&#8217;ll save 2 string, 1 double and 1 bool parameter on locally. Design is not important in that sample \ud83d\ude42 Place 2 Entry, 1 Stepper, 1 Label and 1 switch in your ContentPage (I&#8217;ve used MainPage for that which comes default on a new project).<\/p>\n<p>Our MainPage.XAML will be like that:<\/p>\n<div class=\"codecolorer-container html4strict default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"html4strict codecolorer\"><span class=\"sc-1\">&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?--&gt;<\/span><br \/>\n<br \/>\n<span class=\"sc2\">&lt;<span class=\"kw2\">label<\/span>&gt;&lt;<span class=\"sy0\">\/<\/span><span class=\"kw2\">label<\/span>&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<span class=\"kw2\">button<\/span>&gt;&lt;<span class=\"sy0\">\/<\/span><span class=\"kw2\">button<\/span>&gt;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-795\" src=\"http:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/26194133_1780406791970426_878754029_o.png\" alt=\"Xamarin Forms Savable Object allows Save data withouw sql in Xamarin Forms\" width=\"1080\" height=\"1920\" srcset=\"https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/26194133_1780406791970426_878754029_o.png 1080w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/26194133_1780406791970426_878754029_o-169x300.png 169w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/26194133_1780406791970426_878754029_o-768x1365.png 768w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/26194133_1780406791970426_878754029_o-576x1024.png 576w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2017\/12\/26194133_1780406791970426_878754029_o-600x1067.png 600w\" sizes=\"(max-width: 1080px) 100vw, 1080px\" \/><br \/>\nAnd we need a ViewModel. Just Create a cs file to root of project and name it\u00a0<strong>MainPageViewModel.cs.\u00a0<\/strong>After created and Implementing some MVVM things, ViewModel will be like that:<\/p>\n<div class=\"codecolorer-container csharp default\" style=\"overflow:auto;white-space:nowrap;width:100%;height:300px;\"><div class=\"csharp codecolorer\"><span class=\"kw1\">using<\/span> <span class=\"co3\">System.ComponentModel<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">using<\/span> <span class=\"co3\">System.Runtime.CompilerServices<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">using<\/span> <span class=\"co3\">Xamarin.Forms<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">namespace<\/span> TestAppSavableObject<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw4\">class<\/span> MainPageViewModel <span class=\"sy0\">:<\/span> SavableObject, INotifyPropertyChanged<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw1\">public<\/span> MainPageViewModel<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">string<\/span> name <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;defaultName&quot;<\/span>, surname <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;defaultSurname&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">double<\/span> age<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">bool<\/span> isToggled<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw4\">string<\/span> Name<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw1\">get<\/span> <span class=\"sy0\">=&amp;<\/span>gt<span class=\"sy0\">;<\/span> name<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">set<\/span> <span class=\"br0\">&#123;<\/span> name <span class=\"sy0\">=<\/span> <span class=\"kw1\">value<\/span><span class=\"sy0\">;<\/span> OnPropertyChanged<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw4\">string<\/span> Surname<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw1\">get<\/span> <span class=\"sy0\">=&amp;<\/span>gt<span class=\"sy0\">;<\/span> surname<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">set<\/span> <span class=\"br0\">&#123;<\/span> surname <span class=\"sy0\">=<\/span> <span class=\"kw1\">value<\/span><span class=\"sy0\">;<\/span> OnPropertyChanged<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw4\">double<\/span> Age<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw1\">get<\/span> <span class=\"sy0\">=&amp;<\/span>gt<span class=\"sy0\">;<\/span> age<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">set<\/span> <span class=\"br0\">&#123;<\/span> age <span class=\"sy0\">=<\/span> <span class=\"kw1\">value<\/span><span class=\"sy0\">;<\/span> OnPropertyChanged<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw4\">bool<\/span> IsToggled<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw1\">get<\/span> <span class=\"sy0\">=&amp;<\/span>gt<span class=\"sy0\">;<\/span> isToggled<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">set<\/span> <span class=\"br0\">&#123;<\/span> isToggled <span class=\"sy0\">=<\/span> <span class=\"kw1\">value<\/span><span class=\"sy0\">;<\/span> OnPropertyChanged<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#91;<\/span>IgnoreSave<span class=\"br0\">&#93;<\/span><br \/>\n<span class=\"kw1\">public<\/span> Command SaveCommand <span class=\"br0\">&#123;<\/span> <span class=\"kw1\">get<\/span><span class=\"sy0\">;<\/span> <span class=\"kw1\">set<\/span><span class=\"sy0\">;<\/span> <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#region INotifyPropertyChanged Implementation<\/span><br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw1\">event<\/span> PropertyChangedEventHandler PropertyChanged<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">void<\/span> OnPropertyChanged<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#91;<\/span>CallerMemberName<span class=\"br0\">&#93;<\/span> <span class=\"kw4\">string<\/span> propertyName <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\nPropertyChanged<span class=\"sy0\">?.<\/span><span class=\"me1\">Invoke<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">this<\/span>, <span class=\"kw3\">new<\/span> PropertyChangedEventArgs<span class=\"br0\">&#40;<\/span>propertyName<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"co2\">#endregion<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Everything is normal until here! Just Inherit from\u00a0<strong>SavableObject<\/strong> and see the Magic! Add &#8220;<strong>using Plugin.LocalData.Abstractions;<\/strong>&#8221; and add\u00a0<strong>Load()\u00a0<\/strong>method in constructor. Then set SaveCommands value:<\/p>\n<div class=\"codecolorer-container csharp default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"csharp codecolorer\">Load<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\nSaveCommand <span class=\"sy0\">=<\/span> <span class=\"kw3\">new<\/span> Command<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">=&amp;<\/span>gt<span class=\"sy0\">;<\/span>Save<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>Everything is ready. When this class Instaced ones, datas will be read and restored to your properties. When you use <strong>Save()\u00a0<\/strong>method, all properties in class will be saved. If you want to not save some properties like Command in my situation, just add\u00a0<strong>[IgnoreSave]\u00a0<\/strong>attribute to that property. Everything is OK!.<\/p>\n<p>&nbsp;<\/p>\n<p>If you want to work with\u00a0<strong>Collections,\u00a0<\/strong> you can see the sample project from here:<\/p>\n<p><a href=\"https:\/\/github.com\/enisn\/TestAppSavableObject\">https:\/\/github.com\/enisn\/TestAppSavableObject<\/a><\/p>\n<h1>Supported Situations:<\/h1>\n<ul>\n<li>All value type properties can be saved,<\/li>\n<li>All the strings (Length is not important) can be saved,<\/li>\n<li>All Collections, which Inherits from\u00a0<strong>IEnumarable<\/strong> can be saved<\/li>\n<\/ul>\n<blockquote>\n<h2>WARNING!<\/h2>\n<p>If one of your property is a <strong>Nullable<\/strong>,\u00a0<strong>Application Properties\u00a0<\/strong>will be have a bug and you&#8217;ll not be able to save or load any data! If you have Nullable&lt;&gt; something or something with ? that class proeprties will not be saved or loaded!<\/p>\n<p>Object support will be added in later versions.<\/p><\/blockquote>\n<p><\/p>","protected":false},"excerpt":{"rendered":"Hi Everyone, Xamarin Forms Android SQLite Problem As most of you knows SQLite has some problems in Android 7 Nougat. And I&#8217;ve presented you a different method to save your data to local storage in device. The Logic of my Plugin Firsly, We should know\u00a0Application.Current.Properties is a dictionary in Xamarin Forms which is able to save to device. You can add a value with using,\u00a0Application.Current.Properties.Add(&#8220;MyData&#8221;,&#8221;Hello, My name is Enis!&#8221;); and when you use\u00a0Application.Current.SavePropertiesAsync() that dictionary saves all values with keys. And you can call them later like\u00a0Application.Current.Properties\u00a0. UUgh. It&#8217;s easy way to save little datas. But if you have too much data or some Collecton to save. It&#8217;s not usable! Don&#8217;t worry, it&#8217;s possible but it&#8217;s too complicated. That&#8217;s why I&#8217;ve written some class to manage Application Properties. Your models will be saved in Xamarin Forms Layer with its properties via using Save() method.That means it works for every platform. And than it&#8217;ll be restored when you call Load() method (Mostly in constructor.) &nbsp; Let&#8217;s make an sample Firstly you should download the plugin which named as\u00a0Xamarin.Forms.SavableObject\u00a0and you can find my plugin&#8217;s source codes from this Git.Hub link. Plugin should be installed only Portable Layer. It&#8217;s enough to work. Most important dependency is Newtonsoft.JSON v.9.0.1, if you don&#8217;t have, please install that package first. &nbsp; Before start coding. I&#8217;ve not supported any pattern except MVVM. It&#8217;ll works any pattern successfully, but it&#8217;s not easy to use with as MVVM. We&#8217;ll save 2 string, 1 double and 1 bool parameter on locally. Design is not important in that sample \ud83d\ude42 Place 2 Entry, 1 Stepper, 1 Label and 1 switch in your ContentPage (I&#8217;ve used MainPage for that which comes default on a new project). Our MainPage.XAML will be like that: &lt;!&#8211;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&#8211;&gt; &lt;label&gt;&lt;\/label&gt; &lt;button&gt;&lt;\/button&gt; And we need a ViewModel. Just Create a cs file to root of project and name it\u00a0MainPageViewModel.cs.\u00a0After created and Implementing some MVVM things, ViewModel will be like that: using System.ComponentModel; using System.Runtime.CompilerServices; using Xamarin.Forms; namespace TestAppSavableObject &#123; public class MainPageViewModel : SavableObject, INotifyPropertyChanged &#123; public MainPageViewModel&#40;&#41; &#123; &#125; string name = &quot;defaultName&quot;, surname = &quot;defaultSurname&quot;; double age; bool isToggled; public string Name &#123; get =&amp;gt; name; set &#123; name = value; OnPropertyChanged&#40;&#41;; &#125; &#125; public string Surname &#123; get =&amp;gt; surname; set &#123; surname = value; OnPropertyChanged&#40;&#41;; &#125; &#125; public double Age &#123; get =&amp;gt; age; set &#123; age = value; OnPropertyChanged&#40;&#41;; &#125; &#125; ","protected":false},"author":1,"featured_media":796,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,15,6],"tags":[32,33,31,30,28,29],"_links":{"self":[{"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/posts\/791"}],"collection":[{"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/comments?post=791"}],"version-history":[{"count":18,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/posts\/791\/revisions"}],"predecessor-version":[{"id":962,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/posts\/791\/revisions\/962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/media\/796"}],"wp:attachment":[{"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/media?parent=791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/categories?post=791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/tags?post=791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}