2016-04-28 7 views
0

コードで実際に何か不思議なことが起こっていて、それを修正する方法がわかりません。 一部のユーザーをコアデータに保存するときには機能しますが、現在のユーザーを保存する場合は機能しません。保存しようとするデータは無制限ですが、この同じデータをログに出力すると、コアデータに現在のユーザーを格納するために落ちる

forループでは、保存したいすべてのユーザーをループし、現在のユーザーを保存するよりも現在のユーザーのユーザー名と等しい場合は、他のユーザーを保存します。

通常、両方の操作、現在のユーザーの追加または他のユーザーの追加は同じです。

[email protected] 
antoine 
Optional("[email protected]") 
TEST 2 - interacting user has been saved to core data 
[email protected] 
thomas 
Optional("[email protected]") 
ERROR CURRENT USERS : Error Domain=NSCocoaErrorDomain Code=1560 "(null)" UserInfo={NSDetailedErrors=(
"Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo={NSValidationErrorKey=profileName, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.), NSValidationErrorObject=<WeGrupp.Users: 0x7fd4d0c3fe60> (entity: Users; id: 0x7fd4d0c13310 <x-coredata:///Users/t9279CD60-1946-485B-BE68-F1EE10D8ACFE4> ; data: {\n added = 0;\n currentUser = 0;\n groups =  (\n );\n photo = nil;\n profileName = nil;\n username = nil;\n})}", 
"Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo={NSValidationErrorKey=username, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.), NSValidationErrorObject=<WeGrupp.Users: 0x7fd4d0c3fe60> (entity: Users; id: 0x7fd4d0c13310 <x-coredata:///Users/t9279CD60-1946-485B-BE68-F1EE10D8ACFE4> ; data: {\n added = 0;\n currentUser = 0;\n groups =  (\n );\n photo = nil;\n profileName = nil;\n username = nil;\n})}")}, [NSDetailedErrors: (
"Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo={NSValidationErrorKey=profileName, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.), NSValidationErrorObject=<WeGrupp.Users: 0x7fd4d0c3fe60> (entity: Users; id: 0x7fd4d0c13310 <x-coredata:///Users/t9279CD60-1946-485B-BE68-F1EE10D8ACFE4> ; data: {\n added = 0;\n currentUser = 0;\n groups =  (\n );\n photo = nil;\n profileName = nil;\n username = nil;\n})}", 
"Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo={NSValidationErrorKey=username, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.), NSValidationErrorObject=<WeGrupp.Users: 0x7fd4d0c3fe60> (entity: Users; id: 0x7fd4d0c13310 <x-coredata:///Users/t9279CD60-1946-485B-BE68-F1EE10D8ACFE4> ; data: {\n added = 0;\n currentUser = 0;\n groups =  (\n );\n photo = nil;\n profileName = nil;\n username = nil;\n})}")] 

は私のコード:私が渡していたデータがゼロであることを想定されていないことを確認することが重要であるため、

var managedObjectContext = AppDelegate().managedObjectContext 

// MARK - Adding all the users in Core data 
for userFromParseDataBase in usersFromParseDataBase { 

    let entity = NSEntityDescription.entityForName("Users", inManagedObjectContext: self.managedObjectContext) 
    let newUser = Users(entity: entity!, insertIntoManagedObjectContext: self.managedObjectContext) 

    // Checking if the current user is correctly logged in 
    if userFromParseDataBase.username == userName { 

     print(userFromParseDataBase.username) 
     print(userFromParseDataBase.profileName) 

     // Adding other users interacting with currentUser 
     newUser.username = userFromParseDataBase.username 
     print(newUser.username) 
     newUser.profileName = userFromParseDataBase.profileName 
     let imageData = try! userFromParseDataBase.profileImage.getData() 
     if (imageData.length != 0){ 
      newUser.photo = imageData 
     } 
     newUser.currentUser = true 
     newUser.added = false 

     do { 
      try newUser.managedObjectContext?.save() 
      self.usersFromCoreData.append(newUser) 
      print("TEST 1 - current user has been saved to core data") 
     } catch { 
      let saveError = error as NSError 
      print("ERROR CURRENT USERS : \(saveError), \(saveError.userInfo)") 
     } 

     // Checking if there are added users 
    }else if self.arrayAddedUsers.contains(userFromParseDataBase.username) { 

     // Adding added users 
     newUser.username = userFromParseDataBase.username 
     newUser.profileName = userFromParseDataBase.profileName 
     let imageData = try! userFromParseDataBase.profileImage.getData() 
     if (imageData.length != 0){ 
      newUser.photo = imageData 
     } 
     newUser.currentUser = false 
     newUser.added = true 
     do { 
      try newUser.managedObjectContext?.save() 
      self.usersFromCoreData.append(newUser) 
      print("TEST 1 - added user has been saved to core data") 
     } catch { 
      let saveError = error as NSError 
      print("ERROR ADDED USERS :\(saveError), \(saveError.userInfo)") 
     } 

     // Checking if the current user interacts with users that have not been added 
    }else if self.arrayGroupMembers3.contains(userFromParseDataBase.username){ 

     print(userFromParseDataBase.username) 
     print(userFromParseDataBase.profileName) 

     // Adding other users interacting with currentUser 
     newUser.username = userFromParseDataBase.username 
     print(newUser.username) 
     newUser.profileName = userFromParseDataBase.profileName 
     let imageData = try! userFromParseDataBase.profileImage.getData() 
     if (imageData.length != 0){ 
      newUser.photo = imageData 
     } 
     newUser.currentUser = false 
     newUser.added = false 

     do { 
      try newUser.managedObjectContext?.save() 
      self.usersFromCoreData.append(newUser) 
      print("TEST 3 - interacting user has been saved to core data") 
     } catch { 
      let saveError = error as NSError 
      print("ERROR INTERACTING USERS : \(saveError), \(saveError.userInfo)") 
     } 
    }else { 
     print("No condition is true") 
    } 

} 

これは、ログに出力する内容で、私はすべてを印刷します

誰かが私の問題に対する解決策を見つけたら、本当に感謝します。私は本当にこれを解決する方法を知らない!

+0

管理オブジェクトのコンテキストを表示してください。古典的なif文を使わないようにしましょう! –

+0

ご協力いただき、ありがとうございました。管理対象オブジェクトコンテキストをコードに追加しました。しかし、古典的なif文を使用しないとどういう意味ですか?たとえば、switch文を使用することを意味しますか? – C00kieMonsta

+0

ごめんなさい!私はfor-inの代わりにサイクルを意味しました。 –

答えて

0

あなたはリンゴのドキュメントを見るとあなたのような見つける:

NSValidationMultipleErrorsError = 1560、 NSValidationMissingMandatoryPropertyError = 1570、あなたがに設定必須プロパティを持つオブジェクトを保存しようとすることを意味します

なし。あなたがそこに見つけることができるのエラーについて

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/ObjectValidation.html

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Miscellaneous/CoreData_Constants/

+0

はい、それは問題です。それらのプロパティはゼロであってはなりません。ログに表示されているように、必須ではない値を渡すので、これは本当に奇妙です – C00kieMonsta

0

私は私がスコープ内に実際にはエンティティとNEWUSERを入れて持っている... pheww、

を解トンを見つけましたそれぞれのIFステートメントのそれは...

とにかく、あなたの助けをありがとう;)私はエンティティ変数の場所に注意を払う必要はありません。ありがとうございました

関連する問題