McDonal_11
McDonal_11

Reputation: 4075

Unable to delete NSData values in NSURLConnection in swift

I am populating list of appointments in UITableView. Once viewappointment button clicked, appointments values are getting through API and using NSURLConnection to get values. Successfully getting 5 appointments and populated in UITableView.

If I am clicking again that button, 10 appointments are displaying. If I am clicking again, 15 appointments are displaying.

Same datas are repeating. I dont know how to stop repetition.

I have used below line to remove all datas which already present before new datas get append. Kindly guide me.

appoApiData.setData(NSData())  //TO REMOVE ALL DATAS

Coding Part:

//BUTON ACTION: 
appointmentAPI() //FUNCTION CALL

    func appointmentAPI()
    {
        let url = NSURL(string: baseURL + "appointment/getbyuser/" + String(sharedResources.shared.id))
        let request = NSMutableURLRequest(URL: url!)

        appoApiConnection = NSURLConnection(request: request, delegate: self)! //NSURLConnection Delegates Method Call

        println("URL CONNEXN \(appoApiConnection)")
    }

    //NSURL CONNECTION DELEGATES  
    func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
    {
        if(connection == appoApiConnection)
        {
            println("RESPONSE_APPO: \(response)")
        }

    }

    func connection(connection: NSURLConnection!, didReceiveData conData: NSData!)
    {
        if(connection == appoApiConnection)
        {
            appoApiData.setData(NSData()) //CLEARING ALL PREVIOUS VALUES
            self.appoApiData.appendData(conData)
            println("appo_data  \(appoApiData.length)")

            //Button Click 1st time: appo_data = 5 
            //Button Click 1st time: appo_data = 10 
            //Button Click 1st time: appo_data = 15
            // and so on.... 
        }
    }

Upvotes: 1

Views: 212

Answers (1)

vadian
vadian

Reputation: 285064

In didReceiveResponse set the length of the NSMutableData instance to 0

func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
{
    if(connection == appoApiConnection)
    {
      appoApiData.length = 0            
      println("RESPONSE_APPO: \(response)")
    }
}

and delete appoApiData.setData(NSData())

Upvotes: 1

Related Questions