一些奇怪的東西與我的代碼發生的事情,我不知道如何解決它。 當我想在存儲核心數據部分用戶它的工作原理,但我想我保存當前用戶,這是行不通的,顯然我嘗試保存的數據是零,但是當我打印此相同的數據到日誌中,應有盡有似乎完全沒有下降到當前用戶存儲核心數據
在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")
}
}
這就是被打印到日誌中,我打印的一切,因爲它看到我傳遞的數據不應該是零是很重要的:
[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})}")]
如果有人發現我的問題的解決方案,我真的很感激,我真的不知道如何解決這個問題!
顯示您管理的對象上下文請。並儘量不要使用經典的if語句! –
感謝您的幫助,我將託管對象上下文添加到代碼中。但是,不使用經典的if語句意味着什麼?你的意思是使用switch語句嗎? – C00kieMonsta
對不起,如果!我的意思是for-in週期而不是for。 –