2016-06-16 2 views
1

현재 응용 프로그램에서 핵심 데이터를 사용하고 있습니다. 나는 다음과 같은 엔티티를 가진다 : 알림 (to-1), People (to-many). 다음과 같이 실체는 다음과 같습니다작업을 완료 할 수 없습니다. (코코아 오류 133021) + swift 2

schema for Notification entity

schema for People entity

인민 개체 필드 ID입니다 고유 제한 조건이 있습니다. 기본적으로 Person (People 엔터티에 저장 될 것입니다.)에서 알림 (Notification 엔터티에 저장 됨)을 받겠습니다. 특정 id를 가진 사람이 하나 이상의 알림을 보내고 새 항목을 만들지 않으면 (복제 될 사람) 엔티티를 업데이트하려고합니다. 내가 뭐하는 거지

의 I는 다음과 같은 오류 얻을 위 :

작업을 완료 할 수 없습니다 있습니다. (코코아 오류 133021)

누군가이 문제를 해결할 수 있도록 도와 줄 수 있습니까? 아래는 내 코드와 내가 저장하려고하는 데이터의 예입니다.

let entityNotification = NSEntityDescription.entityForName("Notification", inManagedObjectContext: self.managedContext) 

    let newNotification = Notification(entity: entityNotification!, insertIntoManagedObjectContext: self.managedContext) 

    newNotification.message = data["message"] as? String 

    if let actor = data["actor"] as? [String : AnyObject] 
    { 
     let newPeople = NSEntityDescription.insertNewObjectForEntityForName("People", inManagedObjectContext: self.managedContext) as! People 

     newPeople.id = actor["id"] as? Int 
     newPeople.name = actor["name"] as? String 

     newNotification.actor = newPeople 
    } 

    if let designator = data["designator"] as? [String : AnyObject] 
    { 
     let newPeople = NSEntityDescription.insertNewObjectForEntityForName("People", inManagedObjectContext: self.managedContext) as! People 

     newPeople.id = designator["id"] as? Int 
     newPeople.name = designator["name"] as? String 

     newNotification.designator = newPeople 
    } 

    do 
    { 
     try newNotification.managedObjectContext?.save() 
    } 
    catch let error as NSError 
    { 
     print(error.localizedDescription) 
    } 

데이터 모델 :

let notif = ["message" : "testing", 
       "actor" : ["id": 1, "name": "jim"], 
       "designator" : ["id": 2, "name": "dave"]] 

    let notif1 = ["message" : "testing 1", 
       "actor" : ["id": 1, "name": "jim21"], 
       "designator" : ["id": 2, "name": "dave"]] 
+0

NSMergeByPropertyObjectTrumpMergePolicy에 문맥에 mergePolicy을 설정 한 후 결과가 당신이 원하는 정말 무엇인지 확인하기 위해 그것을 밖으로 시도 할 필요가 기존 g'People' 인스턴스 – Paulw11

+0

CoreData를로드하려고 할 튜토리얼 : https://iosdevcenters.blogspot.com/2016/06/crud-operations-using-code-data-swift.html –

답변

4

중복 생성의 문제를 해결하는 전통적인 방법은 그 정체성과 Person에 대한 요청을 가져 오는 대신 새로운 만드는 업데이트하는 것입니다. 이것은 또한 당신에게 가장 좋은 선택이 될 수 있습니다.

고유 한 제약 조건을 사용하면 핵심 데이터가 병합을 수행 할 수 있지만이를 알려야합니다. 현재는 오류가 발생하여 병합되고 있는데 이는 그리 유용하지 않습니다. 당신은 ... 아무데도 코드에서

+0

그것이 작동했습니다 .. 정말 고마워요. – iMobileBand

관련 문제