<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Toán Nguyễn on Medium]]></title>
        <description><![CDATA[Stories by Toán Nguyễn on Medium]]></description>
        <link>https://medium.com/@thetoan0425?source=rss-7d03ebce122a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*tKkDJPiIg8bf9bk1.</url>
            <title>Stories by Toán Nguyễn on Medium</title>
            <link>https://medium.com/@thetoan0425?source=rss-7d03ebce122a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 14:55:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thetoan0425/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction database with Realm]]></title>
            <link>https://medium.com/@thetoan0425/introduction-database-with-realm-760272cca84c?source=rss-7d03ebce122a------2</link>
            <guid isPermaLink="false">https://medium.com/p/760272cca84c</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[realm]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Toán Nguyễn]]></dc:creator>
            <pubDate>Fri, 23 Nov 2018 04:27:03 GMT</pubDate>
            <atom:updated>2018-11-23T04:34:30.410Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*gPEjqJjKqyBQj0zgReGAEQ.jpeg" /></figure><p>Realm is good database, it’s very easy to use and fast. Realm object database is a simple alternative to SQLite and Core Data and proudly open source.</p><p>In my projects , I integrate it as main database quickly. I write a database manager using Realm and Generic. If you have many models, you just define them.</p><h3>1. Installation</h3><p>You can install it following link: <a href="https://github.com/realm/realm-cocoa">https://github.com/realm/realm-cocoa</a></p><h3>2. Create Model</h3><blockquote>import RealmSwift</blockquote><blockquote>class Student: Object {</blockquote><blockquote>@objc dynamic var studentId: Int</blockquote><blockquote>@objc dynamic var name: String</blockquote><blockquote>@objc dynamic var age: Int</blockquote><blockquote>@objc dynamic var address: String</blockquote><blockquote>override static func primaryKey() -&gt; Int {</blockquote><blockquote>return “studentId”</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><p>Very easy to create a Realm object, you need import RealmSwift and inheritance Object. You should create a primary key, this key will help you update object.</p><h3>3. Create Database manager</h3><blockquote>class DBManager {</blockquote><blockquote>private var database: Realm</blockquote><blockquote>static let shared = DBManager()</blockquote><blockquote>private override init() {</blockquote><blockquote>database = try! Realm()</blockquote><blockquote>}</blockquote><blockquote>func getDataFromDB&lt;T: Object&gt;(type: T.Type) -&gt; Results&lt;T&gt; {</blockquote><blockquote>let results: Results&lt;T&gt; = database.objects(type)</blockquote><blockquote>return results</blockquote><blockquote>}</blockquote><blockquote>func addData&lt;T: Object&gt;(object: T) {</blockquote><blockquote>try! database.write {</blockquote><blockquote>database.add(object, update: true)</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>func addData&lt;T: Object&gt;(objects: [T]) {</blockquote><blockquote>try! database.write {</blockquote><blockquote>database.add(objects, update: true)</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>func deleteAllFromDatabase() {</blockquote><blockquote>try! database.write {</blockquote><blockquote>database.deleteAll()</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>func deleteFromDb&lt;T: Object&gt;(object: T) {</blockquote><blockquote>try! database.write {</blockquote><blockquote>database.delete(object)</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>func getDataWith&lt;T: Object&gt;(type: T.Type, condition: String) -&gt;Results&lt;T&gt; {</blockquote><blockquote>return database.objects(type).filter(condition)</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><p>It’s my DatabaseManager class, I define it is a singleton. This class support add (update), delete object and remove all data from database.</p><h3>4. Using</h3><ul><li>Create object and add to Realm data</li></ul><p>Create a new student:</p><blockquote>let newStudent = Student()</blockquote><blockquote>newStudent.id = 1</blockquote><blockquote>newStudent.name = “Taylor Swift”</blockquote><blockquote>newStudent.age = 28</blockquote><blockquote>newStudent.address = “United State”</blockquote><p>Add this student to database:</p><blockquote>DBManager.shared.addData(object: newStudent)</blockquote><p>or</p><blockquote>DBManager.shared.addData(objects: [newStudent])</blockquote><ul><li>Get all data</li></ul><blockquote>let students = DBManager.shared.getDataFromDB(type: Student.self)</blockquote><ul><li>Get data with condition</li></ul><p>let student = DBManager.shared.getDataWith(type: Student.self, condition: “studentId == 1”)</p><ul><li>Remove object</li></ul><blockquote>DBManager.shared.deleteFromDb(object: newStudent)</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=760272cca84c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>