                             self.texture = texture.texture
                         }
                         self.context.flush()
-                        
-                        if writer.status == .writing {
-                            writer.finishWriting {
-                                self.processNext()
-                            }
-                        } else {
-                            self.processNext()
-                        }
-                    }
+                        self.writeNextFrame()
+                     }
                 })
                 commandBuffer?.commit()
             } catch {

        }
    }
    
    func processNext() {
        guard let reader = self.reader,
            let writer = self.writer,
            let output = self.output else {
                return
        }

            }
        }
    }
    
    private func writeNextFrame() {
        guard let pixelBufferPool = adaptor?.pixelBufferPool else {
            print("Pixel buffer asset writer input did not have a pixel buffer pool available; cannot retrieve frame")
            return
        }
        
        var newPixelBuffer: CVPixelBuffer? = nil
        let status  = CVPixelBufferPoolCreatePixelBuffer(nil, pixelBufferPool, &newPixelBuffer)
        guard let pixelBuffer = newPixelBuffer, status == kCVReturnSuccess else {
            print("Could not get pixel buffer from asset writer input; dropping frame...")
            return
        }
        
        self.processNext()
    }
}

extension SampleViewController4 : MTKViewDelegate {
