gpt4 book ai didi

swift - 尝试获取核心数据中的对象时出错 "-[__NSArrayI entity] : unrecognized selector sent to instance"

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

我正在尝试从 Parse 获取所有数据,并将这些数据与手机中的核心数据同步。我目前遇到一个提取请求未正确执行的小问题。我认为原因是我将 ManagedObjectContext 用于不同的任务,但我真的不知道如何解决这个问题。

我在代码中清楚地指出了问题发生的位置。这很奇怪,因为我的代码没有崩溃,但它只是将错误打印到日志中。 (我已将结果显示到代码下方的日志中)

这是我的代码:

// 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.
["test1@info.com", "test2@info.com", "test3@info.com", "test4@info.com", "test5@info.com", "test6@info.com"]
te1t2@info.com
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() 上设置中断来消除解析警告。我在当前代码中看不到任何前台 Parse 调用,因此它一定在其他地方。 This SO question有一个答案展示了如何定义断点。

其次,以下代码值得关注:

self.messages[i].imageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error == nil{
newGroup.photo = imageData
}else{
print(error)
}
})

newGroup 是一个 CoreData 实体,您将保存其 MOC,但同时也会在后台更新它。作为查看是否导致问题的简单第一步,请注释掉此代码。理想情况下,您只想在所有图像加载结束时保存 MOC 一次。

第三,您的代码崩溃了 - “无法识别的选择器发送到实例”。您可以通过中断所有异常来查看发生这种情况的位置。在断点导航器下执行此操作:

Add breakpoint

关于swift - 尝试获取核心数据中的对象时出错 "-[__NSArrayI entity] : unrecognized selector sent to instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36254572/

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