简体   繁体   中英

Swift2.0 CoreData issue on NSFetchRequest

This is a code snippet of me trying to fetch data from coredata. I tried to fetch data and checked using breakpoint and there was NO data fethed! The code is in swift2. Any suggestions would be appreciated. I defined a struct constants where I defined all the constants. gate is a string type defined in nsmanagedobject class. The snippet is a part of viewDidLoad() method.

let context: NSManagedObjectContext? = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext!
let TPTodayFetchRequest = NSFetchRequest(entityName: Constants.CoreDataEntities.TPTodayCoreDataEntity)

do {
let patroDaily = try context!.executeFetchRequest(TPTodayFetchRequest) as! [TPToday]
for patroEntity in patroDaily {
print(patroEntity.gate)

}
}
catch { print("error") }

Except for cumbersome variable naming, and unfortunate indentation, your code is fine and should work.

You will have to investigate if you have indeed saved the data you are expecting to find. For example, you could have the program log the documents directory URL after which you could examine the sqlite file with the sqlite3 command line tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM