问题:将Json数据保存到Core Data [英] Issue: Saving Json data to Core Data

查看:358
本文介绍了问题:将Json数据保存到Core Data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习如何从博客中读取数据并将其保存到核心数据,但保存不能按预期工作。有4个博客条目,我希望在核心数据中有4个不同的条目。请参阅下面的代码,让我知道我在哪里错了:

I am trying to learn how to read data from a blog and save it to core data but the save is not working as intended. There are 4 blog entries and I expect to have 4 different entries in core data. Please see the code below and let me know where i went wrong:

 let task = session.dataTaskWithURL(url!, completionHandler:{(data , response, error) -> Void in
        if (error != nil){
            println(error)
        }else{
            var jsonResult:NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as NSDictionary
            var managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
            let newBlog = NSEntityDescription.insertNewObjectForEntityForName("BlogDetails",inManagedObjectContext:managedObjectContext) as NSManagedObject
            var dateFormater = NSDateFormatter()
            dateFormater.dateFormat = "yyyy-MM-dd HH:mm:ss" //"yyyy-MM-dd"
            var readRequest = NSFetchRequest(entityName: "BlogDetails")
            for var i = 0; i < ((jsonResult["items"] as? NSArray)?.count)!; i++ {
                var item = jsonResult["items"]![i] as NSDictionary
                var blogAuthorDirectory = item["author"]! as NSDictionary
                var blogAuthor = blogAuthorDirectory["displayName"] as NSString
                var blogAuthorImageDirectory = blogAuthorDirectory["image"] as NSDictionary
                // concatenate String
                var blogAuthorImage =  blogAuthorImageDirectory["url"] as NSString
                var blogAuthorImageUrl = ("https:" + blogAuthorImage)
                var title = item["title"] as String
                // convert date from String
                var publishedDate:NSDate = dateFormater.dateFromString(stringTmp as NSString)!
                // read content
                var content = item["content"] as? NSString
                // Write it to core data
                newBlog.setValue(blogAuthorImageUrl, forKey: "image")
                newBlog.setValue(blogAuthor, forKey: "author")
                newBlog.setValue(title, forKey: "title")
                newBlog.setValue(publishedDate, forKey: "publisheddate")
                managedObjectContext.save(nil)
                var results = managedObjectContext.executeFetchRequest(readRequest, error: nil)
                println(results)
            }
        }
    })
    task.resume()

以下是最后一次迭代结果中的条目:
1.它只有3个字典计数,前2个计数中的值具有所有项目零。如何生成?
2.每次迭代,它会覆盖最后一次计数的值,不会附加它。

following are the entries in result in the last iteration: 1. It only has 3 dictionary counts out of which values in first 2 count has all items as nil. how is that being generated? 2. With every iteration, it overwrites value in last count and doesn't append it.

感谢您的帮助。

推荐答案

如果要将对象附加到CoreData,您需要执行 insertIntoManagedObjectContext managedContext.save(nil)方法。

If you want to append objects to your CoreData, you need to do insertIntoManagedObjectContext before you call the managedContext.save(nil) method.

但是,

let newBlog = NSEntityDescription.insertNewObjectForEntityForName("BlogDetails",inManagedObjectContext:managedObjectContext) as NSManagedObject

在您的for循环之外声明,因此可能每次迭代后都没有创建新博客。

is declared outside of your for loop, so probably no new blog created after each iteration.

这篇关于问题:将Json数据保存到Core Data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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