Skip to main content

SwiftUI - publishing changes from background threads is not allowed

The purple warning notification of annoyance recently came my way!  I received the following warning:

Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

 

What was happening?

The functionality of the code uses URLSession to get a user's name via API and the display is simple piece that initially message of Hello {user}.

RealmSwift 10.1 framework not found realm-sync

Updating Realm and RealmSwift from 5.5.x to 10.0.0 brought in the following error 

RealmSwift 10.1: framework not found realm-sync

The greatest clue I came across to solving this issue was on the Realm GitHub issues page for realm-sync error.  However, updating Cocopods wasn't the only step required to correct this issue.  The steps I took were:

SwiftUI String to Binding<String> error

After creating a new view with a @Binding string as follows

struct EditRival: View {

  @Environment(\.presentationMode) var presentationMode
  @EnvironmentObject var realmPerson: RealmPersons
  @Binding var uid: String

 

The previews struct had an error with the var previews

Validating email in Swift or SwiftUI

I'm currently working on a project that requires login, register, forget password functionality in SwiftUI.  As I'm developing the code, I came across a great resource for validating an email address with Regex:

http://emailregex.com/

Realm object with NSPredicate

What to do when you want to filter a Realm object, using NSPredicate in SwiftUI or in Swift.  

Applying the filter to a simple condition in one line

let exists = realm.objects(ExampleObj.self).filter(NSPredicate(format: "uid = %ld", uid))

Creating a variable predicate that will be injected as a condition

CDN error on pod update

Today while in Terminal, I ran a regular script pod update.  However, this time I received the following error

[!] CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/d/a/2/Alamofire/5.1.0/Alamofire.podspec.json Response: Timeout was reached

I read on GitHub that it could be a cache issue, so I ran:

Creating a new SwiftUI project

Creating a new SwiftUI project is a quick process.  There are already thousands of sites outlining how to do these steps.  Rather than repeat this steps at the top of each article, we will refer to this article to get you across the line if you don't know or need a refresher.

Step 1

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project.

SwiftUI adding a background image with a gradient tint

I'm going to take you on a journey about adding a gradient tint to a background image.  The journey will go through text modifiers with regular and semibold font weights, and font colour.  Using an image from Unsplash.  Adding a black tint, then transform it to a gradient using a colour array.  If you get lost along the journey, the final result is available on GitHub.

Gradient tint over a background image

How to apply a gradient tint over a background image?

Set the background image

The first step is to set the background image.  I wanted the image to cover the entire device, so scale aspect fill will be required.

self.backgroundImage = UIImageView(image: UIImage(named: "bgkMain"))
self.backgroundImage.contentMode = .scaleAspectFill
self.view.insertSubview(self.backgroundImage, at: 0)

 

Subscribe to Swift 5