我試圖從Parse獲取所有數據並將這些數據與我手機中的核心數據同步。我目前遇到一個沒有正確執行的提取請求的問題。我認爲原因是我正在爲不同的任務使用managedObjectContext,但我不知道如何解決這個問題。錯誤試圖獲取核心數據中的對象「 - [__ NSArrayI實體]:無法識別的選擇器發送到實例」
我在代碼中明確指出問題出在哪裏。這很奇怪,因爲我的代碼不會崩潰,但它只是向日志打印一個錯誤。 (我已經把顯示的日誌只是下面的代碼的結果)
這是我的代碼:
// CORE DATA UPDATE
for var i = 0; i < self.messages.count; i++ {
let entity = NSEntityDescription.entityForName("Groups", inManagedObjectContext: self.managedObjectContext)
let newGroup = Groups(entity: entity!, insertIntoManagedObjectContext: self.managedObjectContext)
newGroup.groupId = self.messages[i].groupId
newGroup.updatedAt = NSDate()
newGroup.groupName = self.messages[i].groupName
self.messages[i].imageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error == nil{
newGroup.photo = imageData
}else{
print(error)
}
})
newGroup.lastMessage = self.messages[i].message
let usersRelationship = newGroup.mutableSetValueForKey("Users")
let arrayMembers = self.dictionaryGroupIds[self.messages[i].groupId]! as [String]
print(arrayMembers)
for member in arrayMembers {
print(member)
print("CODE IS STOPPING HERE ")
// MY CODE STOPS HERE
// When I want to print the members in the arraymembers, it never displays all the member, sometimes it is only one member than it is 3 members, but never all of them so
let fetchRequest = NSFetchRequest(entityName: "Users")
fetchRequest.predicate = NSPredicate(format: "username = %@", member)
fetchRequest.returnsObjectsAsFaults = false
do {
let result = try self.managedObjectContext.executeFetchRequest(fetchRequest)
usersRelationship.addObject(result)
}catch{
let fetchError = error as NSError
print("\(fetchError), \(fetchError.userInfo)")
}
}
do {
// This get never printed to the log
print("SUCCESS")
try newGroup.managedObjectContext?.save()
} catch {
let saveError = error as NSError
print("\(saveError), \(saveError.userInfo)")
print("CRASH")
}
}
}
這是被顯示的日誌:
2016-03-28 08:51:09.511 WeGrupp[1268:35749] Warning: A long-running operation is being executed on the main thread.
Break on warnBlockingOperationOnMainThread() to debug.
2016-03-28 08:51:09.823 WeGrupp[1268:35749] Warning: A long-running operation is being executed on the main thread.
Break on warnBlockingOperationOnMainThread() to debug.
["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"]
[email protected]
CODE IS STOPPING HERE
2016-03-28 08:51:10.140 WeGrupp[1268:35749] -[__NSArrayI entity]: unrecognized selector sent to instance 0x7fb0eb4d5c60
2016-03-28 08:51:10.143 WeGrupp[1268:35749] Warning: A long-running operation is being executed on the main thread.
Break on warnBlockingOperationOnMainThread() to debug.
因此,這是它,我認爲它與併發性有關,但不幸的是我找不到我的問題的答案。我一直在其他論壇上,但沒有人能提供正確的答案。
非常感謝
您是否嘗試過做什麼的警告提示,並把休息的warnBlockingOperationOnMainThread上看到它的發生?不過,我懷疑這個問題是在newGroup變量不斷變化時試圖在後臺設置照片。 – Michael
您是否試圖將照片保存到CoreData?它可能無法正常工作,因爲它在後臺加載,也許在保存MOC後。 – Michael
感謝您的幫助邁克爾,但是您能否更準確地瞭解您認爲我可能會做的事情,因爲我沒有真正瞭解它。 實際上我想要做的就是鏈接所有用戶,作爲組的一部分,我只是在上面的代碼中添加了相同的視圖。因爲我在用戶和組之間創建了多對多關係。每個組都有很多用戶,每個用戶可以有很多組 – C00kieMonsta