CS193p Notes — Lecture 7: Multithreading EmojiArt

Colors and Images

  • UIColor allows manipulating colors
  • Image is a View that takes name (String) of the image in Assets.xcassets folder
  • Image(uiImage: self.document.backgoundImage) and backgroundImage: UIImage?
  • It’s also possible to get system images
  • See the different system images by downloading an app from here https://developer.apple.com/design/

Multithreaded Programming (6:15)

  • UI code is using main queue
  • Grand Central Dispatch (GCD) is where queue stuff is called
  • Two fundamental tasks:
  1. getting access to a queue
  2. plopping a block of code on a queue
  • DispatchQueue.main is the queue where all UI code must be posted
  • DispatchQueue.global(qos: QoS) a non-UI queue with a certain quality of service
  • qos is one of the following
  • .userInterface do this fast because UI depends on it
  • .userInitiated the user just asked to do this, so do it now
  • .utility needs to happen but user didn't asked for it
  • .background maintenance tasks (cleanups, etc.)
let queue = DispatchQueue.main or Dispatch.global(gos: )
queue.async { }
  • Dispatch.global(gos: ) not used very much because there are higher level APIs for specific use cases(e.g. URLSession)

EmojiArt Demo (20:38)

View

import SwiftUIstruct EmojiArtDocumentView: View {
@ObservedObject var document: EmojiArtDocument
var body: some View { }
}

ModelView

import SwiftUIclass EmojiArtDocument: ObservableObject {}
  • When looping through strings, it doesn’t make sense to make all strings in the app identifiable but just those. There is parameter id in ForEach that can be set to \.self to use string's own value as identifier.
struct EmojiArt {
var backgroundURL: URL?
var emojis: [Emoji]()
struct Emoji: Identifiable {
let text: String
var x: Int
var y: Int
var size: Int
let id: Int
fileprivate init(text: String, x: Int, y: Int, size: Int, id: Int) {
self.text = text
self.x = x
self.y = y
self.size = size
self.id = id
}
}

private var uniqueEmojiId = 0
mutating func addEmoji(_ text: String, x: Int, y: Int, size: Int) {
uniqueEmojiId += 1
emojis.append(Emoij(text: text, x: x, y: y, size: size, id: uniqueEmojiId))
}
}
  • privatefile makes init only accessible from that file and that way other files can't create emojis
  • In this case we wanted to do this because there is unique way to create id for each emoji and we don’t want any other file to create emojis with it’s own id system
  • Model should be private in ViewModel because it’s job is to offer intent(s) which View will use to access Model

https://twitter.com/reallankinen

Lecture 1
Lecture 2
Lecture 3
Lecture 4
Lecture 5
Lecture 6
Lecture 7
Lecture 8
Lecture 9
Lecture 10
Lecture 11
Lecture 12
Lecture 13
Lecture 14

Originally published: https://www.notion.so/lankinen/Lecture-7-Multithreading-EmojiArt-f84d41f0006b459ab3bd217fde989e1d

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store