<?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 Nawin P. on Medium]]></title>
        <description><![CDATA[Stories by Nawin P. on Medium]]></description>
        <link>https://medium.com/@nawinest?source=rss-2bf5896669ca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3296-ndvSTzOMBeIOa4zvg.jpeg</url>
            <title>Stories by Nawin P. on Medium</title>
            <link>https://medium.com/@nawinest?source=rss-2bf5896669ca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:37:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nawinest/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[Create Custom TabBar with Lotties animation in SwiftUI]]></title>
            <link>https://medium.com/@nawinest/create-custom-tabbar-with-lotties-animation-in-swiftui-ed76325e1dda?source=rss-2bf5896669ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed76325e1dda</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[tab-bar]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Nawin P.]]></dc:creator>
            <pubDate>Mon, 08 Aug 2022 07:32:34 GMT</pubDate>
            <atom:updated>2022-08-08T16:17:45.552Z</atom:updated>
            <content:encoded><![CDATA[<h4>Use Lotties animation for animate TabBar’s tab when tap</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jJLYk5PlXBphaGTJIuyuHw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*_rIrIcHQg46UKNJ4UMjBUQ.gif" /><figcaption>Final result.</figcaption></figure><p>If we need to add something which make application look more cool to our application, The first thing that come up to our mind is Animation. But! Animation is most terrible dream for developer. And I don’t know why Designer always put it to application T_T.</p><p><strong>Time for hero! Lotties.</strong></p><p>What’s Lotties ?. Lotties is a tools for display animation with JSON file. LottieFiles provides all the tools that you need to create, edit, test and display Lottie animations. Lotties also provides free animated icon you can explore it in their website. <a href="https://lottiefiles.com/">HERE</a></p><h3><strong>Let’s jump in.</strong></h3><p><strong>Section 1 : </strong>First step add dependency first.</p><ol><li>go to Target&gt; Package Dependencies&gt; Add package dependency</li><li>pasted library Github’s source : <a href="https://github.com/airbnb/lottie-ios">https://github.com/airbnb/lottie-ios</a></li><li>Pressed next for add it into our project.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bjJSrjInNxBvcUbfMevXDw.png" /><figcaption>Add Lotties package dependencies</figcaption></figure><p><strong>Section 2: </strong>Next, We will create custom TabView main page which has custom TabBar at bottom.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0d1b10ed1603ac6900ef218e372c726/href">https://medium.com/media/e0d1b10ed1603ac6900ef218e372c726/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YHTfbVldDM5m4OQILndfiA.png" /><figcaption>Result of First custom TabBar.</figcaption></figure><p><strong>Section 3:</strong> We will add Lotties to our TabBar.</p><p>Lotties already had a View for animation name “AnimationView” but Unfortunately, It supports only UIKit. We have to implement <a href="https://developer.apple.com/documentation/swiftui/uiviewrepresentable">UIViewRepresentable</a> for use it in SwiftUI. Like this..</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ce64a55fc15da8959c910798fcb7454/href">https://medium.com/media/6ce64a55fc15da8959c910798fcb7454/href</a></iframe><p>For icon file, I will download from LottiesFiles’s website and paste it in my project. If you want to use same icon with me. You can follow links at below urls.</p><p>home icon — <a href="https://lottiefiles.com/93492-home-icon">https://lottiefiles.com/93492-home-icon</a></p><p>chat icon — <a href="https://lottiefiles.com/81963-chat-bubbles">https://lottiefiles.com/81963-chat-bubbles</a></p><p>profile — <a href="https://lottiefiles.com/28497-profile-icon">https://lottiefiles.com/28497-profile-icon</a></p><p>setting — <a href="https://lottiefiles.com/53408-setting">https://lottiefiles.com/53408-setting</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*x5slbzS2th03QUyMNmpgaw.png" /></figure><p><strong>Section 4:</strong> Next, We will add LottieView to our TabBar. By change our HomeView and TabModel code a little bit like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22b9c5cd351a7c23a5b4c795d0fe86f9/href">https://medium.com/media/22b9c5cd351a7c23a5b4c795d0fe86f9/href</a></iframe><p>Finished !!! Result..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*_rIrIcHQg46UKNJ4UMjBUQ.gif" /><figcaption>Result</figcaption></figure><p>Thanks for reading, Have you enjoy coding ~</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed76325e1dda" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI + MVVM : @ObservedObject หรือ @StateObject]]></title>
            <link>https://medium.com/@nawinest/swiftui-mvvm-observedobject-%E0%B8%AB%E0%B8%A3%E0%B8%B7%E0%B8%AD-stateobject-d88912679cae?source=rss-2bf5896669ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/d88912679cae</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[mvvm]]></category>
            <dc:creator><![CDATA[Nawin P.]]></dc:creator>
            <pubDate>Wed, 03 Aug 2022 05:35:33 GMT</pubDate>
            <atom:updated>2022-08-03T05:35:33.640Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI + MVVM : @ObservedObject หรือ @StateObject</h3><h4>การจัดการ State นอก View ด้วย Observable object</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AsjKW50u0xBicDXRWSzRWw.png" /><figcaption>SwiftUI + MVVM : @ObservedObject หรือ @StateObject</figcaption></figure><h3>Introduction</h3><p>พอพูดถึง <strong>SwiftUI</strong> สิ่งหนึ่งที่เป็นข้อดีของมันมาก ๆ หรือออกมาเตะตัดขา UIKit เลยก็คือ State นั่นเอง การจัดการ State ใน View นั้นทำได้ง่ายมา แค่ ประกาศ annotation @State ไว้หน้าตัวแปรที่เราต้องการแล้วก็ปรับเปลี่ยนไปมาใน View นั้นๆ ได้ตามใจชอบ แต่ถ้าหากจะพูดถึงในการพัฒนาจริง ๆ นั้น เราคงไม่ยัด State ไว้ใน View สะหมดหรอก เราก็คงแยกออกมาจัดการด้านนอก เพื่อลดความเละเทะ และ Maintainable ในอนาคต</p><h3>Recap : Model View View Model Pattern</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*2powkdq-rdebQyrXbPSCww.png" /><figcaption>Image 1 : MVVM Architecture</figcaption></figure><p>องค์ประกอบหลักของ MVVM จะมี 3 ส่วนหลัก ๆ ก็คือ</p><ol><li>View : ส่วนแสดงผลใน SwiftUI เราจะมองเป็น View Class เลย</li><li>Model : ส่วนของ Data Model</li><li>ViewModel : ส่วนของการจัดการข้อมูลและ Business Logic ต่าง ๆ</li></ol><p>ส่วน Topic ของวันนี้ก็คือส่วนของการ Data bindings ข้อมูลระหว่าง ViewModel กับ View นั้นเอง ก่อนอื่นเลย เรามาสร้างไฟล์ View กับ ViewModel เปล่า ๆ หน้าตาประมาณนี้ขึ้นมาก่อนครับ</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ac310aebbb6d37034e735d1a8155191/href">https://medium.com/media/7ac310aebbb6d37034e735d1a8155191/href</a></iframe><p>ถ้าเราเอาโค้ดนี้ไปรันเลย จะพบว่ามันไม่แสดงอะไรเลยนะครับ เพราะยังไม่ได้ Binding เลย // คนรีบกำหมัดแล้ว 55555555</p><h3>@ObservedObject</h3><p>คือ property wrapper ตัวหนึ่งที่ทำหน้าที่บอกกับ View ว่าตัวแปรที่ถูก Wrapper จะทำการ Observe value โดยจะรองรับการ observe object ที่เป็น reference type โดยวิธีการใช้ก็มีดังนี้</p><ol><li>ใส่ @ObservedObject หน้าตัวแปรที่เราต้องการ Observe ซึ่งในตัวอย่างก็คือ viewModel นั้นเอง</li><li><strong>สำคัญ </strong>Object ViewModel ที่เราจะทำการ observe นั้นต้อง Inherit เป็น Subclass ของ ObservableObject ด้วย</li></ol><p>ก็จะได้หน้าประมาณนี้</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e85b3fbc39c5118958ae2fce9e52bec/href">https://medium.com/media/5e85b3fbc39c5118958ae2fce9e52bec/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*Mr1W3vaynT86EjFWfywlFg.png" /><figcaption>Image 2 : MainDetailView Full code</figcaption></figure><h3>@StateObject</h3><p>คือ property wrapper ตัวหนึ่งที่ออกมาพร้อมกับ iOS 14 ทำหน้าที่ทุกอย่างเหมือนกันกับ @ObservedObject เลยครับ โอเค จบ…5555 อ่านไม่ผิดครับ มันคือหน้าที่เดียวกันเลย เพียงแต่ StateObject จะมีคุณสมบัติพิเศษเพิ่มมาคือมันจะทำตัวเป็นเหมือน State ของ View ด้วย</p><p>โดยปกติเเล้ว Swift UI จะมีการ Redraw ทุกครั้งที่สิ่งที่มีการ Observe เปลี่ยน อาจจะยังไม่มองไม่เห็นภาพ เราลองสร้าง Custom View เล็ก ๆ เพิ่มมาอีกสักหนึ่งตัวครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r4SWfPVlI18n9i7n01vP2w.png" /><figcaption>Image 3 : Custom view and additional code</figcaption></figure><p><strong>สิ่งที่เพิ่มเข้ามา</strong></p><ol><li>สร้าง Custom view “SubDetailView” ขึ้นโดยมี @ObservedObject viewModel ของตัวเองด้วยโดยเก็บ Counter</li><li>เพิ่มหน่วงเวลา 3 วินาทีก่อน Set countries ใน MainDetailViewModel</li></ol><p>จากโค้ดก็ดูเหมือนจะไม่มีอะไรผิดปกติ จนกระทั่งรันแอปขึ้นมาเล่นก็เกิดเหตุการณ์ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7_dOH-rg1O3K6JRyw3a1JA.png" /><figcaption>Image 4 : Problem state was reset !!</figcaption></figure><p>อะ อ้าว…. เจออย่างนี้วิธีการแก้ก็มีอยู่ด้วยกันหลายวิธีครับ อาทิเช่น ใช้ ViewModel ตัวเดียวกัน หรือ Pass state เป็น Binding ลงมาแทน หรือ ใช้ @StateObject</p><p>วิธีโดยใช้ @StateObject ก็ง่ายแสนง่าย ก็คือ ใน SubDetailView.swift เราเปลี่ยนจาก @ObservedObject เป็น @StateObject ให้ ViewModel โล้ด</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bea386b6b284439b258cae063bdad9f2/href">https://medium.com/media/bea386b6b284439b258cae063bdad9f2/href</a></iframe><p>เท่านี้ Count ก็จะไม่ถูก reset หลังจาก Set countries ใน MainDetailViewModel อีกต่อไป</p><h3>Conclusion</h3><p>@StateObject and @ObservedObject ในเรื่องของลักษณะเฉพาะของมันแทบจะคล้ายกันทุกอย่าง ต่างการที่ StateObject จะทำหน้าที่คล้าย State ถ้าอ่านบทความต่างประเทศมันถูกเรียกว่า Source of truth เลย @ObservedObject ตัว Object ที่ถูก wrap ด้วยมันจะยอมสร้างใหม่ทุกครั้งที่มี View create เลยทำให้ค่าถูก reset ไปด้วยนั้นเอง</p><p>วิธีการใช้งานอาจจะแตกต่างกันไปตาม UseCase บางครั้ง ผมเลยสรุปคร่าว ๆ เรื่องวิธีการใช้อยู่ 2 แบบ</p><ol><li>หาก View นั้นเป็น Parent View ที่ต้องการเก็บข้อมูลเป็น reference object ในรูปแบบของ State และไม่ต้องการให้ถูก re-render จากสิ่งใดก็ตามที่เปลี่ยนแปล งให้เลือกใช้ @StateObject</li><li>หาก View นั้นเป็น Child / Component View ต้องการ Binding ข้อมูลที่เป็น object ให้เลือกใช้ @ObservedObject</li></ol><p>*** ตัวอย่างที่ถูกสร้างขึ้นเป็นการ mock usecase ที่เพียงอาจจะเกิดขึ้นได้<br>*** บทความนี้เคย Publish ที่ Account <a href="https://medium.com/u/bcde57385718">Nawin P</a> มาแล้ว ตอนนี้ย้าย Account ครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d88912679cae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GCD : Swift Multi-Threading คืออะไร ?]]></title>
            <link>https://medium.com/@nawinest/gcd-swift-multi-threading-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3-fafc698a6c5c?source=rss-2bf5896669ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/fafc698a6c5c</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[dispatches]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Nawin P.]]></dc:creator>
            <pubDate>Wed, 04 Dec 2019 09:14:54 GMT</pubDate>
            <atom:updated>2019-12-04T09:14:54.323Z</atom:updated>
            <content:encoded><![CDATA[<h3>GCD : Swift Multi-Threading คืออะไร ?</h3><p>Apple documentation ได้กล่าวไว้ :<br>“ <strong>GCD : Grand Central Dispatch</strong> provides and manages FIFO queues to which your application can submit tasks in the form of block objects. Work submitted to dispatch queues are executed on a pool of threads fully managed by the system. No guarantee is made as to the thread on which a task executes. ”</p><p>เข้าใจง่าย ๆ คือ ตัว GCD จะเป็น API ที่ช่วยให้เราทำเรื่อง Concurrency operations ต่าง ๆ ได้ง่ายและสะดวกมากขึ้นนั่นเองครับ โดยช่วยเรื่องการจัด Queue ต่าง ๆ ผ่านตัว DispatchQueue</p><h3>DispatchQueue</h3><p>เป็น Object class ที่จัดการเกี่ยวกับการ execute ตัว operaion ต่าง ๆ ในรูปแบบลำดับ ( serially ) หรือการทำงานหลาย ๆ งานในเวลาเดียวกัน ( concurrently ) บน Background thread หรือ main thread. จะทำการจัดการ Task ที่เกิดขึ้นในรูปแบบของ FIFO (First-in, First-out)</p><h3>Asynchronous / Synchronous</h3><p>ถ้าเข้าใจ Concept ของเจ้าสองตัวนี้อยู่แล้วข้ามส่วนนี้ไปเลยครับ 555 ส่วนคนยังไม่เคยได้ลองเขียนโค้ดแบบ Asynchronous / Synchronous มาก่อนเลย แนะนำให้อ่านก่อนครับ 555</p><h4>Synchronous concept</h4><p>เป็นการทำงานแบบลำดับโดยการทำงานถัดไป จะทำหลังจากงานก่อนหน้าเสร็จแล้วเท่านั้น</p><blockquote>Let’s go to see example ~</blockquote><p>จากตัวอย่างนี้เราจะเห็นการทำงานเป็นลำดับโดยการทำงานที่เกิดจาก for loop ตัวที่ 2 จะทำงานหลังจากตัวแรกทำงานเสร็จเรียบร้อยแล้ว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kr7U5xt2mX93ohzJlQMzWw.png" /><figcaption>ตัวอย่างการทำงาน รูปแบบ synchronous</figcaption></figure><h4>Asynchronous concept</h4><p>เป็นการทำงานแบบตรงกันข้ามของ Synchronous นั่นเเหละ คือมันจะไม่รอการทำงานส่วนอื่น ๆ ไม่จำเป็นรอการทำงานก่อนหน้า</p><blockquote>Let’s go to see example ~</blockquote><p>จากตัวอย่างนี้เราจะเห็นการทำงานของ 2 for loop แบบพร้อม ๆ กันโดยไม่รอกัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XggOnojOP9d9FOTxp9gw3A.png" /><figcaption>ตัวอย่างการทำ Operation แบบ async</figcaption></figure><h3>Queues Type</h3><p>ประเภทของ Queue แบ่งได้ออกเป็น 2 ประเภทใหญ่ ๆ คือ</p><h4>Main Queue</h4><p>เป็น queue ที่มีระดับ priority สูงสุดเนื่องจากเป็นการทำงานบน Main thread ใช้ในการ update UI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fGRccrXiK0ibUfugTt1fTA.png" /><figcaption>ตัวอย่างการเรียกใช้งานผ่านคำสั่ง DispatchQueue.main</figcaption></figure><h4>Global Queue</h4><p>เป็นงานทำงานของ queue ที่ไม่เกี่ยวข้องกับ UI ทำงานในส่วนของ Background โดยสามารถแบ่งประเภทหลัก ๆ ได้ด้วย QOS ( Quality of service ) ซึ่งเป็นตัวกำหนดลำดับสำคัญ</p><blockquote><strong>higher priority</strong> work is performed more quickly and with more resources than lower priority work — apple document</blockquote><p>โดยแบ่งออกเป็น 4 ประเภทด้วยกัน ได้แก่</p><ul><li>.userInteractive เป็น quality-of-service class สำหรับการงานที่เป็น user-interactive เช่น animations, event handling, หรือ updating your app’s user interface ใช้งานคล้ายกับ Main Queue เลย</li><li>.userInitiated เป็น quality-of-service class สำหรับบังคับให้ User ทำการหยุดการทำงานบน application เพื่อรอการทำงานของ task นี้เสร็จก่อน เหมาะสำหรับการทำการโหลดข้อมูลบางอย่างมาแสดงผล</li><li>.utility เป็น quality-of-service class สำหรับงานที่ user ไม่ได้ติดตามผลอย่างทันที</li><li>.background เป็น quality-of-service class สำหรับงานที่มีความสำคัญ ใช้เวลานานและไม่ต้องการผู้ใช้งานเห็น เช่น การ maintenance หรือ clean tasks ต่าง ๆ</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*39u3k6NQxSojwAEZE5LxFQ.png" /><figcaption>ตัวอย่างการใช้งาน Global Queue</figcaption></figure><p>จริง ๆ แล้วส่วนการทำ GCD สามารถทำอะไรได้อีกหลายอย่างเลยนะครับ เช่น การทำ delay และอีกหลาย ๆ อย่าง ซึ่งสามารถนำไปลองเล่นกันได้ สุดท้ายนี้ขอขอบคุณทุกคนที่เข้ามาอ่านหากมีข้อผิดพลาดต้องขออภัยมา ณ ที่นี้ ค้าบบบ 😁</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fafc698a6c5c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>