gpt4 book ai didi

ios - 将当前用户存储在核心数据中

转载 作者:行者123 更新时间:2023-11-30 13:27:56 25 4
gpt4 key购买 nike

我的代码发生了一些非常奇怪的事情,我不知道如何修复它。当我想在核心数据中存储一些用户时,它可以工作,但是当我想存储当前用户时,它不起作用,显然我尝试保存的数据为零,但是当我将相同的数据打印到日志时,一切似乎都是这样完全没问题

在 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")
}

}

这就是打印到日志的内容,我打印所有内容,因为重要的是要看到我传递的数据不应该为零:

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})}")]

如果有人找到我的问题的解决方案,我将非常感激,我真的不知道如何解决这个问题!

最佳答案

如果你查看苹果文档,你会发现:

NSValidationMultipleErrorsError = 1560, NSValidationMissingMandatoryPropertyError = 1570,

这可能意味着您尝试保存强制属性设置为 nil 的对象。

关于错误,您可以在那里找到: 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/

关于ios - 将当前用户存储在核心数据中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36919951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com