2016-05-19 5 views
2

コアデータのエンティティの一意性制約が機能しない(ユニークアイテムの名前を作るために)。私が読んだことはすべて、それがかなりシンプルだと言います - 制約を設定し、エラーを処理してください。私は何の誤りもなく、何度も同じエントリーを追加することができます。私は新しいエンティティ制約インスペクタでコアデータにおける制約を設定しようとしています

アプリはIOS 9.0を必要としない、Xcodeのツールの要件は7.0

制約、category1Nameに設定され、文字列です。

私のaddItemコードは次のとおりです。

func addNewRecord() { 

    //check to be sure the entry is not empty 
    if (categoryTextField.text == "") { 

     //prompt requiring a name 
     let ac = UIAlertController(title: nil, message: "Name Required", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil)) 
     self.presentViewController(ac, animated: true, completion: nil) 

    } else { 

    let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName("Category1", inManagedObjectContext: kAppDelegate.managedObjectContext) as! Category1 

    newManagedObject.category1Name = categoryTextField.text 
    newManagedObject.category1Description = categoryTextView.text 

    //bunch more items... 

    //save it 
    kAppDelegate.saveContext() 
    makeEntryFieldsEnabledNO() 
    performSegueWithIdentifier("unwindToCategoriesTableViewController", sender: self) 

    }//if else 

}//addNewRecord 

AppDelegateが保存標準です:

func saveContext() { 
    if managedObjectContext.hasChanges { 
     do { 
      try managedObjectContext.save() 
     } catch { 

      //insert your standard error alert stuff here 
      let nserror = error as NSError 
      print("From the print line: Unresolved error \(nserror), \(nserror.userInfo)") 

      abort() 
     }//do catch 
    }//if moc 
}//saveContext 

はここでコアデータの制約です:

enter image description here

このアプリはiCloudのが有効になっています。

managedObjectContextポリシーをマージがNSMergeByPropertyObjectTrumpMergePolicy

lazy var managedObjectContext: NSManagedObjectContext = { 
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail. 
    let coordinator = self.persistentStoreCoordinator 
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) 
    managedObjectContext.persistentStoreCoordinator = coordinator 
    managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy 
    return managedObjectContext 

}()//var managedObjectContext 

に設定されているすべての指導をいただければ幸いです。

+2

私は制約が正しく保存されていることを確認倍になる - そこにある(またはあった)、それはモデルを更新することができなかったのXcodeのバグ - [この答え]を参照(http://stackoverflow.com/a/35682163を/ 3985749)。 – pbasdf

答えて

0

マージ競合が発生したときにエラーを取得して手動で処理する場合は、ポリシーをNSErrorMergePolicyに変更する必要があります。エラーが発生し、マージ競合の解決に必要なオブジェクトIDがユーザー情報に表示されます。それ以外の場合は、指定されたマージポリシーに従ってマージして保存します。 属性と関係を上書きしてNSMergeByPropertyObjectTrumpMergePolicyを指定する場合は、設定したポリシーによってオブジェクト属性は上書きされますが、関係は上書きされません。

+1

ありがとうございます。私はそれについてのテストをします。しかし、ユニーク制約は単に起こっていません。私は、同じcategory1Namesでたくさんの新しいオブジェクトを作成することができます。 – user2698617

2

pbasdfからのコメントは、上記正しいように思えます。インスペクタの制約は保存されません。私は、提供されたリンクで提案された両方の方法を使用しました - 制約を追加し、別の属性を変更し、ファイルを保存してから、その属性を元に戻してファイルを再度保存しました。この制約は、今私が期待するように機能します。私はこれを答えとしてマークします。 pbasdfはクレジットを取得する必要があります。

関連する問題