在中间件中,闭包为零

我正在努力通过SwiftUI实现类似fluxlike的实现。

我想跟踪用户的位置以及每当有新的位置更新时。因此,我正在使用中间件。日志记录和Firebase身份验证中间件正在运行,但是位置跟踪不起作用。因为newLocation为nil,但是,如果我将关闭位置location.newLocation {}放在MiddlewareProvider的init中,它将发布位置更改。为什么在中间件中为零?

我包括了firebase和日志记录中间件,以显示可用的中间件:

class VMiddlewareProvider: MiddlewareProvider {

    private var location: TrackLocation

    init() {
        self.location = TrackLocation()

    }

    func provideMiddleware() -> [Middleware<FluxState>] {
        return [
            loggingMiddleware(),locationTrackingMiddleware(),firebaseMiddleware()
        ]
    }

    private func loggingMiddleware() -> Middleware<FluxState> {
        let loggingMiddleware: Middleware<AppState> = { dispatch,getState in
            return { next in
                return { action in
                    #if DEBUG
                    let name = __dispatch_queue_get_label(nil)
                    let queueName = String(cString: name,encoding: .utf8)
                    print("#action: \(String(reflecting: type(of: action))) on queue: \(queueName ?? "??")")
                    #endif
                    return next(action)
                }
            }
        }
        return loggingMiddleware
    }

    private func locationTrackingMiddleware() -> Middleware<FluxState> {
        let middleware: Middleware<AppState> = { dispatch,getState in
            return { next in
                return { action in
                    switch action as? Locationaction {
                    case .trackLocation:

                        self.location.newLocation = { result in
                            /// never gets called because newLocation is nil
                            print(result)
                        }
                        return next(action)

                    default:
                        return next(action)
                    }
                    return next(action)
                }
            }
        }
        return middleware
    }

    private func firebaseMiddleware() -> Middleware<FluxState> {
        let firebaseMiddleware: Middleware<AppState> = { dispatch,getState in
            return { next in
                return { action in
                    switch action {
                    case let action as accountactions.Authenticate:
                        let handle = Auth.auth().addStateDidchangelistener {  (auth,user) in
                            if let user = user {
                                return next(accountactions.Authentificationaction(isLoggedIn: true,userUID: user.uid))
                            }  else {
                                return next(accountactions.Authentificationaction(isLoggedIn: false,userUID: nil))
                            }
                        }
                    default:
                        return next(action)
                    }
                }
            }
        }
        return firebaseMiddleware
    }
}

TrackLocation类如下:

class TrackLocation: NSObject {
    let locationmanager = CLLocationmanager()
    var newLocation: ((CLLocation)->())?


    override init() {
        super.init()
        askForPermission()
        locationmanager.delegate = self
        locationmanager.startUpdatingLocation()
        locationmanager.startMonitoringSignificantLocationChanges()
        locationmanager.startMonitoringVisits()
    }

    func checkAuthorisation() {
        askForPermission()
    }

    func askForPermission() {
        locationmanager.requestWhenInUseAuthorization()

    }

}

extension TrackLocation: CLLocationmanagerDelegate {
    func locationmanager(_ manager: CLLocationmanager,didVisit visit: CLVisit) {
        if let location = manager.location {

            newLocation?(location)

        }
    }

    func locationmanager(_ manager: CLLocationmanager,didUpdateLocations locations: [CLLocation]) {
        newLocation?(locations.last!)

    }

    func locationmanager(_ manager: CLLocationmanager,didChangeAuthorization status: CLAuthorizationStatus) {
        print(status)
    }
}
patrxx 回答:在中间件中,闭包为零

有两个可能的错误: 1。 该函数实际上并未调用self.location.newLocation。请小心并调试它。

private func locationTrackingMiddleware() -> Middleware<FluxState> {
        let middleware: Middleware<AppState> = { dispatch,getState in
            return { next in
                return { action in
                    switch action as? LocationAction {
                    case .trackLocation:
//// this code isn't called! please,check
                        self.location.newLocation = { result in
                            /// never gets called because newLocation is nil
                            print(result)
                        }
                        return next(action)

                    default:
                        return next(action)
                    }
                    return next(action)
                }
            }
        }
        return middleware
    }
  1. 在代码中的某些地方,您擦除了此变量:self.location.newLocation = nil

顺便说一句,您可以在某处删除变量location

本文链接:https://www.f2er.com/3145833.html

大家都在问