我的代码发生了一些非常奇怪的事情,我不知道如何修复它。 当我想将一些用户存储在核心数据中时,它可以工作,但是当我想存储我当前的用户时,它不起作用,显然我尝试保存的数据是零,但当我将相同的数据打印到日志时,一切似乎非常好
在for循环中,我遍历我要存储的所有用户,如果用户名等于当前用户的用户名,则存储当前用户,否则我存储其他用户。
这真的很奇怪,因为通常两个操作,添加当前用户或添加其他用户都是相同的。
我的代码:
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")
}
}
这是打印到日志的内容,我打印所有内容,因为重要的是看到我传递的数据不应该是nil:
antoine@info.com
antoine
Optional("antoine@info.com")
TEST 2 - interacting user has been saved to core data
thomas@info.com
thomas
Optional("thomas@info.com")
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})}")]
如果有人找到我的问题的解决方案,我真的很感激,我真的不知道如何解决这个问题!
答案 0 :(得分:0)
如果您查看Apple doc,您会发现:
NSValidationMultipleErrorsError = 1560, NSValidationMissingMandatoryPropertyError = 1570,
这可能意味着您尝试使用设置为nil的强制属性保存对象。
关于您可以在那里找到的错误: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/ObjectValidation.html
答案 1 :(得分:0)
我找到了解决方案t,pheww ......
我必须在每个IF语句的范围内实际使用实体和newUser,这就是它......
无论如何,感谢您的帮助;)没有您的帖子我不会注意实体变量的位置。再次感谢