中间件中的闭包为零

问题描述 投票:0回答:1

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

我想跟踪用户的位置以及每当有新的位置更新时。因此,我正在使用中间件。日志记录和Firebase身份验证中间件正在运行,但是位置跟踪不起作用。因为newLocation为nil,但是,如果我将Closure 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)
    }
}

我正在努力通过SwiftUI实现类似fluxlike的实现。我想跟踪用户的位置以及何时有新的位置更新。因此,我正在使用中间件。日志记录和...

swift core-location middleware swiftui
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.