user4916078
user4916078

Reputation:

Parsing XML data to Core Data

I'm trying to fetch data from an online xml file and put it inside my core data.

I can successfully retrieve the data and I can even put it inside core data, but it seems that only the last retrieved feed is stored.

Here is the example of the xml file I'm trying to retrieve.

Parser code example:

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        if (elementName as NSString).isEqualToString("question") {
            if !xml_tmp1.isEqual(nil) {
                elements.setObject(xml_tmp1, forKey: "text")
            }
            if !xml_tmp2.isEqual(nil) {
                elements.setObject(xml_tmp2, forKey: "answer0")
            }

            if !xml_tmp3.isEqual(nil) {
                elements.setObject(xml_tmp3, forKey: "answer1")
            }

            if !xml_tmp4.isEqual(nil) {
                elements.setObject(xml_tmp4, forKey: "answer2")
            }

            if !xml_tmp5.isEqual(nil) {
                elements.setObject(xml_tmp5, forKey: "answer3")
            }
            tmp_xml.addObject(elements)
        }
    }

Core Data code example:

    func temp_func() {
        var n: Int = tmp_xml.count
        var i: Int = 0

        var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
        var context: NSManagedObjectContext = appDel.managedObjectContext!

        var tmp = NSEntityDescription.insertNewObjectForEntityForName("Questions", inManagedObjectContext: context) as! NSManagedObject

        while (i != br) {
            tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("text"), forKey: "question")
            tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("answer0"), forKey: "answer_1")
            tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("answer1"), forKey: "answer_2")
            tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("answer2"), forKey: "answer_3")
            tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("answer3"), forKey: "answer_4")

            i++
        }

        context.save(nil)
    }

So basically, I should get three new entries in my core data, but only the last one ( 15 * 7 question ) is stored.

Upvotes: 3

Views: 1508

Answers (1)

Zhengjie
Zhengjie

Reputation: 451

I think U just create one entry in U core data and U setValue than save it. So only last Entry save in the core data. U can just create entry in the while loop. like this see it can work ?

func temp_func() {
    var n: Int = tmp_xml.count
    var i: Int = 0

    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    var context: NSManagedObjectContext = appDel.managedObjectContext!


    while (i != br) {
        var tmp = NSEntityDescription.insertNewObjectForEntityForName("Questions", inManagedObjectContext: context) as! NSManagedObject
        tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("text"), forKey: "question")
        tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("answer0"), forKey: "answer_1")
        tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("answer1"), forKey: "answer_2")
        tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("answer2"), forKey: "answer_3")
        tmp.setValue(tmp_xml.objectAtIndex(i).valueForKey("answer3"), forKey: "answer_4")

        i++
    }

    context.save(nil)
}

Upvotes: 2

Related Questions