{"id":684,"date":"2018-01-18T21:34:24","date_gmt":"2018-01-18T21:34:24","guid":{"rendered":"http:\/\/sezeromer.com\/?p=684"},"modified":"2023-02-26T12:46:28","modified_gmt":"2023-02-26T09:46:28","slug":"xamarin-web-servis-ile-online-veritabani-islemleri-part-3","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/xamarin-web-servis-ile-online-veritabani-islemleri-part-3\/","title":{"rendered":"Xamarin Online Database Operations with Web Service [Part 3]"},"content":{"rendered":"<p>We have come to the last part of the process that we will do with our online database with <strong>Xamarin<\/strong>. So far we have created a Mobile App Quickstart project in <strong>Azure<\/strong>. In this project, we created a database to use <strong>SQL<\/strong>, not <strong>SQLite<\/strong>, and added a table here. In the last part, we are looking at how to send data to our Xamarin project. We come down to the project we created in Azure and download the Xamarin.Forms part of the project. If you want to link the project to the database you have worked on, Azure tells you how to do it step by step.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/01\/web-service-4.png\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>When we download the project and open it, Constants, TodoItem, TodoItemManager, and so on, come out. First I want to start with Constanst. There is an address in this class. We will take our statements from this address and record this adrese. If you remember, the URL we published in our web service in part 2 was the same URL.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public static class Constants\r\n\t{\r\n\t\t\/\/ Replace strings with your Azure Mobile App endpoint.\r\n\t\tpublic static string ApplicationURL = @\"https:\/\/sezeromer.azurewebsites.net\";\r\n\t}<\/pre>\n<\/div>\n<p>When we look at the TodoItem class we see variables like id, name, done. We will try to get the columns in our tables in this class. We need to create a separate class for each table in our database and create separate variables for each of the columns in the tables in these classes. We can get our data with <strong>Json<\/strong>.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class TodoItem\r\n\t{\r\n\t\tstring id;\r\n\t\tstring name;\r\n\t\tbool done;\r\n\r\n\t\t[JsonProperty(PropertyName = \"id\")]\r\n\t\tpublic string Id\r\n\t\t{\r\n\t\t\tget { return id; }\r\n\t\t\tset { id = value;}\r\n\t\t}\r\n\r\n\t\t[JsonProperty(PropertyName = \"text\")]\r\n\t\tpublic string Name\r\n\t\t{\r\n\t\t\tget { return name; }\r\n\t\t\tset { name = value;}\r\n\t\t}\r\n\r\n\t\t[JsonProperty(PropertyName = \"complete\")]\r\n\t\tpublic bool Done\r\n\t\t{\r\n\t\t\tget { return done; }\r\n\t\t\tset { done = value;}\r\n\t\t}\r\n\r\n        [Version]\r\n        public string Version { get; set; }\r\n\t}<\/pre>\n<\/div>\n<p>Finally we look at the TodoItemManager class. There are functions in this class that allow you to receive data and record data. Here you see only the functions of the pull and save functions. You can easily delete and update it yourself.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public partial class TodoItemManager\r\n    {\r\n        static TodoItemManager defaultInstance = new TodoItemManager();\r\n        MobileServiceClient client;\r\n\r\n#if OFFLINE_SYNC_ENABLED\r\n        IMobileServiceSyncTable&lt;TodoItem&gt; todoTable;\r\n#else\r\n        IMobileServiceTable&lt;TodoItem&gt; todoTable;\r\n#endif\r\n\r\n        const string offlineDbPath = @\"localstore.db\";\r\n\r\n        private TodoItemManager()\r\n        {\r\n            this.client = new MobileServiceClient(Constants.ApplicationURL);\r\n\r\n#if OFFLINE_SYNC_ENABLED\r\n            var store = new MobileServiceSQLiteStore(offlineDbPath);\r\n            store.DefineTable&lt;TodoItem&gt;();\r\n\r\n            \/\/Initializes the SyncContext using the default IMobileServiceSyncHandler.\r\n            this.client.SyncContext.InitializeAsync(store);\r\n\r\n            this.todoTable = client.GetSyncTable&lt;TodoItem&gt;();\r\n#else\r\n            this.todoTable = client.GetTable&lt;TodoItem&gt;();\r\n#endif\r\n        }\r\n\r\n        public static TodoItemManager DefaultManager\r\n        {\r\n            get\r\n            {\r\n                return defaultInstance;\r\n            }\r\n            private set\r\n            {\r\n                defaultInstance = value;\r\n            }\r\n        }\r\n\r\n        public MobileServiceClient CurrentClient\r\n        {\r\n            get { return client; }\r\n        }\r\n\r\n        public bool IsOfflineEnabled\r\n        {\r\n            get { return todoTable is Microsoft.WindowsAzure.MobileServices.Sync.IMobileServiceSyncTable&lt;TodoItem&gt;; }\r\n        }\r\n\r\n        public async Task&lt;ObservableCollection&lt;TodoItem&gt;&gt; GetTodoItemsAsync(bool syncItems = false)\r\n        {\r\n            try\r\n            {\r\n#if OFFLINE_SYNC_ENABLED\r\n                if (syncItems)\r\n                {\r\n                    await this.SyncAsync();\r\n                }\r\n#endif\r\n                IEnumerable&lt;TodoItem&gt; items = await todoTable\r\n                    .Where(todoItem =&gt; !todoItem.Done)\r\n                    .ToEnumerableAsync();\r\n\r\n                return new ObservableCollection&lt;TodoItem&gt;(items);\r\n            }\r\n            catch (MobileServiceInvalidOperationException msioe)\r\n            {\r\n                Debug.WriteLine(@\"Invalid sync operation: {0}\", msioe.Message);\r\n            }\r\n            catch (Exception e)\r\n            {\r\n                Debug.WriteLine(@\"Sync error: {0}\", e.Message);\r\n            }\r\n            return null;\r\n        }\r\n\r\n        public async Task SaveTaskAsync(TodoItem item)\r\n        {\r\n            if (item.Id == null)\r\n            {\r\n                await todoTable.InsertAsync(item);\r\n            }\r\n            else\r\n            {\r\n                await todoTable.UpdateAsync(item);\r\n            }\r\n        }<\/pre>\n<\/div>\n<p>After all this, you can view and save your data in your mobile application. If you have a question, please contact me by mail or comment.<\/p>","protected":false},"excerpt":{"rendered":"<p>We have come to the last part of the process that we will do with our online database with Xamarin. So far we have created a Mobile App Quickstart project in Azure. In this project, we created a database to use SQL, not SQLite, and added a table here. In the last part, we are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":631,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[289,288,3],"tags":[42,207,258,91,7,236,208,210,251],"class_list":["post-684","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-ios","category-xamarin-ile-yapilmis-projeler","tag-azure","tag-database","tag-dream-spark","tag-mobile-apps-quickstart","tag-omer-sezer","tag-online-database","tag-sql","tag-sqlite","tag-web-service"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/684"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=684"}],"version-history":[{"count":3,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/684\/revisions"}],"predecessor-version":[{"id":2567,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/684\/revisions\/2567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/631"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}