Swift核心数据不会永久存储数据 [英] Swift core data does not store data permanently

查看:489
本文介绍了Swift核心数据不会永久存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为朋友的实体..属性是名称和年龄。我已经实现桥接,因为NSManagedObject类的朋友有.h和.m文件。我的代码存储数据:

  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)

我的代码检索数据

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

所以当我检索,它给出结果与data..but它不永久存储...如果我退出并运行应用程序,然后以前的数据已经走了。任何人告诉我什么是我的代码的问题...



这里是示例项目 https://github.com/rnystrom/Swift-CoreData 我从Github ...这个项目也有同样的问题..

解决方案

保存方法的返回值是什么是true还是false?你可以显示下面这行的输出,如果你把它放在正确的行的保存方法之前:

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


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..

解决方案

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)")

这篇关于Swift核心数据不会永久存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆