Swift core data does not store data permanently

旧街凉风 提交于 2020-01-17 17:12:05

问题


I have an Entity called "Friend".. Attributes are name and age..here I have implemented bridging because NSManagedObject class for Friend has .h and .m file. My code to store data:

var err: NSError?
var delegate:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var friend:Friend = Friend(entity: NSEntityDescription.entityForName("Friend", inManagedObjectContext: delegate.managedObjectContext), insertIntoManagedObjectContext: delegate.managedObjectContext)
friend.friendName = "Mani"
friend.friendAge = "23"
delegate.managedObjectContext.save(&err)

My code to retrieve data

var result: Array = delegate.managedObjectContext.executeFetchRequest(NSFetchRequest(entityName: "Friend"), error: &err)
println("reslut \(result)")

So when I retrieve, it gives result with data..but its not stored permanently..if I quit and run the app then previous data has gone..can anyone tell me what is the issue with my code...

Here is the sample project https://github.com/rnystrom/Swift-CoreData I got it from Github..this project also has same issue..


回答1:


What is the return value of the save method, is it true or false? Can you show what the output is of the following line, if you place this right before the line with the save method:

println("delegate: \(delegate); MOC: \(delegate.managedObjectContext)")


来源:https://stackoverflow.com/questions/24545845/swift-core-data-does-not-store-data-permanently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!