JSON出现问题-数据重复

时间:2019-03-04 10:18:55

标签: ios swift

我有一个应用程序,问题是在tableView中重复数据。

如何修复Array中的数据复制?

func jsonGet(page: Int) {
    let pathFull = "https://test.com"
    guard let url = URL(string: pathFull) else {return} //
    URLSession.shared.dataTask(with: url) { (data, response, err) in
        guard let data = data else {return}
        do {
            let posts =  try JSONDecoder().decode(JobsData.self, from: data)
            for post in posts.jobs.data {
                self.newsPost.append(post)
            }

            DispatchQueue.main.async {
                self.tableView.reloadData()
            }

        } catch let jsonErr {
            self.alertViewBaisc(title: "erorr", text: "error", button: "ok")
            print("Error serializing json", jsonErr)
        }
        }.resume()
}

5 个答案:

答案 0 :(得分:0)

问题是您要继续插入newsPost数据,请参见self.newsPost.append(post)

替换代码
func jsonGet(page: Int) {
    let pathFull = "https://test.com"
    guard let url = URL(string: pathFull) else {return} //
    URLSession.shared.dataTask(with: url) { (data, response, err) in
        guard let data = data else {return}
        do {
            let posts =  try JSONDecoder().decode(JobsData.self, from: data)
            var tempPost = [DataType]()
            for post in posts.jobs.data {
                tempPost.append(post)
            }
           self. newsPost =  tempPost

            DispatchQueue.main.async {
                self.tableView.reloadData()
            }

        } catch let jsonErr {
            self.alertViewBaisc(title: "erorr", text: "error", button: "ok")
            print("Error serializing json", jsonErr)
        }
        }.resume()
}

另一种方法是,您可以检查帖子是否已在您的数组中

(self. newsPost.filter{$0.uniqueKey == post.uniqueKey}.count == 0)

答案 1 :(得分:0)

您可以在每次通话时清除错误信息

self.newsPost.removeAll()

或像这样声明它

var newsPost = Set<Post>() // this will guarantee uniqueness in a single fetch 

您也不需要for-循环就可以直接执行

newsPost = posts.jobs.data

除了问题

答案 2 :(得分:0)

您总是将数据附加到newsPost中。

您实际上想做的是从newPost中删除所有值,然后附加新值。

您的完整代码应如下所示:

let pathFull = "https://test.com"
guard let url = URL(string: pathFull) else {return} //
URLSession.shared.dataTask(with: url) { (data, response, err) in
    guard let data = data else {return}
    do {
        let posts =  try JSONDecoder().decode(JobsData.self, from: data)

        self.newPost.removeAll()  // < this line.

        for post in posts.jobs.data {
            self.newsPost.append(post)
        }

        DispatchQueue.main.async {
            self.tableView.reloadData()
        }

    } catch let jsonErr {
        self.alertViewBaisc(title: "erorr", text: "error", button: "ok")
        print("Error serializing json", jsonErr)
    }
    }.resume()
}

答案 3 :(得分:0)

您可以使用contains来解决此问题:

let posts =  try JSONDecoder().decode(JobsData.self, from: data)
            for post in posts.jobs.data {
                if self.newPost contains(post){
                    //Skip
                 }
                 else{
                     self.newsPost.append(post)
                 }
            }

答案 4 :(得分:0)

只需替换

for post in posts.jobs.data {
    self.newsPost.append(post)
}

self.newsPosts = posts.jobs.data

由于新数据替换了旧数据,因此避免了重复的重复循环以及重复的条目。