user7345006 user7345006 -4 years ago 133
Swift Question

Sqlite file empty but data can fetch view in simulator using core data

I try implement core data in my app. I can insert data and fetch data in emulator. I try print the data and the data is exist. But when i open sqlite file, the data is empty. It only have table and attribute but not have data. I using swift 3.

This is JobOrder.swift

import Foundation
import CoreData

public class JobOrder: NSManagedObject {

}


This is JobOrder+CoreDataProperties.swift

import Foundation
import CoreData

extension JobOrder {

@nonobjc public class func fetchRequest() -> NSFetchRequest<JobOrder> {
return NSFetchRequest<JobOrder>(entityName: "JobOrder");
}

@NSManaged public var body: String?
@NSManaged public var id: Int64
@NSManaged public var orderID: Int64

}


This the way i insert data

func seedPerson(){

let entity = NSEntityDescription.insertNewObject(forEntityName: "JobOrder", into: moc)

entity.setValue("name", forKey: "body")

do{
try moc.save()
}catch{
fatalError("failed: \(error)")
}
}


This the way i fetch data

func fetch(){
let personFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "JobOrder")
do{
let fetchPerson = try moc.fetch(personFetch)
print("bodyName \((fetchPerson.first! as AnyObject).body!))")
}
catch {
}
}


For insert and fetch, it working but when open my sqlite file the data not exist.

This is my file DataController

import UIKit
import CoreData
class DataController: NSObject {
var managedObjectContext: NSManagedObjectContext
override init() {
// This resource is the same name as your xcdatamodeld contained in your project.
guard let modelURL = Bundle.main.url(forResource: "modeldb", withExtension:"momd") else {
fatalError("Error loading model from bundle")
}
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
guard let mom = NSManagedObjectModel(contentsOf: modelURL) else {
fatalError("Error initializing mom from: \(modelURL)")
}
let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = psc

let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let docURL = urls[urls.endIndex-1]

// let storeURL = docURL.URLByAppendingPathComponent("modeldb.sqlite")
let storeURL = NSURL(string:"modeldb.sqlite", relativeTo:docURL)
do {
try psc.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL as URL?, options: nil)
} catch {
fatalError("Error migrating store: \(error)")
}
}
}


This is my file modeldb.xcdatamodelid

i also got error in log

CoreData: warning: Unable to load class named 'JobOrder' for entity 'JobOrder'. Class not found, using default NSManagedObject instead.

Answer Source

Further discussion resulted in a question about how to turn off WAL mode in CoreData using Swift 3.0. So I'm adding the necessary code here as an answer so that others can benefit from it too:

let dict = [NSSQLitePragmasOption: ["journal_mode":"DELETE"]]
do {
    try coordinator.addPersistentStore(ofType:NSSQLiteStoreType, configurationName:nil, at:url, options:dict)
} catch {
    // Report error
    abort()
}

In the above, coordinator is an NSPersistentStoreCoordinator instance and url is a URL instance pointing to the location of the persistent store.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download