<?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 Toni Sučić on Medium]]></title>
        <description><![CDATA[Stories by Toni Sučić on Medium]]></description>
        <link>https://medium.com/@tonisucic?source=rss-4e0828fe41fc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CA0XC16zN2A6zyea7fRpEQ.jpeg</url>
            <title>Stories by Toni Sučić on Medium</title>
            <link>https://medium.com/@tonisucic?source=rss-4e0828fe41fc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 14:06:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tonisucic/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[Polymorphic Map Annotations in SwiftUI + MapKit]]></title>
            <link>https://medium.com/swlh/map-and-heterogeneous-annotation-views-in-swiftui-mapkit-93cd17e98ae1?source=rss-4e0828fe41fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/93cd17e98ae1</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[maps]]></category>
            <category><![CDATA[mapkit]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Toni Sučić]]></dc:creator>
            <pubDate>Sun, 19 Jul 2020 06:31:47 GMT</pubDate>
            <atom:updated>2020-07-26T12:20:15.512Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>On WWDC 2020, Apple announced a much sought-after addition to SwiftUI, namely the new SwiftUI-native <a href="https://developer.apple.com/documentation/mapkit/map">Map</a> view. 🗺 Finally we don’t have to create messy wrappers around <a href="https://developer.apple.com/documentation/mapkit/mkmapview">MKMapView</a> anymore. 🙌 However, there’s a catch.</p><blockquote>“Maps only show annotation views of the same type, backed by a single collection.” — Apple’s official documentation</blockquote><p>At first glance I found this to be a bit strange. The Map view supports three types of annotation views: <a href="https://developer.apple.com/documentation/mapkit/mappin">MapPin</a>, <a href="https://developer.apple.com/documentation/mapkit/mapmarker">MapMarker</a>, and <a href="https://developer.apple.com/documentation/mapkit/mapannotation">MapAnnotation</a>. But for some reason, Apple won’t let us use these types together, although doing so is both possible and common practice with <a href="https://developer.apple.com/documentation/mapkit/mkmapview">MKMapView</a>. How come? 🤔</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KYSh2MUe0U3Eowjt" /></figure><p>While UIKit is object-oriented, SwiftUI is protocol-oriented (remember <a href="https://www.youtube.com/watch?v=xE5EcHuz52I">Crusty from WWDC 2015</a>?) Because of this, virtually all data types in SwiftUI are either structs or enums used in conjunction with protocols. This comes with a set of challenges that I’ll elaborate on in a bit.</p><p>Each of the aforementioned annotation views implements <a href="https://developer.apple.com/documentation/mapkit/mapannotationprotocol">MapAnnotationProtocol</a>. If we look at Apple’s documentation for this protocol, we’re told not to make our own types conform to it:</p><blockquote>“Don’t create types conforming to this protocol. Instead, use one of the framework-provided types <a href="https://developer.apple.com/documentation/mapkit/mapannotation">MapAnnotation</a>, <a href="https://developer.apple.com/documentation/mapkit/mapmarker">MapMarker</a>, and <a href="https://developer.apple.com/documentation/mapkit/mappin">MapPin</a>.”</blockquote><p>Very little information is disclosed about <a href="https://developer.apple.com/documentation/mapkit/mapannotationprotocol">MapAnnotationProtocol</a>, but from what I’ve gathered it contains one undocumented field: <em>_annotationData</em> of type <em>_MapAnnotationData.</em><strong> </strong>Xcode won’t autocomplete it, nor will it provide any information about it, but it’s still exposed to us because Swift’s grammar requires that all protocol fields be public, which means that we’ll be able to use it further down in the article. 👏</p><p>So how does Apple want you to use <a href="https://developer.apple.com/documentation/mapkit/map">Map</a>? Here’s a basic example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Udz6gLXT3fde5Ku6aMPydA.png" /><figcaption>Fig 1. A map that shows two (fake) restaurants in Oslo, Norway</figcaption></figure><p>In the example above the collection of annotation items is homogeneous. If you look at the <em>annotationContent(annotationItem:)</em> function you’ll see that it returns an opaque type, which is a common theme in SwiftUI. Since the return type is opaque, all paths of the function must return the exact same type, which means that you may not return multiple <em>MapAnnotation</em> objects in the same function where the content type differs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B768_qTtZfq4N5EeP-6Vow.png" /><figcaption>Fig 2. Xcode complains that not all paths return the exact same concrete type since the content of each MapAnnotation may differ</figcaption></figure><p>You run into a similar problem if you want to return either <a href="https://developer.apple.com/documentation/mapkit/mapannotation">MapAnnotation</a>, <a href="https://developer.apple.com/documentation/mapkit/mapmarker">MapMarker</a>, or <a href="https://developer.apple.com/documentation/mapkit/mappin">MapPin</a> polymorphically based on a generic <em>AnnotationItem</em> type. Here’s what happens when we try to generalize the annotation items:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*487Yb8-iNKK8786mGxw2Kg.png" /><figcaption>Fig 3. We start running into limitations in Swift’s type system</figcaption></figure><p>Annotation items must conform to <em>Identifiable</em>, which is a PAT (protocol with an associated type), and PATs, unlike plain protocols, don’t have an existential. This is what’s causing us the errors in fig 3. The term “existential” is seldom used, because in most cases you don’t need to explicitly know about them. If you’d like to learn more about them, you can do so <a href="https://robnapier.net/existential-spelling">here</a>. Long story short: protocols have an existential, but PATs don’t, thus PATs can’t be used as types on their own. They can only be used to constrain a generic type.</p><p>There was a <a href="https://github.com/austinzheng/swift-evolution/blob/az-existentials/proposals/XXXX-enhanced-existentials.md">proposal</a> to enhance existentials so that they’d be applicable in more contexts, but progress on this topic has been slow. In Swift’s generics manifesto, <a href="https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generalized-existentials">generalized existentials</a> are listed as an unlikely future feature addition. Generalized existentials would have made it possible to create a more elegant API for heterogeneous collections of polymorphic types, but alas, we don’t have such a language feature available to us as of now, so we’ll have to resort to a different solution.</p><p>We run into yet another problem with <em>MapAnnotationProtocol:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NzQB4LZFQL6jE1IZrCCzsw.png" /><figcaption>Fig 4. One of the initializers of the Map struct</figcaption></figure><p>As can be seen in fig 4, the generic <em>Annotation</em> type is constrained by <em>MapAnnotationProtocol. </em>This means that the type returned by the <em>annotationContent(annotationItem:)</em> function must be a concrete type, which is why we use the opaque return type syntax (i.e. the <em>some</em> keyword.)</p><p>So at this point it’s clear why Apple limited the interface of <a href="https://developer.apple.com/documentation/mapkit/map">Map</a> to only support homogeneous collections of map annotation items and views, but is there perhaps a way around this? Turns out, there <em>is</em>.</p><p>Fortunately, there is a workaround for both of the aforementioned problems which you may be familiar with: <a href="https://medium.com/@chris_dus/type-erasure-in-swift-84480c807534">type erasure</a>. It is in fact possible to create a type-erased version of <em>MapAnnotationProtocol </em>and it actually works in spite of one of the fields being undocumented / hidden from us. One thing to take note of though, is that <em>MapAnnotationProtocol </em>isn’t a PAT, so creating a type-erased version of it wouldn’t have been necessary had it not been for the fact that the <em>annotationContent(annotationItem:)</em> function must return a concrete implementation of <em>MapAnnotationProtocol </em>as opposed to the existential of <em>MapAnnotationProtocol. </em>Fortunately for us, a type-erased version of <em>MapAnnotationProtocol</em> satisfies this requirement.</p><p>So what could <em>Map</em> look like with some type erasure thrown in?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NiK_bQeulVSnj1htUVPUbw.png" /><figcaption>Fig 5. Map with a heterogeneous array of annotation items and annotation views</figcaption></figure><p>For the example above I created a couple of types of my own: <em>MapAnnotationItem</em>, <em>AnyMapAnnotationItem</em>, and <em>AnyMapAnnotation</em>. I also extended <em>Map</em> with two convenience initializers that work with these types (notice how the initializer in fig 5 doesn’t require you to provide a <em>annotationContent(annotationItem:)</em> function.)</p><p>Each annotation item type conforms to <em>MapAnnotationItem, </em>which also implements <em>Identifiable</em>. <em>AnyMapAnnotationItem </em>is used in the example above to create a heterogeneous array of annotation items by erasing the the type of each concrete <em>MapAnnotationItem</em> instance. In addition to backing each annotation with model data, <em>MapAnnotationItem </em>is also responsible for defining what the annotation view should look like with its <em>annotation</em> property (akin to how views that implement the <em>View</em> protocol define what they should look like with their <em>body</em> property.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd0a03b6a2c8c8b89fdbecbd933000d0/href">https://medium.com/media/dd0a03b6a2c8c8b89fdbecbd933000d0/href</a></iframe><p>Then there’s the <em>AnyMapAnnotation</em> type, which is the type-erased version of <em>MapAnnotationProtocol:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c25534af282bd058b0dc8687d7dfa6ea/href">https://medium.com/media/c25534af282bd058b0dc8687d7dfa6ea/href</a></iframe><p>So Apple doesn’t really want us to implement this protocol ourselves, but I think this is a reasonable use case.</p><p>Then there’s the final piece of the puzzle which are these initializer extensions that tie everything together:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/baaa1f2f97a53d0c488edea2e9d65d54/href">https://medium.com/media/baaa1f2f97a53d0c488edea2e9d65d54/href</a></iframe><p>Simply include these files in your project and you’ll be able to mix and match annotation items and annotation views. ✨</p><h3>Conclusion</h3><p>This concludes my article. I’m curious to see whether Apple will update <a href="https://developer.apple.com/documentation/mapkit/map">Map</a> to support heterogeneous annotation views out of the box in a future update. I wrote this article on 19 July 2020 after encountering the limitations described above in Xcode 12 beta 2 and felt compelled to share my learnings with others who might find this interesting or useful. Considering that Xcode 12 is still in beta and that this new Map API is unstable, we may well see changes to it in the coming weeks or months.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93cd17e98ae1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/map-and-heterogeneous-annotation-views-in-swiftui-mapkit-93cd17e98ae1">Polymorphic Map Annotations in SwiftUI + MapKit</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pitfalls to avoid when implementing push notifications]]></title>
            <link>https://tonisucic.medium.com/pitfalls-to-avoid-when-implementing-push-notifications-784965f078ab?source=rss-4e0828fe41fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/784965f078ab</guid>
            <category><![CDATA[push-notification]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[firebasecloudmessaging]]></category>
            <category><![CDATA[apps]]></category>
            <dc:creator><![CDATA[Toni Sučić]]></dc:creator>
            <pubDate>Mon, 18 Nov 2019 18:44:20 GMT</pubDate>
            <atom:updated>2022-05-21T16:37:18.904Z</atom:updated>
            <content:encoded><![CDATA[<h4>What to do and what not to do when implementing push notifications in your app, and elaborating on the concept of pull notifications to supplement push notifications.</h4><p><em>A note to the reader: I use the term pull notification in this article to refer to notifications that are stored in the database on the backend. When I originally wrote this article I used the term “message notification” to refer to such notifications, however, I later changed it to “pull notification” since this name makes more sense intuitively and forms a dichotomous relationship with the term “push notification”.</em></p><p>When implementing push notifications for your mobile app there are certain pitfalls you want to avoid.</p><ul><li>Don’t start with designing the push notification. Start with the event that will trigger it, then consider whether you need a push notification, a pull notification, or both.</li><li>Don’t directly store the payload of the push notification in the database. The title and/or the body of a push notification should never be stored in the database, since it might change in the future or be translated into other languages.</li><li>Don’t store a single device token per user. Design your database such that a user can have multiple device tokens. Some users have multiple devices (like developers who often have a test device in addition to their personal device). Create a strategy for purging invalid tokens. People get new phones and lose or break old ones, rendering old tokens orphaned/invalid. You can read more about how to do this with Firebase Cloud Messaging <a href="https://www.sentinelstand.com/article/device-group-management-with-firebase-cloud-messaging">here</a>.</li><li>Remember to remove the device token with a call to the backend when the user logs out lest they continue to receive push notifications in spite of not being authenticated.</li><li>If you’re sending out reminder notifications at a given time of day, don’t use a time-based job scheduler to schedule a function that runs once every 24 hours. It won’t work across different time zones.</li><li>Don’t confuse push notifications with pull notifications and vice versa when conversing with your team.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*00YwXNZ_cTzAlL8L.png" /><figcaption>Push notifications are typically associated with your lock screen</figcaption></figure><h4>The duality of notifications</h4><p>The way I see it, there are two types of notifications: push notifications and pull notifications. A pull notification is deceptively similar to a push notification in that like a push notification, it also has a title and a body, but unlike a push notification, a pull notification is persisted to the database and can be fetched by the app later. Pull notifications don’t appear on your lock screen, but they are viewed in your app instead.</p><ul><li><em>Push notifications</em> are <em>ephemeral</em> and <em>static</em>. They’re dispatched according to the «fire and forget» principle and are not stored in the database. It’s possible to update (“collapse”) push notifications on the lock screen by using a collapse ID/key.</li><li><em>Pull notifications</em> are <em>permanent</em> and <em>dynamic.</em> They’re templates that are populated with data from the database and then either provided to the client through a REST API, or used in the payload of a push notification.</li></ul><p>Push notifications are derived from pull notifications — not the other way around (i.e. you don’t store the payload of the push notification in the database. You only store the notification type, which is tied to a template in your business logic. This template can then be used to render a pull notification, which can be used to create a push notification). In some cases you might want to send a push notification that’s not tied to a pull notification on the backend, in which case you’d simply create a payload for it without creating a pull notification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5PNoaecNc4pS98GQOCcmwg.png" /><figcaption>Pull (Message) notification &amp; Push notification matrix</figcaption></figure><p>A pull notification might look something like this:</p><p><em>Title: Group update</em><br><em>Body: Steve shared a link in the group “Bicycle group”.</em></p><p>Looks a lot like a push notification. The pull notification also has a title and a body, but the key difference is that a pull notification doesn’t appear on a user’s lock screen like a push notification. It typically appears in the form of a notification list/feed in the app. Pull notifications are templates, as can be seen in the body of our example: &quot;{user.firstName} shared an article in the group {group.name}.&quot; The phrasing of it might change in the future, new interpolated variables might be added, and at some point it might be necessary to translate it into other languages:</p><ul><li><em>Steve delte en lenke i gruppa “Bicycle group”.</em></li><li><em>Steve hat einen Link in der Gruppe “Bicycle group” geteilt.</em></li></ul><p>You’ll therefore not find this text in the database. It’s dynamically generated from the <em>first_name</em> field in the user table and the <em>name</em> field in the group table. The definition of the template and the variables on which it depends are defined in a function in the business logic layer of the application (possibly in the model). The database has no idea what the text looks like. It just knows what type of notification it is and which tables in the database it depends on.</p><p>In a relational SQL database this setup might look something like this (with the pull notification being named message notification in this case):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*khcgAhAGJGRcXn8XZEL7ew.png" /></figure><p>Notice how the rows in the message notification table don’t store any information at all about the title and the body. The <em>message_notifications</em> table only has a type that shows what kind of notification it is, when it was created and foreign keys to other tables it depends on (simplified a bit for the sake of example). In a more complex setup you could include a table that stores whether a user has enabled or disabled a given type of push notification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*IUiAdy1M4tg4AyLsFWXjjQ.png" /><figcaption>Table for storing which notification types will trigger a push notification for a given user</figcaption></figure><p>In order to generate the title and the body for the pull notification, you need to program a function on the backend that looks at the type of the notification and generates the appropriate strings for it. If the pull notification depends on data stored in the database like in the example above, you need to query the database for that data and interpolate it in your string. This could be anything from someone’s name to the number of likes a post has received.</p><p>Dependencies like this that are fetched from the database introduce another layer of complexity, because you have to decide on what happens when the data you are querying gets deleted. Should the pull notification be deleted as well, or should it display something like &quot;&lt;deleted user&gt; shared a link in the group &lt;deleted group&gt;.&quot; or some variation thereof? There might also be pull notifications that depend on more than a user and a group, and in that case you’d need to add another foreign key column to the <em>message_notifications</em> table for that dependency. The foreign keys should be nullable in case a given notification type requires fewer dependencies (like the “achievement” notification above), or in case the referenced foreign key doesn’t exist anymore.</p><p>Once you have defined the pull notification it’s time to consider whether it makes sense to also send an accompanying push notification. If it does, you simply take the text you just dynamically generated and put it in the notification payload. You should also include the pull notification ID in the payload so that the client knows which pull notification the push notification it just received is tied to on the backend.</p><p>If the notification is localized you’ll additionally need to store the locale of the user in the user table so that the backend knows into which language the template should be translated before it’s written to an HTTP response or put in the payload of a push notification.</p><h3>Notification feed</h3><p>Your app might need a feed or a list where the user can view a history of their notifications. Facebook has the notification bell 🔔 which will display a list of notifications once you tap on it. When you tap on a given notification in the list it will take you to another screen that’s relevant to the notification. For instance, if someone accepted your friend request on Facebook and you got a notification for that, tapping on it would take you to their profile page.</p><p>When the backend provides you the notification list it can do so in two ways: it can either include all the data that’s needed to display the contextually relevant screen when you tap on it, or it can include IDs instead which would require the user to do another request to the backend to get the necessary data to display the right screen (e.g. a user’s profile page or a post in a group).</p><h4>Collapsing push notifications</h4><p>If multiple people liked your picture on Instagram, it wouldn’t make sense to store a new pull notification for each like, nor would it be a good idea to send a push notification for each of them, since any user with a large following would have their lock screen bombarded with push notifications.</p><p>For this reason, all social media apps should have one pull notification for all the likes of your post. Since pull notifications are dynamic, you can just query the number of likes for a given post and interpolate them in the title or body of the pull notification. So at first it might look like “Steve liked your post”, but after a few more likes it would look like “Steve, Tim, Craig and 4 others liked your post”. Since push notifications are derived from pull notifications, you can easily send a new push notification with the updated text for each new like, but alas, that would result in a spammed lock screen.</p><p>Fortunately there’s a way of coalescing multiple push notifications into a single push notification. This is achieved by “collapsing” the existing notification and letting a new one take its place. APNs has the concept of an <em>apns-collapse-id</em>, and GCM has a <em>collapseKey</em>. Both can be used to replace an existing push notification that’s either in flight or that has already landed on the user’s lock screen, or it could even be used to remove a push notification from the lock screen (e.g. if a user unlikes a post right after having liked it). The end result is that the user is given the impression that an existing push notification on their lock screen was refreshed with new information.</p><p>This is a lot better than spamming the lock screen with many different notifications, but it’s not perfect since new notifications will continue to appear for each new like after the user has cleared the previous push notification they got. To solve this you can implement some kind of logic that “throttles” the number of push notifications sent to the user the more likes they get. So every time the number of likes increases by an order of magnitude, the frequency by which push notifications are sent out will be reduced.</p><h3>Viewed notifications</h3><p>It’s common for apps to keep track of which notifications you’ve viewed. On Facebook, notifications you’ve previously tapped on will have a white background, whereas notifications you haven’t tapped on will have a light blue background.</p><p>A naïve approach would be to store this information on the client, but the problem with that is that the <em>viewed</em>-state wouldn’t carry over to other devices, nor would it be retained if you reinstalled the app or got a new device.</p><p>The <em>viewed-</em>state can easily be stored on the backend simply by adding a <em>viewed</em> column to the <em>message_notifications</em> table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*POEve_W37ub7WwkmRBBkcQ.png" /><figcaption><em>message_notifications table extended with “viewed” column</em></figcaption></figure><p>You could also query and count all message notifications in the past 24 hours where <em>viewed</em> = <em>FALSE</em>, and supply that count to the badge field in the push notification payload so that the user will see a badge on the app icon that reflects the number of unviewed notifications.</p><h3>Scheduled notifications and time zones</h3><p>Some apps might need push notifications to be sent out at a specific point in time, e.g. a meditation app reminding its user to meditate every evening at 8 p.m or a reminder app issuing a reminder at a user-specified date and time. In cases like this, it’s important to consider the impact time zone changes could have on the user experience, and whether it would make sense to design your backend to be time zone aware.</p><p>Because people travel to other time zones, and due to daylight saving time, your app may end up sending push notifications at the wrong time if the user’s time zone isn’t accounted for. Since the backend doesn’t know in which time zone the user is located, the app needs to report it to the backend. You could either let the user specify their time zone in the app manually, or you could make the app detect it automatically and send it to the backend whenever it changes.</p><p>It’s considered good practice to use UTC time on the backend exclusively and rely on time zone libraries for time zone conversions. The rules governing time zones in different countries are quite complex, as illustrated in <a href="https://www.youtube.com/watch?v=-5wpm-gesOY">this video</a>.</p><p>If you need to send out reminder notifications every evening at 7 p.m. you can do so by checking the time zone stored on each user before sending them out. That way it doesn’t matter whether it’s standard time or daylight saving time, or whether the user is on vacation in New Zealand or at home in Norway — the user will always get their reminder notification at 7 p.m.</p><p>If you’re using a time-based job scheduler (like cron) to send out the reminder notifications, you can schedule a function that enumerates all the users on <a href="https://crontab.guru/#0,30,45_*/1_*_*_*">this schedule</a> (cron syntax 0,30,45 */1 * * *. More on why I picked that particular schedule <a href="https://www.timeanddate.com/time/time-zones-interesting.html">here</a>), then you can send a push notification to the ones whose time zone indicates that it’s 7 p.m. from their point of reference.</p><p><em>Note: There are exceedingly few territories where the offset is 45 minutes, namely the city of Eucla in Australia, Chatham Islands (New Zealand), and the country of Nepal. If you’re convinced that your app won’t be used in these territories, the simpler schedule </em><em>*/30 * * * * would suffice.</em></p><h3>Conclusion</h3><p>Push notifications are notoriously difficult to implement since there’s more to them than meets the eye. In a more complex app it’s not immediately obvious how to approach the task of implementing them. In this article I’ve hopefully set forth a framework that you can use as a foundation for implementing them in your app.</p><p>An important thing to keep in mind when implementing push notifications is that users tend to be averse to them in general since they’re so oft-abused. <a href="https://www.gridnorthdesign.com/articles/annoying-notifications/">Overuse of push notifications is one of the main drivers behind app-uninstalls</a>, so don’t go overboard with them. Only implement push notifications that will add value for the user, and give the user the option to enable or disable them as they see fit. You should also consider whether using <a href="https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications#3544375">provisional authorization for trial notifications</a> might be a good idea.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=784965f078ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to deal with project.pbxproj in conjunction with git]]></title>
            <link>https://tonisucic.medium.com/how-to-deal-with-project-pbxproj-in-conjunction-with-git-15a76fa7b5c9?source=rss-4e0828fe41fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/15a76fa7b5c9</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Toni Sučić]]></dc:creator>
            <pubDate>Sun, 25 Feb 2018 11:25:25 GMT</pubDate>
            <atom:updated>2020-12-06T20:55:05.957Z</atom:updated>
            <content:encoded><![CDATA[<p>There’s a thing that has been driving me nuts ever since I started programming in Xcode, and that’s the infamous <em>project.pbxproj</em> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*CvVcF8Jv2o5oeaRZk8cxvg.png" /><figcaption>The project.pbxproj file contains metadata like settings, file references, configuration, and targeted platforms which Xcode uses to build your project.</figcaption></figure><p>Whenever you add, move, delete or rename a file / folder in Xcode (among other things), it will result in <em>project.pbxproj</em> being modified. Git, of course, recognizes this and adds it to the staging area. Now, the problem with this is that once you have enough changes in your staging area, and you want to split them up into logically segregated commits, it becomes extremely tedious because you have to manually go through the changes in the <em>project.pbxproj</em> file and find the ones that correspond with the files or folders you modified.</p><h3>Why is this a problem?</h3><h4>Scenario #1</h4><p>Imagine you add the files <em>Logger.swift</em> and <em>bg-image.png</em> to the project. You decide to commit the newly added <em>Logger.swift</em> &amp; <em>bg-image.png</em> files in two separate commits called “Add logging class” and “Add background image”, respectively. However, you forgot to add the corresponding changes in the <em>project.pbxproj</em> file, so now, if your colleague clones the project, he’ll get weird errors in Xcode because the files are there but the corresponding <em>project.pbxproj </em>info<em> </em>is not present in the repo he cloned, because it wasn’t included in the past two commits<em>.</em></p><h4>Scenario #2</h4><p>You have a bunch of changes in the staging area and decide to commit them. You’ve committed five commits, each being self contained and not dependent on subsequent commits… or so you think. All of a sudden you realize that while doing this, you forgot to commit the changes in <em>project.pbxproj, </em>so you decide to commit all the changes therein at once in one commit that succeeds the five foregoing commits. A couple of days later, you realize that you’ve inadvertently introduced a bug into the code which is so hard to find that you decide to revert to a past commit, but the commit you reverted to happens to be one of those commits that was committed without the corresponding <em>project.pbxproj</em> changes, so Xcode starts complaining. This happens because the commit you reverted to is dependent on a subsequent commit.</p><h4>Scenario #3</h4><p>You’re working on a project with someone else. There are four branches: <em>master</em>, <em>develop</em>, <em>onboarding</em>, <em>preferences</em>. You and your colleague are working on the <em>onboarding</em> and <em>preferences</em> branches, respectively. Every now and then, you synchronize the <em>develop</em> branch with your <em>onboarding</em> branch and in doing so, you merge your <em>project.pbxproj </em>file<em> </em>with the one on the <em>develop</em> branch. If you’re lucky, your colleague hasn’t done the same a few times already and you don’t get any merge conflicts. But, as is often the case, your colleague has made some changes to it and consequently, merge conflicts arise.</p><p>And that’s just the tip of the ice berg. There are many issues that could occur as a consequence of forgetting about <em>project.pbxproj</em> or managing it poorly.</p><h3>Is there a solution to this?</h3><p>There is one basic rule you can adhere to in order to decrease the likelihood of the aforementioned issues occurring.</p><blockquote>Commit newly added, moved, deleted or renamed files / folders ASAP — even if they’re empty</blockquote><p>If you do this right away as opposed to postponing it indefinitely, you can clear the changes in <em>project.pbxproj </em>before they become too numerous. By committing the changes in <em>project.pbxproj</em> with the corresponding file / folder changes the moment they appear, and doing so regularly and consistently, you won’t have to worry about their later becoming too numerous to handle. This deals with the issues raised in the first &amp; second scenarios.</p><p>The third scenario is more difficult to deal with. In this case, communication is crucial. Try to avoid merging a myriad of changes in your <em>project.pbxproj</em> file into the <em>develop</em> branch. Instead, merge small changes frequently, and make sure to inform anyone who’s working in their own branch about it so that they can run <em>git merge/rebase </em>in the branch they’re working on to synchronize their <em>project.pbxproj</em> file with the one on the <em>develop</em> branch that you just updated. Using a merge driver like <a href="https://simonwagner.github.io/mergepbx/">this one</a> is also a good idea as it eases the process of handling merges in <em>project.pbxproj</em>.</p><h4>tl;dr</h4><p>Make sure that there aren’t any uncommitted changes in <em>project.pbxproj </em>at all times. When running <em>git status</em>, you shouldn’t see <em>project.pbxproj </em>there, and if you do, deal with it immediately.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15a76fa7b5c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Type safe models with Firestore & Swift 4]]></title>
            <link>https://tonisucic.medium.com/type-safe-models-with-firestore-swift-4-1cd38a9afd49?source=rss-4e0828fe41fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cd38a9afd49</guid>
            <category><![CDATA[model]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Toni Sučić]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 19:02:00 GMT</pubDate>
            <atom:updated>2017-10-07T22:26:16.546Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Firestore is a new, alternative Firebase database which brings a number of benefits with it over the old Firebase database (henceforth referred to as rtdb), two of which are a more rigid structure and the possibility of querying multiple properties as opposed to just one. However, as of the current beta release, there is no built in way of mapping the documents in Firestore to custom objects in Swift. In this article, I will elaborate on a possible solution to this problem.</p><h4>Firestore structure</h4><p>Firestore is a document database and as such it is organized into documents, which are contained by collections. A document must be a part of a collection, and a collection cannot contain another collection. Documents may point to subcollections.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*48hjMRtmUJtykrKBTVzS6w.png" /><figcaption>Fig 1. Illustration of collections (rectangles) in Firestore and the documents (squares) they contain</figcaption></figure><p>A document is a simple key / value data structure which can be mapped to a Swift object rather easily. Firestore even supports a number of data types which are not supported by rtdb.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7lScTN0Xzk1OwsheAFHImQ.png" /><figcaption>Fig 2. Supported data types in Firestore</figcaption></figure><p>Most of the types in Firestore map to native Swift types (e.g. the date and time / timestamp type maps to the Date type in Swift.) The geographical point type is a simple struct containing values for latitude and longitude.</p><h4>Building a type safe layer on top of Firestore</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3128d81a3dc3b6ea2ba1bf02f221dde/href">https://medium.com/media/e3128d81a3dc3b6ea2ba1bf02f221dde/href</a></iframe><p>The book model in fig 3 has a property with which it can be referenced uniquely / unambiguously, namely the isbn property, rendering an auto-generated ID unnecessary for this particular model (more on that later.) It also has an enum property (format), an inline array (authors), and an optional property (award), with which the serializer works concordantly.</p><p>Each property is mutable (declared with var) so that you can fetch a result and mutate the properties in order to update the model. The model must then be persisted with .setModel(model:).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5631b535aa094e585022f5352ad69e55/href">https://medium.com/media/5631b535aa094e585022f5352ad69e55/href</a></iframe><p>Implementing the FirestoreModel protocol is fairly straightforward. The protocol only has two implementation requirements, and an optional serialized property which can be overridden if need be (by default it uses reflection to serialize every single property in the model).</p><p>In addition to making FirestoreBook conform to FirestoreModel, the Format-enum had to be changed in order for its RawValue to be of type String so that it could be stored in Firestore. Also, the enum had to conform to the custom protocol StringRawRepresentable because of a limitation in the Swift type system (Any cannot be cast to a protocol with an associated type, which RawRepresentable happens to be.)</p><p><strong>Note: Changing the isbn property (aka the documentID) will cause a new document to be created once it is persisted to the database with </strong><strong>.setModel(model:) while leaving a copy behind with the old isbn ID in the database. Make sure to delete the old document if you change the documentID.</strong></p><p>The failable initializer will fail if the document does not contain all the non-optional properties defined in the model.</p><p><strong>Note: A model must have at least one non-optional property in order for it to be retained by Firestore due to its default behavior whereby it automatically removes documents that have no children (properties.)</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fa0e6061fb697302e7b2fd52e0a0d3d/href">https://medium.com/media/8fa0e6061fb697302e7b2fd52e0a0d3d/href</a></iframe><p>Saving a model is also very easy. Simply use the .setModel(model:) extension method instead of the .setData(documentData:) method you would normally use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/081f2636f76644a26504250b184abdf8/href">https://medium.com/media/081f2636f76644a26504250b184abdf8/href</a></iframe><p>Likewise, fetching a model can be done just as easily by calling the .getModel(modelType:) method extension on the DocumentReference class instead of the .getDocument(callback:) method. The CollectionReference and Query classes each have a .getModels(modelType:) method extension for fetching multiple models.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3120092be12dca6eb978a0a0ec7840e6/href">https://medium.com/media/3120092be12dca6eb978a0a0ec7840e6/href</a></iframe><p>Models with auto-generated IDs are implemented a bit differently.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b98c11aff70401d31f1cf43968bf8445/href">https://medium.com/media/b98c11aff70401d31f1cf43968bf8445/href</a></iframe><p>They are also stored a bit differently. Since the documentID is not known before the model has been persisted, you pass nil to the documentID property upon initialization.</p><p>The process of fetching a model does not change and thus remains the same as in fig 6.</p><h3>Conclusion</h3><p>The implementation of the FirestoreModel-protocol and its associated code can be viewed <a href="https://gist.github.com/ts95/bcf5382e9b43f49ef96e3db2da1c4c0f">here</a>.</p><p><strong>Disclaimer:</strong> This implementation is a proof of concept and is not intended to be used in production. You are encouraged to modify it or develop it further to test its feasibility for your use case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cd38a9afd49" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>