{"id":2241,"date":"2021-12-05T11:54:53","date_gmt":"2021-12-05T11:54:53","guid":{"rendered":"https:\/\/sezeromer.com\/?p=2241"},"modified":"2023-03-04T13:58:45","modified_gmt":"2023-03-04T10:58:45","slug":"swift-ile-realm-nasil-kullanilir","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/swift-ile-realm-nasil-kullanilir\/","title":{"rendered":"How to Use Realm with Swift"},"content":{"rendered":"<p>Hello friends, in this article we will talk about what is Realm and how to use it. When developing mobile applications with iOS, we often need to store some data on the user&#8217;s device. While this is very simple data at times, it can go as far as establishing a database at other times. At such times, you can use Core Data, SQLite or Realm. If you want to get more details about Realm, you can find it <a href=\"https:\/\/github.com\/realm\/realm-cocoa\">here<\/a>.<\/p>\n<h3>Project Plan<\/h3>\n<p>In the sample project we will do, users will be able to record and see the movies they watch. In addition, they can delete or update the movies they have added. The sample working image is as follows.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2246\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/11\/42.gif\" alt=\"\" width=\"397\" height=\"861\" \/><\/p>\n<p>A movie list page and add movies to list page are required. We will also turn it into an update page with small touches on the adding film to the list page. I leave the design parts aside because you can reach the project at the end of the article.<\/p>\n<h3>Model Building<\/h3>\n<p>In order to add a model to the Real database we will create, we need to create a model suitable for the database. The model we create must inherit from the Object class. The Object class is actually a class that comes with the Real pod and creates the Base Model. Then I create whatever properties I want in my Movie class. I define it with Dynamic type because when the data changes in one place, I want it to change quickly on all sides. It is enough for me to create id, movieName and createdDate in my Movie class. In this model, I also create a Create function, with this function, when we call it a movie, a Movie class is returned to you.<\/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;}\">import RealmSwift\r\n\r\nclass Movie: Object {\r\n    @objc dynamic var id: String = UUID().uuidString\r\n    @objc dynamic var movieName: String = \"\"\r\n    @objc dynamic var createdData: Date = Date()\r\n    \r\n    static func create(movieName: String) -&gt; Movie {\r\n        let movie = Movie()\r\n        movie.movieName = movieName\r\n        return movie\r\n    }\r\n}<\/pre>\n<\/div>\n<h3>Listing<\/h3>\n<p>The listing process is actually very simple. The same objects saved in the Realm database are kept in one place and when you want to retrieve these recorded objects, Realm gives you your objects in the Result. After this process, you can use the objects.<\/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;}\">lazy var movies: Results&lt;Movie&gt; = { self.realm.objects(Movie.self) }()<\/pre>\n<\/div>\n<h3>Add and Update<\/h3>\n<p>You can update and add with the Realm.write function as follows. If you are going to update, it is enough to change the name of the object that comes dynamically in Realm.write. If you are going to add, you can do it with the realm.add() function as follows.<\/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;}\">try! realm.write {\r\n                if movie != nil {\r\n                    movie?.movieName = tfMovieName.text ?? \"\"\r\n                    navigationController?.popViewController(animated: true)\r\n                } else {\r\n                    let newMovie = Movie.create(movieName: tfMovieName.text ?? \"\")\r\n                    realm.add(newMovie)\r\n                    navigationController?.popViewController(animated: true)\r\n                }\r\n            }<\/pre>\n<\/div>\n<h3>Delete<\/h3>\n<p>The delete operation is very similar to the insert operation, the only difference is that you use the delete function instead of the create function. You can delete the object you want to delete with the delete function.<\/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;}\">try! realm.write({\r\n            realm.delete(movie)\r\n        })<\/pre>\n<\/div>\n<p>In fact, although it is very simple and easy to use, there are a few advanced parts. Like how to migrate the database we created. In order for this article not to be too long, I will talk about them in another article. You can find that article <a href=\"https:\/\/sezeromer.com\/?p=2248&amp;preview=true\">here<\/a>. You can find the project we created in this article <a href=\"https:\/\/github.com\/omersezer\/swift-projects\/tree\/main\/realm\">here<\/a>.<\/p>\n<p>If you have questions, you can reach us by sending an e-mail or comment. Good work.<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends, in this article we will talk about what is Realm and how to use it. When developing mobile applications with iOS, we often need to store some data on the user&#8217;s device. While this is very simple data at times, it can go as far as establishing a database at other times. At [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1706,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[327,706],"tags":[205,873,496,207,875,874,872,707,871,175,282],"class_list":["post-2241","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","category-swiftui","tag-core","tag-coredata","tag-data","tag-database","tag-db","tag-loca","tag-realm","tag-swiftui","tag-swil","tag-veritabani","tag-xcode"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2241"}],"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=2241"}],"version-history":[{"count":6,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2241\/revisions"}],"predecessor-version":[{"id":2683,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2241\/revisions\/2683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/1706"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=2241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=2241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=2241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}