Coder Social home page Coder Social logo

Comments (5)

luizmb avatar luizmb commented on May 27, 2024

For some reason I missed that question, I'm sorry taking so long to reply.

The strict version of me would tell you that no side-effects should happen in the Reducer (even print or specially crashing the app). For debug purposes, however, that could be useful sometimes. But if you're willing to keep that assertionFailure in there, even given that assertionFailure only happens in Debug builds, it'd be more correct in the middleware, so I would recommend Middleware' afterReducer closure for that, at that point your new state is already reduced, you still have the original Action that originated the inconsistent state and you can even have the old state to compare if you want.

Another option, that I like much more, is trigger an "error" action from the middleware in that case, and another middleware called "DebugMiddleware" would be responsible for crashing, or logging to console or whatever crash breadcrumb backend you do have. This is very useful to track problems in production, because all these "assertionFailure" should give you very good hints when understanding other crashes or problems with Release builds. In my company's apps we have QA menus where you can crash the app and provide a text with some context, all of this goes to Crashlytics as breadcrumbs and all the possible "assertionFailures" on the way are also logged as breadcrumbs in there, so when QA crew finds something weird in the app, they crash the app on purpose and provide a text explaining their findings... Then they create a Jira ticket pointing to the crash on Crashlytics and we have a lot of information in there to debug.

By the way, every action in our apps is also logged to Crashlytics as "Records" (Crashlytics.crashlytics().log(Action as String)) and the last known state is logger as key/values to the "Keys" section (Crashlytics.crashlytics().setCustomValue(value, forKey: key)). I do this in a special Crashlytics middleware, and in a background queue.

Hope it helps.

from swiftrex.

luizmb avatar luizmb commented on May 27, 2024
override public func handle(action: AppAction, from dispatcher: ActionSource, afterReducer: inout AfterReducer) {
    let recorder = crashRecorder() // Dependency Injection for Crashlytics
    // log breadcrumbs
    serialBgQueue.async {
        recorder.log("🕹 \(action)")
    }

    // Saves the latest state. This will be overwritten continuoisly and the latest state is sent to firebase in case of a crash
    afterReducer = .do { [weak self] in
        guard let self = self else { return }

        let state = self.getState()
        self.serialBgQueue.async {
            let json = state.toDictionary(encoder: self.encoder())
            json.forEach { (key, value) in
                recorder.set(value: value, for: "state.\(key)")
            }
        }
    }
}

from swiftrex.

luizmb avatar luizmb commented on May 27, 2024

missed this one

extension Encodable {
    public func toDictionary(encoder: JSONEncoder) -> [String: Any] {
        guard
            let data = try? encoder.encode(self),
            let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
        else { return [:] }

        return json
    }
}

from swiftrex.

luizmb avatar luizmb commented on May 27, 2024

Moving question to Slack
https://swiftrex.slack.com/archives/C01M6CT2TJ9/p1612718006001200

from swiftrex.

luizmb avatar luizmb commented on May 27, 2024

Invitation link:
https://join.slack.com/t/swiftrex/shared_invite/zt-m9kk7xio-imhcsF2eHJqb~YP_ls6U9g

from swiftrex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.