Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

Swift core data does not store data permanently

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...