Skip to content

Instantly share code, notes, and snippets.

@wingovers
wingovers / PotentialApproach.md
Last active April 22, 2024 21:22
Nesting ObservableObjects / Observing Repositories for SwiftUI

Nesting ObservableObjects / Observing Repositories for SwiftUI View Models

Situation

A simple app has a few repository objects that own the in-memory instance of specific models and organize persistence appropriately. Its instantiated by a DI container and passed to view models.

Observation

SwiftUI views do not diff and update upon changes within the repo's model. The view model does not fire its objectWillChange publisher – even though the repository is marked as @Published.

@LeeKahSeng
LeeKahSeng / Decode-Dynamic-Keys-JSON-Final.swift
Last active June 4, 2023 14:04
Decode and Flatten JSON with Dynamic Keys Using Swift Decodable (https://swiftsenpai.com/swift/decode-dynamic-keys-json/)
import Foundation
let jsonString = """
{
"S001": {
"firstName": "Tony",
"lastName": "Stark"
},
"S002": {
"firstName": "Peter",
@dry1lud
dry1lud / combine-retry.md
Last active November 27, 2023 10:07
Retry operation in Swift/Combine.

There is a function .retry() in Combine that helps to retry a request. Although it is not enough just to call retry() to achieve retring logic. The retry() function does not do another request but it re-subscribes only to a publisher. To make another request the tryCatch() might be used. In the code below if the first call fails there are three attempts to retry (retry(3)):

import UIKit
import Combine
import PlaygroundSupport

enum CustomNetworkingError: Error {
    case invalidServerResponse
}