ArKit entire scene is drifting away

I'm using ArKit with Metal in my ios app without ARSCNView. Most of times it works fine. However sometimes the whole scene is drifting away (especially first few seconds), then it sort of comes back but far from the original place. I don't see this effect in other model viewers - Unity based or WebAR based. Surely they use ArKit under the hood (not all but most of them do).

Here is a snippet of my code:

private let session = ARSession()

func initialize() {
    let configuration = ARWorldTrackingConfiguration()
    configuration.maximumNumberOfTrackedImages = 10
    configuration.planeDetection = [.horizontal, .vertical]
    configuration.automaticImageScaleEstimationEnabled = true
    configuration.isLightEstimationEnabled = true

    session.delegate = self
    session.run(configuration)
}


func getCameraViewMat(...) -> simd_float4x4 {
    //...
    return session.currentFrame.camera.viewMatrix(for: .portrait)
}

func createAnchor(...) -> Int {
    // ...
    anchors[id] = ARAnchor(transform: mat)
    session.add(anchor: anchors[id]!)
    return id
}

func getAnchorTransform(...) -> simd_float4x4 {
    //...
    return anchors[id]!.transform
}

func onUpdate(...) {
    // draw session.currentFrame.rawFeaturePoints!.points
    // draw all ARPlaneAnchor
}

func session(_ session: ARSession, cameraDidChangeTrackingState camera: ARCamera) {
    print("TRACKING STATE CHANGED: \(camera.trackingState)")
}

I can see it's not just anchors problem - everything moves including the point cloud. The tracking state is normal and changes to limited only after the drift occurred which is too late.

What can I do to prevent the drift?

ArKit entire scene is drifting away
 
 
Q