如何使用swift 3.0更新核心数据中的多个记录

时间:2017-05-29 09:24:46

标签: core-data swift3 nsurlsession insert-update

我有一个json文件,我想通过它将json变量插入到核心数据中   - 我有解析的json   - 调用插入功能以保存数据   - 调用fetch函数来显示数据

  • 为了避免重复输入,我在解析之前应用了删除查询。

    • 但从功能上来说,每次加载视图时都会出错,所有记录都会被解析和插入。

    • 所以我想申请更新查询,但不知道,如何。

下面的

是我的json文件

"Data": [ { "NID": 9, "NoticeType": "General", "NoticeDetails": "Test notice to all school", "NoticeTitle": "Test notice to all school", }, { "NID": 6, "NoticeType": "General", "NoticeDetails": "Notice to all school tset purpose", "NoticeTitle": "Notice to all school tset purpose", }, { "NID": 5, "NoticeType": "General", "NoticeDetails": "Test to all school", "NoticeTitle": "Test to all school", }, { "NID": 4, "NoticeType": "General", "NoticeDetails": "notice to all test", "NoticeTitle": "notice to all test", }, { "NID": 3, "NoticeType": "General", "NoticeDetails": "Test notice for all", "NoticeTitle": "Test notice for all", } ]

下面是我的解析,插入和获取数据的代码

                    let responseJson = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String : AnyObject]


                    if let dataArray = responseJson?["Data"] as? NSArray
                    {
                        for i in 0..<(dataArray as AnyObject).count
                        {
                            let jsonObjectInLoop = dataArray[i] as! [String : Any]


                            let NIDObject = jsonObjectInLoop["NID"] as! Int32
                            print("Print NID Object from json loop \(NIDObject)")

                            let noticeType = jsonObjectInLoop["NoticeType"] as! String
                            let noticeDetail = jsonObjectInLoop["NoticeDetails"] as! String
                            let noticeTitle = jsonObjectInLoop["NoticeTitle"] as! String

                            let naFileNameString = ""

                                DatabseManager.sharedInstanceOfDatabase_Manager.insertNoticeFunc(nid_p: NIDObject , noticeType_p: noticeType , noticeDetails_p: noticeDetail , noticeTitle_p: noticeTitle)



                        }

                        self.fetchNotice()
                        DispatchQueue.main.async()
                        {
                                //code
                                self.DetailsTableView.reloadData()
                                self.refreshControl.endRefreshing()
                        }




                    }


                }
////

func insertNoticeFunc(nid_p: Int32, noticeType_p: String, noticeDetails_p: String, noticeTitle_p: String)
    {

        //Insert Data
        let contextVar = appDelegateShared.persistentContainer.viewContext
        let noticeDetailContext = NoticeDetails(context: contextVar)

        noticeDetailContext.nid = nid_p

        noticeDetailContext.noticeType = noticeType_p
        noticeDetailContext.noticeDetails = noticeDetails_p
        noticeDetailContext.noticeTitle = noticeTitle_p

        appDelegateShared.saveContext()

    }



/////

 func fetchNotice() -> Void
    {
        let contextVar = appDelegateShared.persistentContainer.viewContext

        //Fetch Data
        do
        {
            let fetchCoreData = try contextVar.fetch(NoticeDetails.fetchRequest())
            let noticeResult = fetchCoreData as! [NoticeDetails]

            print("notice result count \(noticeResult.count)")


            for notice in noticeResult
            {


                //Store Id's in NSUserDefault
                UserDefaults.standard.set(notice.nid, forKey: "nID_key")
                UserDefaults.standard.synchronize()

                print("NID from Core data \(notice.nid)")
                self.noticeIDArray.append(notice.nid)

                self.dateArray.append(notice.noticeDate!)
                self.noticeTitleArray.append(notice.noticeTitle!)
                self.imageUrlArray.append(notice.imageUrl!)
                self.noticeDescArray.append(notice.noticeDetails!)
                self.postedByArray.append(notice.postedBy!)

            }
            //print("Date Array : \(self.dateArray)")
        }
        catch
        {
            print("Error in fetch notice")
        }
    }

我搜索了很多,但我找不到合适的解决方案。 请尽可能提供任何解决方案

1 个答案:

答案 0 :(得分:0)

如果您没有从后端收到唯一的持久性ID,那么唯一的方法是删除所有条目,然后插入新条目。

在您的情况下,似乎您将NID作为唯一标识符。这意味着您可以从数据库中获取条目,或者如果它不存在则创建一个新条目:

let noticeDetail: NoticeDetails = {
    if let existing = NoticeDetails.fetch(nid: myNID) {
        return existing
    } else {
        return NoticeDetails.createNewEntry()
    }
}()
... insert data to noticeDetail...

然后我假设有些可能会被删除,所以你有两种情况。第一台服务器可能返回值deleted: true/false,如果它被删除,请尝试从数据库中取出并删除它。

在你的情况下,你似乎没有这种奢侈,所以你需要检查阵列中缺少哪些。因此,除非您担心内存,否则最好先获取整个表,最后删除不在新响应中的条目:

var allDetails: [NoticeDetails] = NoticeDetails.findAll() // Get all details
// Iterate through parsed JSON response objects
myDetailDictionaries.forEach { item in 
   // Extract the unique identifier
   if let myNid = item[NID] as? String { 
      let noticeDetail: NoticeDetail = {
         if let (index, detail) = allDetails.enumerated.filtered { $0.nid == myNid }.first {
            allDetails.remove(at: index) // Remove the found object from the array
            return detail
         } else {
            return NoticeDetails.createNewEntry() // Create a new object
         }
      }()
   }
}
// Now allDetails consist only of objects that were not collected by the new response. So these need to be deleted.
allDetails.forEach { $0.deleteFromDatabase() }

或者,您可以查看核心数据的约束,这些约束允许您定义哪些字段需要唯一,并将其​​设置为NID将合并条目,以便只有唯一的。但这并不能解决删除问题,也不会出于多种原因提出建议。