{"id":1900,"date":"2024-08-12T20:16:48","date_gmt":"2024-08-12T20:16:48","guid":{"rendered":"https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/"},"modified":"2024-08-12T20:16:48","modified_gmt":"2024-08-12T20:16:48","slug":"cancellable-withobservationtracking-in-swift","status":"publish","type":"post","link":"https:\/\/dev95.site\/ar\/cancellable-withobservationtracking-in-swift\/","title":{"rendered":"Cancellable withObservationTracking in Swift"},"content":{"rendered":"<div id=\"dev95-389176791\" class=\"dev95-- dev95-entity-placement\"><script async=\"async\" data-cfasync=\"false\" src=\"https:\/\/pl27862732.profitableratecpmnetwork.com\/2ad7a50e0bbc23ac6801d7b77c501463\/invoke.js\"><\/script>\r\n<div id=\"container-2ad7a50e0bbc23ac6801d7b77c501463\"><\/div><\/div><div>\n<p class=\"wp-block-paragraph\">Observation framework came out along with iOS 17 in 2023. Using this framework, we can make objects observable very easily. Please refer to <a href=\"https:\/\/augmentedcode.io\/2023\/06\/07\/observable-macro-in-swiftui\/\">@Observable macro in\u00a0SwiftUI<\/a> for quick recap if needed. It also has a function  <code><a href=\"https:\/\/developer.apple.com\/documentation\/observation\/withobservationtracking(_:onchange:)\">withObservationTracking(_:onChange:)<\/a><\/code> what can be used for cases where we would want to manually get a callback when a tracked property is about to change. This function works as a one shot function and the <code>onChange<\/code> closure is called only once. Note that it is called before the value has actually changed. If we want to get the changed value, we would need to read the value on the next run loop cycle. It would be much more useful if we could use this function in a way where we could have an observation token and as long as it is set, the observation is active. Here is the function with cancellation support.<\/p>\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\">\n<div class=\"wp-block-embed__wrapper\">\n<style>.gist table { margin-bottom: 0; }<\/style>\n<div style=\"tab-size: 8\" id=\"gist131993442\" class=\"gist\">\n<div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n<div class=\"gist-data\">\n<div class=\"js-gist-file-update-container js-task-list-container\">\n<div id=\"file-observation-swift\" class=\"file my-2\">\n<div itemprop=\"text\" class=\"Box-body p-0 blob-wrapper data type-swift  \" style=\"overflow: auto\" tabindex=\"0\" role=\"region\" aria-label=\"Observation.swift content, created by laevandus on 07:59PM on August 12, 2024.\">\n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n<p>  <template class=\"js-file-alert-template\"><\/p>\n<div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" data-component=\"Octicon\" height=\"16\" viewbox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n    <span><br \/>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br \/>\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a><br \/>\n    <\/span><\/p>\n<div data-view-component=\"true\" class=\"flash-action\">        <a href=\"https:\/\/augmentedcode.io\/2024\/08\/12\/cancellable-withobservationtracking-in-swift\/%7B%7B%20revealButtonHref%20%7D%7D\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters<br \/>\n<\/a>\n<\/div>\n<\/div>\n<p><\/template><br \/>\n<template class=\"js-line-alert-template\"><br \/>\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\"><br \/>\n    <svg aria-hidden=\"true\" data-component=\"Octicon\" height=\"16\" viewbox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n<\/span><\/template><\/p><div id=\"dev95-3975540850\" class=\"dev95- dev95-entity-placement\"><center>\r\n<script>\r\n  atOptions = {\r\n    'key' : '4ba6b6513c00e0ba76511f798ae56401',\r\n    'format' : 'iframe',\r\n    'height' : 50,\r\n    'width' : 320,\r\n    'params' : {}\r\n  };\r\n<\/script>\r\n<script src=\"https:\/\/www.highrevenueformat.com\/4ba6b6513c00e0ba76511f798ae56401\/invoke.js\"><\/script>\r\n\t<\/center><\/div>\n<table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"Observation.swift\">\n<tr>\n<td id=\"file-observation-swift-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n<td id=\"file-observation-swift-LC1\" class=\"blob-code blob-code-inner js-file-line\">func withObservationTracking(<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n<td id=\"file-observation-swift-LC2\" class=\"blob-code blob-code-inner js-file-line\">    _ apply: @escaping () -&gt; Void,<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n<td id=\"file-observation-swift-LC3\" class=\"blob-code blob-code-inner js-file-line\">    token: @escaping () -&gt; String?,<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n<td id=\"file-observation-swift-LC4\" class=\"blob-code blob-code-inner js-file-line\">    willChange: (@Sendable () -&gt; Void)? = nil,<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n<td id=\"file-observation-swift-LC5\" class=\"blob-code blob-code-inner js-file-line\">    didChange: @escaping @Sendable () -&gt; Void<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n<td id=\"file-observation-swift-LC6\" class=\"blob-code blob-code-inner js-file-line\">) {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n<td id=\"file-observation-swift-LC7\" class=\"blob-code blob-code-inner js-file-line\">    withObservationTracking(apply) {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n<td id=\"file-observation-swift-LC8\" class=\"blob-code blob-code-inner js-file-line\">        guard token() != nil else { return }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n<td id=\"file-observation-swift-LC9\" class=\"blob-code blob-code-inner js-file-line\">        willChange?()<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n<td id=\"file-observation-swift-LC10\" class=\"blob-code blob-code-inner js-file-line\">        RunLoop.current.perform {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n<td id=\"file-observation-swift-LC11\" class=\"blob-code blob-code-inner js-file-line\">            didChange()<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n<td id=\"file-observation-swift-LC12\" class=\"blob-code blob-code-inner js-file-line\">            withObservationTracking(<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n<td id=\"file-observation-swift-LC13\" class=\"blob-code blob-code-inner js-file-line\">                apply,<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n<td id=\"file-observation-swift-LC14\" class=\"blob-code blob-code-inner js-file-line\">                token: token,<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n<td id=\"file-observation-swift-LC15\" class=\"blob-code blob-code-inner js-file-line\">                willChange: willChange,<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n<td id=\"file-observation-swift-LC16\" class=\"blob-code blob-code-inner js-file-line\">                didChange: didChange<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n<td id=\"file-observation-swift-LC17\" class=\"blob-code blob-code-inner js-file-line\">            )<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n<td id=\"file-observation-swift-LC18\" class=\"blob-code blob-code-inner js-file-line\">        }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n<td id=\"file-observation-swift-LC19\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-observation-swift-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n<td id=\"file-observation-swift-LC20\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<\/table>\n<\/div><\/div>\n<\/p><\/div>\n<\/div><\/div>\n<div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/laevandus\/6723a8b9d140f471900b6e0f3176ccfb\/raw\/abbefbd61a6a787d55145f96637ae0d99ced912f\/Observation.swift\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a><br \/>\n        <a href=\"https:\/\/gist.github.com\/laevandus\/6723a8b9d140f471900b6e0f3176ccfb#file-observation-swift\" class=\"Link--inTextBlock\"><br \/>\n          Observation.swift<br \/>\n        <\/a><br \/>\n        hosted with \u2764 by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\/\">GitHub<\/a>\n      <\/div>\n<\/p><\/div>\n<\/div>\n<\/div>\n<\/figure>\n<p class=\"wp-block-paragraph\">The apply closure drives which values are being tracked, and this is passed into the existing <code>withObservationTracking(_:onChange:)<\/code> function. The token closure controls if the change should be handled and if we need to continue tracking. Will and did change are closures called before and after the value has changed.<\/p>\n<p class=\"wp-block-paragraph\">Here is a simple example where we have a view which controls if the observation should be active or not. Changing the value in the view model only triggers the print lines when observation token is set.<\/p>\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\">\n<div class=\"wp-block-embed__wrapper\">\n<style>.gist table { margin-bottom: 0; }<\/style>\n<div style=\"tab-size: 8\" id=\"gist131993545\" class=\"gist\">\n<div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n<div class=\"gist-data\">\n<div class=\"js-gist-file-update-container js-task-list-container\">\n<div id=\"file-contentview-swift\" class=\"file my-2\">\n<div itemprop=\"text\" class=\"Box-body p-0 blob-wrapper data type-swift  \" style=\"overflow: auto\" tabindex=\"0\" role=\"region\" aria-label=\"ContentView.swift content, created by laevandus on 08:06PM on August 12, 2024.\">\n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n<p>  <template class=\"js-file-alert-template\"><\/p>\n<div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" data-component=\"Octicon\" height=\"16\" viewbox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n    <span><br \/>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br \/>\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a><br \/>\n    <\/span><\/p>\n<div data-view-component=\"true\" class=\"flash-action\">        <a href=\"https:\/\/augmentedcode.io\/2024\/08\/12\/cancellable-withobservationtracking-in-swift\/%7B%7B%20revealButtonHref%20%7D%7D\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters<br \/>\n<\/a>\n<\/div>\n<\/div>\n<p><\/template><br \/>\n<template class=\"js-line-alert-template\"><br \/>\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\"><br \/>\n    <svg aria-hidden=\"true\" data-component=\"Octicon\" height=\"16\" viewbox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n<\/span><\/template><\/p>\n<table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"ContentView.swift\">\n<tr>\n<td id=\"file-contentview-swift-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n<td id=\"file-contentview-swift-LC1\" class=\"blob-code blob-code-inner js-file-line\">struct ContentView: View {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n<td id=\"file-contentview-swift-LC2\" class=\"blob-code blob-code-inner js-file-line\">    @State private var viewModel = ViewModel()<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n<td id=\"file-contentview-swift-LC3\" class=\"blob-code blob-code-inner js-file-line\">    @State private var observationToken: String?<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n<td id=\"file-contentview-swift-LC4\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n<td id=\"file-contentview-swift-LC5\" class=\"blob-code blob-code-inner js-file-line\">    var body: some View {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n<td id=\"file-contentview-swift-LC6\" class=\"blob-code blob-code-inner js-file-line\">        VStack {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n<td id=\"file-contentview-swift-LC7\" class=\"blob-code blob-code-inner js-file-line\">            Text(viewModel.title)<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n<td id=\"file-contentview-swift-LC8\" class=\"blob-code blob-code-inner js-file-line\">            Button(&#8220;Add&#8221;) {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n<td id=\"file-contentview-swift-LC9\" class=\"blob-code blob-code-inner js-file-line\">                viewModel.add()<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n<td id=\"file-contentview-swift-LC10\" class=\"blob-code blob-code-inner js-file-line\">            }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n<td id=\"file-contentview-swift-LC11\" class=\"blob-code blob-code-inner js-file-line\">            Button(&#8220;Start Observing&#8221;) {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n<td id=\"file-contentview-swift-LC12\" class=\"blob-code blob-code-inner js-file-line\">                guard observationToken == nil else { return }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n<td id=\"file-contentview-swift-LC13\" class=\"blob-code blob-code-inner js-file-line\">                observationToken = UUID().uuidString<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n<td id=\"file-contentview-swift-LC14\" class=\"blob-code blob-code-inner js-file-line\">                observeAndPrint()<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n<td id=\"file-contentview-swift-LC15\" class=\"blob-code blob-code-inner js-file-line\">            }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n<td id=\"file-contentview-swift-LC16\" class=\"blob-code blob-code-inner js-file-line\">            Button(&#8220;Stop Observing&#8221;) {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n<td id=\"file-contentview-swift-LC17\" class=\"blob-code blob-code-inner js-file-line\">                observationToken = nil<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n<td id=\"file-contentview-swift-LC18\" class=\"blob-code blob-code-inner js-file-line\">            }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n<td id=\"file-contentview-swift-LC19\" class=\"blob-code blob-code-inner js-file-line\">        }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n<td id=\"file-contentview-swift-LC20\" class=\"blob-code blob-code-inner js-file-line\">        .padding()<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n<td id=\"file-contentview-swift-LC21\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n<td id=\"file-contentview-swift-LC22\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n<td id=\"file-contentview-swift-LC23\" class=\"blob-code blob-code-inner js-file-line\">    func observeAndPrint() {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n<td id=\"file-contentview-swift-LC24\" class=\"blob-code blob-code-inner js-file-line\">        withObservationTracking({<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L25\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"25\"><\/td>\n<td id=\"file-contentview-swift-LC25\" class=\"blob-code blob-code-inner js-file-line\">            _ = viewModel.title<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L26\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"26\"><\/td>\n<td id=\"file-contentview-swift-LC26\" class=\"blob-code blob-code-inner js-file-line\">        }, token: {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L27\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"27\"><\/td>\n<td id=\"file-contentview-swift-LC27\" class=\"blob-code blob-code-inner js-file-line\">            observationToken<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L28\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"28\"><\/td>\n<td id=\"file-contentview-swift-LC28\" class=\"blob-code blob-code-inner js-file-line\">        }, willChange: { [weak viewModel] in<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L29\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"29\"><\/td>\n<td id=\"file-contentview-swift-LC29\" class=\"blob-code blob-code-inner js-file-line\">            guard let viewModel else { return }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L30\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"30\"><\/td>\n<td id=\"file-contentview-swift-LC30\" class=\"blob-code blob-code-inner js-file-line\">            print(&#8220;will change (viewModel.title)&#8221;)<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L31\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"31\"><\/td>\n<td id=\"file-contentview-swift-LC31\" class=\"blob-code blob-code-inner js-file-line\">        }, didChange: { [weak viewModel] in<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L32\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"32\"><\/td>\n<td id=\"file-contentview-swift-LC32\" class=\"blob-code blob-code-inner js-file-line\">            guard let viewModel else { return }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L33\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"33\"><\/td>\n<td id=\"file-contentview-swift-LC33\" class=\"blob-code blob-code-inner js-file-line\">            print(&#8220;did change (viewModel.title)&#8221;)<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L34\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"34\"><\/td>\n<td id=\"file-contentview-swift-LC34\" class=\"blob-code blob-code-inner js-file-line\">        })<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L35\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"35\"><\/td>\n<td id=\"file-contentview-swift-LC35\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L36\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"36\"><\/td>\n<td id=\"file-contentview-swift-LC36\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L37\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"37\"><\/td>\n<td id=\"file-contentview-swift-LC37\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L38\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"38\"><\/td>\n<td id=\"file-contentview-swift-LC38\" class=\"blob-code blob-code-inner js-file-line\">@Observable final class ViewModel {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L39\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"39\"><\/td>\n<td id=\"file-contentview-swift-LC39\" class=\"blob-code blob-code-inner js-file-line\">    var counter = 0<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L40\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"40\"><\/td>\n<td id=\"file-contentview-swift-LC40\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L41\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"41\"><\/td>\n<td id=\"file-contentview-swift-LC41\" class=\"blob-code blob-code-inner js-file-line\">    func add() {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L42\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"42\"><\/td>\n<td id=\"file-contentview-swift-LC42\" class=\"blob-code blob-code-inner js-file-line\">        counter += 1<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L43\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"43\"><\/td>\n<td id=\"file-contentview-swift-LC43\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L44\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"44\"><\/td>\n<td id=\"file-contentview-swift-LC44\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L45\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"45\"><\/td>\n<td id=\"file-contentview-swift-LC45\" class=\"blob-code blob-code-inner js-file-line\">    var title: String {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L46\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"46\"><\/td>\n<td id=\"file-contentview-swift-LC46\" class=\"blob-code blob-code-inner js-file-line\">        &#8220;Number of items: (counter)&#8221;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L47\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"47\"><\/td>\n<td id=\"file-contentview-swift-LC47\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-contentview-swift-L48\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"48\"><\/td>\n<td id=\"file-contentview-swift-LC48\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<\/table>\n<\/div><\/div>\n<\/p><\/div>\n<\/div><\/div>\n<div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/laevandus\/15a55beeaed29bda8b335844863a5b7c\/raw\/0a96c29a5959a817f00c26baeba3e76fdceda80f\/ContentView.swift\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a><br \/>\n        <a href=\"https:\/\/gist.github.com\/laevandus\/15a55beeaed29bda8b335844863a5b7c#file-contentview-swift\" class=\"Link--inTextBlock\"><br \/>\n          ContentView.swift<br \/>\n        <\/a><br \/>\n        hosted with \u2764 by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\/\">GitHub<\/a>\n      <\/div>\n<\/p><\/div>\n<\/div>\n<\/div>\n<\/figure>\n<p class=\"wp-block-paragraph\">If this was helpful, please let me know on <a href=\"https:\/\/mastodon.social\/@toomasvahter\" target=\"_blank\" rel=\"noreferrer noopener\">Mastodon@toomasvahter<\/a> or\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/twitter.com\/toomasvahter\" target=\"_blank\">Twitter @toomasvahter<\/a>. Feel free to subscribe to <a rel=\"noreferrer noopener\" aria-label=\"RSS feed (opens in a new tab)\" href=\"https:\/\/augmentedcode.io\/blog\/feed\/\" target=\"_blank\">RSS feed<\/a>. Thank you for reading.<\/p>\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-background wp-element-button\" href=\"https:\/\/www.patreon.com\/toomasvahter\" style=\"background-color:#ff424d\" target=\"_blank\" rel=\"https:\/\/www.patreon.com\/toomasvahter\">Support me on Patreon<\/a><\/div>\n<\/div>\n<\/div>\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-text-color has-background\" href=\"https:\/\/www.paypal.me\/toomasvahter\" style=\"background-color:#ffc300;color:#002e8c;\" target=\"_blank\" rel=\"https:\/\/www.paypal.me\/toomasvahter\">Donate with Paypal<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-secondary-background-color has-background wp-element-button\" rel=\"https:\/\/www.buymeacoffee.com\/laevandus\">Buy me a coffee<\/a><\/div>\n<\/div>\n<p class=\"wp-block-paragraph\">\n<\/div>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1900\" class=\"pvc_stats total_only  \" data-element-id=\"1900\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/i0.wp.com\/dev95.site\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif?resize=16%2C16&#038;ssl=1\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Observation framework came out along with iOS 17 in 2023. Using this framework, we can make objects observable very easily. Please refer to @Observable macro in\u00a0SwiftUI for quick recap if needed. It also has a function withObservationTracking(_:onChange:) what can be<\/p>\n<div class=\"hosteria-entry-more\"><a href=\"https:\/\/dev95.site\/ar\/cancellable-withobservationtracking-in-swift\/\" class=\"no-underline font-light  group-hover:text-primary-800 dark:group-hover:text-primary-300 py-1\">Read more &gt;&gt;&gt;<\/a><\/div>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1900\" class=\"pvc_stats total_only\" data-element-id=\"1900\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewbox=\"0 0 502 315\" preserveaspectratio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dev95.site\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=\"0\" \/><\/p>\n<div class=\"pvc_clear\"><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fp_fajr_begins":"","fp_fajr_iqamah":"","fp_dhuhr_begins":"","fp_dhuhr_iqamah":"","fp_asr_begins":"","fp_asr_iqamah":"","fp_maghrib_begins":"","fp_maghrib_iqamah":"","fp_isha_begins":"","fp_isha_iqamah":"","fp_midnight":"","fp_midnight_name":"","fp_sunrise":"","fp_single_prayer_begins_title":"","fp_single_prayer_iqamah_title":"","fp_prayer_times_for_today":"","fp_hijra_date":"","fp_fajr_name":"","fp_dhuhr_name":"","fp_asr_name":"","fp_maghrib_name":"","fp_isha_name":"","fp_sunrise_name":"","fp_currentDate":"","fp_current_time":"","fp_current_title":"","fp_current_location":"","fp_masjid_name":"","fp_prayer_title":"","fp_next_prayer_iqamah_time":"","fp_next_prayer_iqamah_title":"","fp_next_prayer_begins_time":"","fp_next_prayer_begins_title":"","fp_next_prayer_title":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[37],"tags":[],"class_list":["post-1900","post","type-post","status-publish","format-standard","hentry","category-posts"],"a3_pvc":{"activated":true,"total_views":0,"today_views":0},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Cancellable withObservationTracking in Swift - Dev95<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dev95.site\/ar\/cancellable-withobservationtracking-in-swift\/\" \/>\n<meta property=\"og:locale\" content=\"ar_AR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cancellable withObservationTracking in Swift - Dev95\" \/>\n<meta property=\"og:description\" content=\"Observation framework came out along with iOS 17 in 2023. Using this framework, we can make objects observable very easily. Please refer to @Observable macro in\u00a0SwiftUI for quick recap if needed. It also has a function withObservationTracking(_:onChange:) what can beRead more &gt;&gt;&gt;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dev95.site\/ar\/cancellable-withobservationtracking-in-swift\/\" \/>\n<meta property=\"og:site_name\" content=\"Dev95\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-12T20:16:48+00:00\" \/>\n<meta name=\"author\" content=\"dev95\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u0643\u064f\u062a\u0628 \u0628\u0648\u0627\u0633\u0637\u0629\" \/>\n\t<meta name=\"twitter:data1\" content=\"dev95\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u0648\u0642\u062a \u0627\u0644\u0642\u0631\u0627\u0621\u0629 \u0627\u0644\u0645\u064f\u0642\u062f\u0651\u0631\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u062f\u0642\u0627\u0626\u0642\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/\"},\"author\":{\"name\":\"dev95\",\"@id\":\"https:\\\/\\\/dev95.site\\\/#\\\/schema\\\/person\\\/b807805ffe2916206b04d0938bce0298\"},\"headline\":\"Cancellable withObservationTracking in Swift\",\"datePublished\":\"2024-08-12T20:16:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/\"},\"wordCount\":506,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/dev95.site\\\/#organization\"},\"articleSection\":[\"Posts\"],\"inLanguage\":\"ar\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/\",\"url\":\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/\",\"name\":\"Cancellable withObservationTracking in Swift - Dev95\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/dev95.site\\\/#website\"},\"datePublished\":\"2024-08-12T20:16:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/#breadcrumb\"},\"inLanguage\":\"ar\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/dev95.site\\\/cancellable-withobservationtracking-in-swift\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/dev95.site\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cancellable withObservationTracking in Swift\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/dev95.site\\\/#website\",\"url\":\"https:\\\/\\\/dev95.site\\\/\",\"name\":\"Dev95\",\"description\":\"A comprehensive platform for data and knowledge, delivering reliable content that meets the aspirations of readers and enthusiasts.\",\"publisher\":{\"@id\":\"https:\\\/\\\/dev95.site\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/dev95.site\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ar\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/dev95.site\\\/#organization\",\"name\":\"Dev95\",\"url\":\"https:\\\/\\\/dev95.site\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ar\",\"@id\":\"https:\\\/\\\/dev95.site\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/dev95.site\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/rbrrbr-6.png?fit=512%2C512&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/dev95.site\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/rbrrbr-6.png?fit=512%2C512&ssl=1\",\"width\":512,\"height\":512,\"caption\":\"Dev95\"},\"image\":{\"@id\":\"https:\\\/\\\/dev95.site\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/dev95.site\\\/#\\\/schema\\\/person\\\/b807805ffe2916206b04d0938bce0298\",\"name\":\"dev95\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ar\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a70a73d950838b20cd80d7ebdc955737e802e8cd896044c5473b32b946c0662a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a70a73d950838b20cd80d7ebdc955737e802e8cd896044c5473b32b946c0662a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a70a73d950838b20cd80d7ebdc955737e802e8cd896044c5473b32b946c0662a?s=96&d=mm&r=g\",\"caption\":\"dev95\"},\"url\":\"https:\\\/\\\/dev95.site\\\/ar\\\/author\\\/mohammad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cancellable withObservationTracking in Swift - Dev95","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/dev95.site\/ar\/cancellable-withobservationtracking-in-swift\/","og_locale":"ar_AR","og_type":"article","og_title":"Cancellable withObservationTracking in Swift - Dev95","og_description":"Observation framework came out along with iOS 17 in 2023. Using this framework, we can make objects observable very easily. Please refer to @Observable macro in\u00a0SwiftUI for quick recap if needed. It also has a function withObservationTracking(_:onChange:) what can beRead more &gt;&gt;&gt;","og_url":"https:\/\/dev95.site\/ar\/cancellable-withobservationtracking-in-swift\/","og_site_name":"Dev95","article_published_time":"2024-08-12T20:16:48+00:00","author":"dev95","twitter_card":"summary_large_image","twitter_misc":{"\u0643\u064f\u062a\u0628 \u0628\u0648\u0627\u0633\u0637\u0629":"dev95","\u0648\u0642\u062a \u0627\u0644\u0642\u0631\u0627\u0621\u0629 \u0627\u0644\u0645\u064f\u0642\u062f\u0651\u0631":"3 \u062f\u0642\u0627\u0626\u0642"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/#article","isPartOf":{"@id":"https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/"},"author":{"name":"dev95","@id":"https:\/\/dev95.site\/#\/schema\/person\/b807805ffe2916206b04d0938bce0298"},"headline":"Cancellable withObservationTracking in Swift","datePublished":"2024-08-12T20:16:48+00:00","mainEntityOfPage":{"@id":"https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/"},"wordCount":506,"commentCount":0,"publisher":{"@id":"https:\/\/dev95.site\/#organization"},"articleSection":["Posts"],"inLanguage":"ar","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/","url":"https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/","name":"Cancellable withObservationTracking in Swift - Dev95","isPartOf":{"@id":"https:\/\/dev95.site\/#website"},"datePublished":"2024-08-12T20:16:48+00:00","breadcrumb":{"@id":"https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/#breadcrumb"},"inLanguage":"ar","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/dev95.site\/cancellable-withobservationtracking-in-swift\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dev95.site\/"},{"@type":"ListItem","position":2,"name":"Cancellable withObservationTracking in Swift"}]},{"@type":"WebSite","@id":"https:\/\/dev95.site\/#website","url":"https:\/\/dev95.site\/","name":"Dev95","description":"A comprehensive platform for data and knowledge, delivering reliable content that meets the aspirations of readers and enthusiasts.","publisher":{"@id":"https:\/\/dev95.site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dev95.site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ar"},{"@type":"Organization","@id":"https:\/\/dev95.site\/#organization","name":"Dev95","url":"https:\/\/dev95.site\/","logo":{"@type":"ImageObject","inLanguage":"ar","@id":"https:\/\/dev95.site\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/dev95.site\/wp-content\/uploads\/2026\/07\/rbrrbr-6.png?fit=512%2C512&ssl=1","contentUrl":"https:\/\/i0.wp.com\/dev95.site\/wp-content\/uploads\/2026\/07\/rbrrbr-6.png?fit=512%2C512&ssl=1","width":512,"height":512,"caption":"Dev95"},"image":{"@id":"https:\/\/dev95.site\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/dev95.site\/#\/schema\/person\/b807805ffe2916206b04d0938bce0298","name":"dev95","image":{"@type":"ImageObject","inLanguage":"ar","@id":"https:\/\/secure.gravatar.com\/avatar\/a70a73d950838b20cd80d7ebdc955737e802e8cd896044c5473b32b946c0662a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a70a73d950838b20cd80d7ebdc955737e802e8cd896044c5473b32b946c0662a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a70a73d950838b20cd80d7ebdc955737e802e8cd896044c5473b32b946c0662a?s=96&d=mm&r=g","caption":"dev95"},"url":"https:\/\/dev95.site\/ar\/author\/mohammad\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/posts\/1900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/comments?post=1900"}],"version-history":[{"count":0,"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/posts\/1900\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/media?parent=1900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/categories?post=1900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev95.site\/ar\/wp-json\/wp\/v2\/tags?post=1900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}