I want the user to be able to export all of their "DiaryItem" CoreData records into a CSV. Here's My code: 

**DiaryItem.swift**

```
import Foundation
import CoreData

public class DiaryItem:NSManagedObject,Identifiable {
   @NSManaged public var createdAt:Date?
   @NSManaged public var title:String?
}

extension DiaryItem {
    static func getAllDiaryItems() -> NSFetchRequest<DiaryItem> {
        let request:NSFetchRequest<DiaryItem> = DiaryItem.fetchRequest() as! NSFetchRequest<DiaryItem>

        let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)

        request.sortDescriptors = [sortDescriptor]

        return request
     }
}

```

**SettingsView.swift** 

```
    import SwiftUI
    import CoreData

    struct SettingsView: View {
    @FetchRequest(entity: DiaryItem.entity(), sortDescriptors: [
        NSSortDescriptor(keyPath: \DiaryItem.createdAt, ascending: false),
    ]) var diaryItems : FetchedResults<DiaryItem>

    var body: some View {
        VStack(alignment: .leading) {
            Button(action: {
                  self.exportDatabase()
               }) {
                  Text("Export")
                  }
        }
    }

    func exportDatabase() {
        let exportString = createExportString()
        saveAndExport(exportString: exportString)
    }

    func saveAndExport(exportString: String) {
        let exportFilePath = NSTemporaryDirectory() + "itemlist.csv"
        let exportFileURL = NSURL(fileURLWithPath: exportFilePath)
        FileManager.default.createFile(atPath: exportFilePath, contents: NSData() as Data, attributes: nil)
        //var fileHandleError: NSError? = nil
        var fileHandle: FileHandle? = nil
        do {
            fileHandle = try FileHandle(forWritingTo: exportFileURL as URL)
        } catch {
            print("Error with fileHandle")
        }

       if fileHandle != nil {
            fileHandle!.seekToEndOfFile()
            let csvData = exportString.data(using: String.Encoding.utf8, allowLossyConversion: false)
        fileHandle!.write(csvData!)

        fileHandle!.closeFile()

        let firstActivityItem = NSURL(fileURLWithPath: exportFilePath)
        let activityViewController : UIActivityViewController = UIActivityViewController(
            activityItems: [firstActivityItem], applicationActivities: nil)

        activityViewController.excludedActivityTypes = [
            UIActivity.ActivityType.assignToContact,
            UIActivity.ActivityType.saveToCameraRoll,
            UIActivity.ActivityType.postToFlickr,
            UIActivity.ActivityType.postToVimeo,
            UIActivity.ActivityType.postToTencentWeibo
        ]
    }
}

func createExportString() -> String {
    var createdAt: Date?
    var title: String?

    var export: String = NSLocalizedString("createdAt, title \n", comment: "")
    for diaryItem in self.diaryItems {

            let createdAtvar = DiaryItem.value(forKey: "createdAt") as! Date?
            let titlevar = DiaryItem.value(forKey: "title") as! String?
            let createdAtString = createdAtvar
            let titleString = titlevar

            export += "\(titleString!),\(createdAtString!)\n"

    }
    print("This is what the app will export: \(export)")
    return export
    }
   }
}

```

Unfortunately when I run this in the simulator it crashes with the error: 

> 
> [SwiftUI] Context in environment is not connected to a persistent store coordinator: 
> 
> 
> 

I've been banging my head against it for hours any help would be hugely appreciated!!