pointfreeco / episode-code-samples

💾 Point-Free episode code.
https://www.pointfree.co
MIT License
939 stars 289 forks source link

Thread 1: Simultaneous accesses to 0x600003a707e0, but modification requires exclusive access #65

Closed bolivarbryan closed 3 years ago

bolivarbryan commented 3 years ago

Following Episode 102 I've tried to replicate .concatenate operator but once I try to check a second todo item app crashes and the issue is directly related with cancel.

Screen Shot 2020-08-08 at 8 12 04 PM

here is my reducer:


let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
    todoReducer.forEach(state: \AppState.todos,
                        action: /AppAction.todo(index:action:),
                        environment: { _ in TodoEnvironment() }
    ),
    Reducer { state, action, environment in
        switch action {
        case .addButtonTapped:
            state.todos.insert(Todo(id: environment.uuid()), at: 0)
            return .none
        case .todo(index: _, action: .checkboxTapped):
            struct CancelDelayId: Hashable { }

            return .concatenate(
              Effect.cancel(id: "todo completion effect"),
              Effect(value: .todoDelayCompleted)
                .delay(for: 1, scheduler: DispatchQueue.main)
                .eraseToEffect()
                .cancellable(id: "todo completion effect")
            )
        case .todo(index: let index, action: let action):
            return .none
        case .todoDelayCompleted:
            state.todos = state.todos
              .enumerated()
              .sorted(by: { lhs, rhs in
                (rhs.element.isComplete && !lhs.element.isComplete) || lhs.offset < rhs.offset
              })
              .map(\.element)
            return .none
        }

    }
)
mbrandonw commented 3 years ago

Hey @bolivarbryan, thanks for the report. Are you running Xcode 12 beta 3 by any chance? Something in Combine changed in that release causing this problem, but it appears to be fixed in beta 4.

bolivarbryan commented 3 years ago

Screen Shot 2020-08-10 at 2 17 09 AM

That's right. Going to check Immediately.

Thanks a lot

bolivarbryan commented 3 years ago

It works!, closing issue. thanks @mbrandonw