1

So I have a problem where I call save after putting some items in an object. I go to an API and I download some info and then I save it to another system for temporary use, however the .save() seems to only save 2 items, with no special pattern in which it selects. Can someone explain what the problem is?

let url = URL(string: link)
    let spots = PFObject(className:"spot")
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in

        if error != nil {
            print(error)
        }
        else{
            if let urlContent = data{

                do{
                    let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
                    let results = (jsonResult as! NSDictionary)["results"]
                    let venueList = (results as! NSArray)
                    //print(jsonResult)
                    var i = 0
                    while i < venueList.count{
                        let venues = venueList[i] as! NSDictionary
                        let name = venues["name"] as! String
                        let geometry = venues["geometry"] as! NSDictionary
                        let location = geometry["location"] as! NSDictionary
                        let cLat = location["lat"] as! Double
                        let cLon = location["lng"] as! Double
                        let vPoint = PFGeoPoint(latitude: cLat, longitude: cLon)
                        //print(name," ", vPoint)
                        spots["venue"] = name
                        spots["name"] = vPoint
                        do{
                           try HotSpots.save()
                            print("Saved! ",name," ", vPoint)
                        }
                        catch{
                            print("Save Error")
                        }
                        i+=1
                    }




                }
                catch{
                    print("JSON Error")
                }
            }
        }
    }
    task.resume()
}
2
  • 1
    What is HotSpots? Side-note: while i < ... and incrementing a counter is very bad syntax in Swift. The usual syntax is a for - in. Commented Sep 4, 2016 at 19:23
  • I'll make sure to fix that! Thanks Commented Sep 4, 2016 at 19:26

1 Answer 1

2

The issue is that you're saving the two values always in the same PFObject instance.

Move the line let spots = PFObject(className:"spot") in the loop.

PS: Use Swift native types. For example this is more efficient

let location = geometry["location"] as! [String:Double]
let cLat = location["lat"]!
let cLon = location["lng"]!
Sign up to request clarification or add additional context in comments.

1 Comment

That did it, Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.