DEV Community

Lankinen
Lankinen

Posted on

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

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 SwiftUI

struct EmojiArtDocumentView: View {
  @ObservedObject var document: EmojiArtDocument

  var body: some View { }
}

ModelView

import SwiftUI

class 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

@RealLankinen

Originally published here

Top comments (0)