2016-03-26 16 views
1

まだコアデータ関係に問題があります。Coredata/SWIFTは関係があるレコードを取得します

例:

エンティティユーザー エンティティ装置(=名前の属性IDDを、)(IDU、NameU =属性)

core data model 質問:私は、新しいデバイスを作成するときに、私はNssetの問題を持っていますユーザーにリンクされています。

コード:

@IBAction func addDeviceBoutonAction(sender: AnyObject) { 

     print("Creation Device debut") 
     var nbCharacteres = Int((newDeviceNameTextField.text?.characters.count)!) 
     if currentUserName == "" { 
      print("Erreur creation device/currentUserName = Vide --> Abort") 
      } 
      else if nbCharacteres == 0 { 
      print("Erreur creation device/newDeviceNameTextField = Vide --> Abort") 
      } 
      else 
      { 
      // init du MOC 
      let MOC = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext 
     // creation de la requete sur l'entité User avec un predicat : on recupere que les infos du user selectionné dans le pickerViewUser (on 
     // travaille avec la variable globale currentUserName qui a été initialisée lors du didSelectRow du pickerViewUser) 
     let fetchRequestUser = NSFetchRequest(entityName: "User") 
     print("Creation device/trace currentUserName = \(currentUserName)") 
     let userPredicate = NSPredicate(format: "userName == %@", currentUserName) 
     // On colle le predicat dans la requete 
     fetchRequestUser.predicate = userPredicate 

     var requestError: NSError? 
     let existingUser = (try! MOC.executeFetchRequest(fetchRequestUser)) as! [User] 
     print("Creation device/trace existingUser[0].userName = \(existingUser[0].userName)") 
     // il faut passer par l'ID plutot que le nom 
     // firstUserFound de type User 
     let firstUserFound = existingUser[0] 
       print("Creation device firstUserFound = \(firstUserFound)")  

     // Insertion d'un nouveau Device dans la base (relationship n to n entre Device et User) 
     let existingDevicesForThisUser = firstUserFound.mutableSetValueForKey("device") 

     // Create Item Record 
     let newDevice = NSEntityDescription.insertNewObjectForEntityForName("Device",inManagedObjectContext: MOC) as! Device 
     //if let newDevice = createRecordForEntity("Device", inManagedObjectContext: MOC) { 
     // Set Attributes and values 
     newDevice.setValue(NSUUID().UUIDString, forKey: "deviceId") 
     newDevice.setValue(newDeviceNameTextField.text, forKey: "deviceName") 
     newDeviceNameTextField.text = "" 
     self.view.endEditing(true) 
     //newDevice.setValue("Device \(newDevice.count + 1)", forKey: "deviceId") 
     // Set Relationship 
     print("avant relationhips") 
     newDevice.setValue(firstUserFound, forKey: "users") 

エラー: 作成デバイスデビュー

私はそれが

まだ働いていないUPDATE多く、多くの関係に関連していると思います

作成デバイス/トレース電流ユーザー名= TOTO

作成装置/ existingUserトレース[0] .userName =オプション( "TOTO")

作成装置firstUserFound =(エンティティ:ユーザー。 ID:0xd000000000080000;データ:{ devices = ""; userId = "8A32EAC6-87E2-4CF1-8265-5E7A32ABCEBB"; userName = toto; 55:13.065 testcoredatarelationships [1568:370426] })

前衛は

2016年4月2日13 relationhips *によりキャッチされない例外 'NSInvalidArgumentException'、理由にアプリを終了:「への値の許容できないタイプ - 多少の関係:property = "users";所望のタイプ= NSSet;与えられた型= testcoredatarelationships.User;値=(エンティティ:ユーザー; ID:0xd000000000080000;データ:{ デバイス= ""; userId = "8A32EAC6-87E2-4CF1-8265-5E7A32ABCEBB"; userName = toto; })。 *まずスローコールスタック:間違っている

ライン: newDevice.setValue(firstUserFound、forKey: "ユーザー")

拡張デバイス{

@NSManaged var deviceId: String? 
@NSManaged var deviceName: String? 
@NSManaged var cards: NSSet? 
**@NSManaged var users: NSSet?** 

}

+0

あなたはすでにJackインスタンスをお持ちですか?あなたの関係は双方向ですか(そうすべきですか)?エンティティモデルの画像を表示します。 – Wain

+0

こんにちは、実際には、それはちょうどダミーのデータです。私はちょうどエンティティ間の関係の船が動作しているかを理解するために、実際の例を作成したいと思います(一部のフィールドが異なっているが、それは[OK]をする必要があり) – h3630fr

+0

コアモデルを置きます。そう( – h3630fr

答えて

1

ますジャックのデバイスを入手する最も簡単な方法は多対多の関係を使用しているため、ジャック(フェッチ要求あり)を取得してデバイスに問い合わせることです。これにより、リレーションシップがナビゲートされるため、別のフェッチは必要ありません。

特定の名前のユーザーと関係のあるデバイスを直接取得することはできますが、それはもっと複雑です。

+0

ユーザーとデバイスは、n個の関係/デバイスとカードへのnに1であるべきである(完了するために助けが必要)、いくつかの1つのデバイス用のカードが、それぞれのカードを私はこの単純なコアデータとの単純なビューを作成しているが、私はそれを達成することはできませんあなたの2件の回答について – h3630fr

+0

)例えば=リファレンスつのデバイスに専用されている:どのようにそれを行うだろうか?ユーザーにはデバイス属性がないため2番目のケースについて、カーンはデバイスで直接取得する方法を教えてください。あなたは両方の可能性の例を持っていますか?あなたはユーザーインスタンス、 'jack'を持っている場合 – h3630fr

+0

が、単に' jack.devices' – Wain

関連する問題