CS193p Notes - Lecture 7: Multithreading EmojiArt
Tags: ios, swift
Published: 2020-07-26 16:38:09

### 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/](https://developer.apple.com/design/)

### Multithreaded Programming ([6:15](https://youtu.be/tmx-OwkBWxA?t=375))

- 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.)

```swift
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](https://youtu.be/tmx-OwkBWxA?t=1238))

View

```swift
import SwiftUI

struct EmojiArtDocumentView: View {
  @ObservedObject var document: EmojiArtDocument

  var body: some View { }
}
```

ModelView

```swift
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.

```swift
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](https://twitter.com/reallankinen)

- [Notes 1](https://dev.to/lankinen/cs193p-notes-lecture-1-course-logistics-and-introduction-to-swiftui-44ap)
- [Notes 2](https://dev.to/lankinen/cs193p-notes-lecture-2-mvvm-and-the-swift-type-system-406f)
- [Notes 3](https://dev.to/lankinen/cs193p-notes-lecture-3-reactive-ui-protocols-layout-5g8a)
- [Notes 4](https://dev.to/lankinen/cs193p-notes-lecture-4-grid-enum-optionals-4noj)
- [Notes 5](https://dev.to/lankinen/cs193p-notes-lecture-5-viewbuilder-shape-viewmodifier-fgh)
- [Notes 6](https://dev.to/lankinen/cs193p-notes-lecture-6-animation-1a8f)
- [**Notes 7**](https://dev.to/lankinen/cs193p-notes-lecture-7-multithreading-emojiart-2ak3)
- [Notes 8](https://dev.to/lankinen/cs193p-notes-lecture-8-gestures-json-1f3m)
- [Notes 9](https://dev.to/lankinen/cs193p-notes-lecture-9-data-flow-26e)
- [Notes 10](https://dev.to/lankinen/cs193p-notes-lecture-10-navigation-textfield-3ei9)
- [Notes 11](https://dev.to/lankinen/cs193p-notes-lecture-11-picker-2hgh)
- [Notes 12](https://dev.to/lankinen/cs193p-notes-lecture-12-core-data-eci)
- [Notes 13](https://dev.to/lankinen/cs193p-notes-lecture-13-persistence-2jeh)
- [Notes 14](https://dev.to/lankinen/cs193p-notes-lecture-14-uikit-integration-1p7m)

[Originally published here](https://www.notion.so/lankinen/CS193p-2020-iOS-development-course-from-Stanford-81c094238e9d471bbf059ea8d83b3e15)