快速复制具有重复实体值的核心数据 [英] core data with duplicate entity values in swift

查看:48
本文介绍了快速复制具有重复实体值的核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Json解析一些数据并保存到coredata中,在获取显示到tableview中的核心数据可以正常工作之后,tableview反复显示所有值如何避免重复值我尝试了很多方法但没有找到方法

I am parsing some data from Json and saved into the coredata,after fetching core data showing into the tableview working fine, tableview is show all the value in repeatedly how can avoid the repeated values I tried many ways but not find way

Json格式

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c202",
                "name": "Leonardo Dicaprio",
                "email": "leonardo_dicaprio@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        }
  ]
}

当我获取显示重复值的名称"时

when I fetching "name" showing repeated values

这些是保存并提取代码

func getfetchdata()
{

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Contacts")


    do{
        let fetchResults = try self.context.fetch(fetchRequest) as? [Contacts]
    if fetchResults!.count > 0 {

        for bEntity in fetchResults! {

            let employeeEntity =  bEntity

            print(employeeEntity.name as Any)

            TableviewData.append(ContactsDataVal.init(name: employeeEntity.name!,
                                                      id: employeeEntity.id!, email: employeeEntity.email!, gender: employeeEntity.gender!, address: employeeEntity.address!))

        }

      print("data values already")

        }}
    catch let error as NSError
    {
        print(error)
    }
}
func getdata()
{

    let url = URL(string: "https://api.androidhive.info/contacts/")

    URLSession.shared.dataTask(with: url!) { (Data, response, error) in
       do
        {
            let data = try JSONSerialization.jsonObject(with: Data!, options: JSONSerialization.ReadingOptions.allowFragments)as! [String:AnyObject]

            let arraydata = data["contacts"]as! [[String:Any]]


            for arravalues in arraydata
            {

                let entityDescription = NSEntityDescription.entity(forEntityName: "Contacts", in:self.context)

                let favouriteObj = Contacts(entity: entityDescription!, insertInto: self.context)

                favouriteObj.name = arravalues["name"] as? String

                favouriteObj.id = arravalues["id"] as? String

                favouriteObj.email = arravalues["email"] as? String

                favouriteObj.gender = arravalues["gender"] as? String

                favouriteObj.address = arravalues["address"] as? String

                do {
                    try self.context.save()
                }

            }

        }catch let error as NSError{

            print("error",error)
        }
    }
        .resume()
  }

如何避免核心数据中的重复值并在表格视图中显示适当的数据

how to avoid repeated values in core data and show proper data into the tableview

推荐答案

也许对提取请求可以做些类似于

Maybe there is something that can be done to the fetch request similar to How to get distinct results from a single field in Core Data (Swift 4) but another option would be to remove the duplicates by simply creating a set from the fetch result:

let fetchSet = Set(fetchResults)

然后遍历集合

这篇关于快速复制具有重复实体值的核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆