无法在swift中删除NSURLConnection中的NSData值

时间:2015-08-19 08:37:47

标签: swift nsurlconnection nsdata

我在UITableView中填充约会列表。单击 viewappointment 按钮后,约会值将通过API获取并使用NSURLConnection获取值。成功获得5个约会并填入UITableView。

如果我再次点击该按钮,则会显示10个约会。 如果我再次点击,则会显示15个约会。

相同的数据正在重复。我不知道如何停止重复。

我使用下面的行删除了新数据附加之前已经存在的所有数据。请指导我。

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

编码部分:

//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.... 
        }
    }

1 个答案:

答案 0 :(得分:1)

didReceiveResponse中将NSMutableData实例的长度设置为0

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

并删除appoApiData.setData(NSData())