【问题标题】:Save a list of data into core data iOS Swift将数据列表保存到核心数据 iOS Swift
【发布时间】:2016-01-08 02:04:29
【问题描述】:

我正在使用 for each 循环将数据列表保存到核心数据中。但是,它只帮助我将for循环中的最后一个数据保存到核心数据中,这是什么问题?我无法将所有数据保存到核心数据中。下面是我正在处理的代码...

let appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context : NSManagedObjectContext = appDelegate.managedObjectContext
    let category = NSEntityDescription.insertNewObjectForEntityForName("Category", inManagedObjectContext:context)


RestApiManager.sharedInstance.makeGetRequest("testingserver", onCompletion: {json in
    for result in json["record"].arrayValue
    {
        print(result)
        let id = result["mc_termid"].stringValue
        let name = result["mc_name"].stringValue
        let parent = result["mc_parent"].stringValue
        let color = result["mc_color"].stringValue
        let update = result["mc_updated"].stringValue
        let termName = result["mc_termname"].stringValue
        let status = result["mc_status"].stringValue

        category.setValue(id, forKey: "mc_termid")
        category.setValue(name, forKey: "mc_name")
        category.setValue(parent, forKey: "mc_parent")
        category.setValue(color, forKey: "mc_color")
        category.setValue(update, forKey: "mc_updated")
        category.setValue(termName, forKey: "mc_termname")
        category.setValue(status, forKey: "mc_status")
    }
    do
    {
        try context.save()
        print("Category save to local database successfully.")
    }
    catch
    {

    }
})

【问题讨论】:

    标签: ios swift core-data


    【解决方案1】:

    每次迭代数组中的一个元素时,都需要插入一个托管对象。
    所以将类别声明放入迭代块中。

    let appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context : NSManagedObjectContext = appDelegate.managedObjectContext
    
    
    RestApiManager.sharedInstance.makeGetRequest("testingserver", onCompletion: {json in
        for result in json["record"].arrayValue
        {
            let category = NSEntityDescription.insertNewObjectForEntityForName("Category", inManagedObjectContext:context)
    
            print(result)
            let id = result["mc_termid"].stringValue
            let name = result["mc_name"].stringValue
            let parent = result["mc_parent"].stringValue
            let color = result["mc_color"].stringValue
            let update = result["mc_updated"].stringValue
            let termName = result["mc_termname"].stringValue
            let status = result["mc_status"].stringValue
    
            category.setValue(id, forKey: "mc_termid")
            category.setValue(name, forKey: "mc_name")
            category.setValue(parent, forKey: "mc_parent")
            category.setValue(color, forKey: "mc_color")
            category.setValue(update, forKey: "mc_updated")
            category.setValue(termName, forKey: "mc_termname")
            category.setValue(status, forKey: "mc_status")
        }
        do
        {
            try context.save()
            print("Category save to local database successfully.")
        }
        catch
        {
    
        }
    })
    

    【讨论】:

    • 认真的吗?我犯了一个粗心的错误?哈哈
    • 你帮我解决这个问题,我现在做到了...我不小心弄错了 omg
    • 编码愉快! :) 大多数时候,让整个项目停止只是一个愚蠢的错误!
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2010-11-09
    相关资源
    最近更新 更多